Skip to content
Closed
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
9 changes: 9 additions & 0 deletions src/Model/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public function addCondition($field, $operator = null, $value = null)
return $this;
}

protected function setSystem(bool $system = true)
{
foreach ($this->elements as $nestedCondition) {
$nestedCondition->setSystem($system && $this->isAnd());
}

return $this;
}

/**
* Return array of nested conditions.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Model/Scope/AbstractScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ protected function init(): void

$this->_init();

// always set system flag if condition added to another condition
$this->setSystem($owner instanceof RootScope);

$this->onChangeModel();
}

abstract protected function onChangeModel(): void;

/**
* @return $this
*/
abstract protected function setSystem(bool $system = true);

/**
* Get the model this condition is associated with.
*/
Expand Down
24 changes: 20 additions & 4 deletions src/Model/Scope/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Condition extends AbstractScope
/** @var mixed */
public $value;

protected bool $system = false;

public const OPERATOR_EQUALS = '=';
public const OPERATOR_DOESNOT_EQUAL = '!=';
public const OPERATOR_GREATER = '>';
Expand Down Expand Up @@ -149,17 +151,21 @@ public function __construct($key, $operator = null, $value = null)
}
}

protected function setSystem(bool $system = true)
{
$this->system = $system;

return $this;
}

protected function onChangeModel(): void
{
$model = $this->getModel();
if ($model !== null) {
// if we have a definitive equal condition set the value as default value for field
// new records will automatically get this value assigned for the field
// TODO: fix when condition is part of OR scope
if ($this->operator === self::OPERATOR_EQUALS && !is_array($this->value)
&& !$this->value instanceof Expressionable
&& !$this->value instanceof Persistence\Array_\Action // needed to pass hintable tests
) {
if ($this->system && $this->isDefiniteValue()) {
// key containing '/' means chained references and it is handled in toQueryArguments method
$field = $this->key;
if (is_string($field) && !str_contains($field, '/')) {
Expand Down Expand Up @@ -262,6 +268,16 @@ public function isEmpty(): bool
return array_filter([$this->key, $this->operator, $this->value]) ? false : true;
}

/**
* Checks if condition sets a definitive scalar value for a field.
*/
protected function isDefiniteValue(): bool
{
return $this->operator === self::OPERATOR_EQUALS && !is_array($this->value)
&& !$this->value instanceof Expressionable
&& !$this->value instanceof Persistence\Array_\Action; // needed to pass hintable tests
}

public function clear()
{
$this->key = null; // @phpstan-ignore-line
Expand Down