Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #6

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getData(): array
return $this->data;
}

public function setData(iterable $data): void
public function setData(iterable $data): DataSource
{
if (!is_array($data) && !($data instanceof ArrayAccess)) {
throw new InvalidArgumentException('Data must be an array or implements ArrayAccess.');
Expand All @@ -37,34 +37,44 @@ public function setData(iterable $data): void
foreach ($data as $key => $value) {
$this->setValue($key, $value);
}

return $this;
}

public function addData(iterable $data): void
public function addData(iterable $data): DataSource
{
foreach ($data as $key => $value) {
$this->setValue($key, $value);
}

return $this;
}

public function getValue(string $key): mixed
public function getValue(string $key)
{
return $this->data[$key] ?? null;
}

public function setValue(string $key, mixed $value): void
public function setValue(string $key, $value): DataSource
{
$this->data[$key] = $value;

return $this;
}

public function unsetValue(string $key): void
public function unsetValue(string $key): DataSource
{
unset($this->data[$key]);

return $this;
}

public function unsetValues(array $keys): void
public function unsetValues(array $keys): DataSource
{
foreach ($keys as $key) {
$this->unsetValue($key);
}

return $this;
}
}
10 changes: 6 additions & 4 deletions src/LogicTreeFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace LogicTree;

use LogicTree\Node\CombineInterface;
use LogicTree\Node\ConditionInterface;
use LogicTree\Node\NodeInterface;
use LogicTree\Node\NodeResult;
use LogicTree\Operator\OperatorInterface;
use LogicTree\Operator\OperatorPool;
use LogicTree\Operator\OperatorType;
Expand All @@ -23,17 +23,19 @@ public function __construct(
$this->conditionManager = $conditionManager ?? new ConditionManager($this->operatorPool);
}

public function addOperator(OperatorType $type, string $operatorCode, OperatorInterface $operator): void
public function addOperator(OperatorType $type, string $operatorCode, OperatorInterface $operator): LogicTreeFacade
{
$this->operatorPool->addOperator($type, $operatorCode, $operator);

return $this;
}

public function createDataSource(iterable $data): DataSource
{
return new DataSource($data);
}

public function executeCombineConditions(CombineInterface|ConditionInterface $node, DataSource $dataSource): bool
public function executeCombineConditions(NodeInterface $node, DataSource $dataSource): NodeResult
{
return $this->conditionManager->execute($node, $dataSource);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Node/AbstractNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public function getParent(): ?CombineInterface
return $this->parent;
}

public function setParent(?CombineInterface $combine): void
public function setParent(?CombineInterface $combine): static
{
$this->parent = $combine;

return $this;
}

public function hasParent(): bool
Expand Down
22 changes: 15 additions & 7 deletions src/Node/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@ public function getChildren(): array
return $this->nodes;
}

public function setChildren(array $children): void
public function setChildren(array $children): static
{
$this->nodes = [];

foreach ($children as $child) {
$this->addChild($child);
}

return $this;
}

public function addChild(CombineInterface|ConditionInterface $condition): void
public function addChild(NodeInterface $node): static
{
if ($condition === $this) {
if ($node === $this) {
throw new LogicException('Child node cannot be the current instance of itself.');
}

$condition->setParent($this);
$this->nodes[] = $condition;
$node->setParent($this);
$this->nodes[] = $node;

return $this;
}

public function getCount(): int
Expand All @@ -66,18 +70,22 @@ public function isInvert(): bool
return $this->isInvert;
}

public function setIsInvert(bool $isInvert): void
public function setIsInvert(bool $isInvert): static
{
$this->isInvert = $isInvert;

return $this;
}

public function getOperator(): string
{
return $this->operator;
}

public function setOperator(string $operator): void
public function setOperator(string $operator): static
{
$this->operator = $operator;

return $this;
}
}
22 changes: 19 additions & 3 deletions src/Node/CombineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,44 @@ public function getChildren(): array;
* Set the logic structure as conditions or combinations
*
* @param NodeInterface[] $children
* @return self
*/
public function setChildren(array $children): void;
public function setChildren(array $children): static;

/**
* Add a logic structure as condition or combination
*
* @param NodeInterface $node
* @return self
*/
public function addChild(CombineInterface|ConditionInterface $node): void;
public function addChild(NodeInterface $node): static;

