95 lines
2.1 KiB
PHP
95 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace PSC\Library\Calc\Model;
|
|
|
|
use \Colors\RandomColor;
|
|
use PSC\Library\Calc\Graph\Tree\Node;
|
|
|
|
class Part extends Node
|
|
{
|
|
private string $color;
|
|
|
|
public function __construct(
|
|
private PartType $type,
|
|
private string $name,
|
|
private string $unParsed,
|
|
private string $parsed = '',
|
|
private array|float $result = 0,
|
|
) {
|
|
if ($parsed == '') {
|
|
$this->parsed = $unParsed;
|
|
}
|
|
parent::setName($name);
|
|
$this->color = RandomColor::one();
|
|
}
|
|
|
|
public function getType(): PartType
|
|
{
|
|
return $this->type;
|
|
}
|
|
|
|
public function getColor(): string
|
|
{
|
|
return $this->color;
|
|
}
|
|
|
|
public function getFullName(): string
|
|
{
|
|
switch ($this->type) {
|
|
case PartType::Formel:
|
|
return sprintf('$F%s$F', $this->name);
|
|
case PartType::Parameter:
|
|
return sprintf('$P%s$P', $this->name);
|
|
case PartType::Value:
|
|
return sprintf('$V%s$V', $this->name);
|
|
case PartType::CalcValue:
|
|
return sprintf('$CV%s$CV', $this->name);
|
|
default:
|
|
return $this->name;
|
|
}
|
|
}
|
|
|
|
public function getUnparsed(): string
|
|
{
|
|
return $this->unParsed;
|
|
}
|
|
|
|
public function getParsed(): string
|
|
{
|
|
return $this->parsed;
|
|
}
|
|
|
|
public function setParsed(string $parsed): void
|
|
{
|
|
$this->parsed = $parsed;
|
|
}
|
|
|
|
public function setResult(array|float $result): void
|
|
{
|
|
$this->result = $result;
|
|
}
|
|
|
|
public function getResult(): array|float
|
|
{
|
|
return $this->result;
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'name' => $this->name,
|
|
'unParsed' => $this->unParsed,
|
|
'parsed' => $this->parsed,
|
|
'result' => $this->result,
|
|
'parts' => array_reduce(
|
|
$this->getChildren(),
|
|
function ($result, $item) {
|
|
$result[] = $item->toArray();
|
|
return $result;
|
|
},
|
|
[],
|
|
),
|
|
];
|
|
}
|
|
}
|