/**
* Retrieve the count of children nodes
*
* @return int
*/
public function getCount(): int;

/**
* Check if it has children
*
* @return bool
*/
public function hasChildren(): bool;

/**
* Check if the result should be inverted
*
* @return bool
*/
public function isInvert(): bool;

/**
* Set is result of combination inverted
*
* @param bool $isInvert
* @return self
*/
public function setIsInvert(bool $isInvert): void;
public function setIsInvert(bool $isInvert): static;
}
84 changes: 71 additions & 13 deletions src/Node/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,100 @@
final class Condition extends AbstractNode implements ConditionInterface
{
public function __construct(
private string $valueIdentifier,
private ?string $firstValueIdentifier,
private string $operator,
private mixed $valueCompare
private mixed $secondValue = null,
private mixed $firstValue = null,
private ?string $secondValueIdentifier = null,
) {
$this->setValueIdentifier($valueIdentifier);
$this->setFirstValueIdentifier($firstValueIdentifier);
$this->setFirstValue($firstValue);
$this->setOperator($operator);
$this->setValueCompare($valueCompare);
$this->setSecondValueIdentifier($secondValueIdentifier);
$this->setSecondValue($secondValue);
}

public function getOperator(): string
{
return $this->operator;
}

public function setOperator(string $operator): void
public function setOperator(string $operator): static
{
$this->operator = $operator;

return $this;
}

/**
* @return string|null
*/
public function getFirstValueIdentifier(): ?string
{
return $this->firstValueIdentifier;
}

/**
* @param string|null $identifier
* @return $this
*/
public function setFirstValueIdentifier(?string $identifier): static
{
$this->firstValueIdentifier = $identifier;
return $this;
}

/**
* @return mixed
*/
public function getFirstValue(): mixed
{
return $this->firstValue;
}

/**
* @param mixed $value
* @return $this
*/
public function setFirstValue(mixed $value): static
{
$this->firstValue = $value;
return $this;
}

public function getValueIdentifier(): string
/**
* @return string|null
*/
public function getSecondValueIdentifier(): ?string
{
return $this->valueIdentifier;
return $this->secondValueIdentifier;
}

public function setValueIdentifier(string $identifier): void
/**
* @param ?string $identifier
* @return $this
*/
public function setSecondValueIdentifier(?string $identifier): static
{
$this->valueIdentifier = $identifier;
$this->secondValueIdentifier = $identifier;
return $this;
}

public function getValueCompare(): mixed
/**
* @return mixed
*/
public function getSecondValue(): mixed
{
return $this->valueCompare;//ToDo: study to be fetched from datasource also?
return $this->secondValue;
}

public function setValueCompare(mixed $value): void
/**
* @param mixed $value
* @return $this
*/
public function setSecondValue(mixed $value): static
{
$this->valueCompare = $value;
$this->secondValue = $value;
return $this;
}
}
63 changes: 59 additions & 4 deletions src/Node/ConditionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,66 @@
*/
interface ConditionInterface extends NodeInterface
{
public function getValueIdentifier(): string;
/**
* Retrieve the first value key identifier to compare
*
* @return ?string
*/
public function getFirstValueIdentifier(): ?string;

public function setValueIdentifier(string $identifier): void;
/**
* Set the first value key identifier to compare
*
* @param ?string $identifier
* @return self
*/
public function setFirstValueIdentifier(?string $identifier): static;

/**
* Retrieve the first value to compare
*
* @return mixed
*/
public function getFirstValue(): mixed;

/**
* Set the first value to compare
*
* @param mixed $value
* @return self
*/
public function setFirstValue(mixed $value): static;


/**
* Retrieve the second value key identifier to compare
*
* @return ?string
*/
public function getSecondValueIdentifier(): ?string;

/**
* Set the second value key identifier to compare
*
* @param string $identifier
* @return self
*/
public function setSecondValueIdentifier(string $identifier): static;

/**
* Retrieve the second value to compare
*
* @return mixed
*/
public function getSecondValue(): mixed;

/**
* Set the second value to compare
*
* @param mixed $value
* @return self
*/
public function setSecondValue(mixed $value): static;

public function getValueCompare(): mixed;

public function setValueCompare(mixed $value): void;
}
Loading