Skip to content

Commit

Permalink
[fix] introduce AbstractScope::setSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
georgehristov committed Aug 8, 2020
1 parent e9e6dfb commit 2940903
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Model/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public function addCondition($field, $operator = null, $value = null)
return $this;
}

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

return $this;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Model/Scope/AbstractScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ public function init(): void

$this->_init();

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

$this->onChangeModel();
}

abstract protected function onChangeModel();

abstract protected function setSystem($system = true);

/**
* Get the model this condition is associated with.
*/
Expand Down
19 changes: 18 additions & 1 deletion src/Model/Scope/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Condition extends AbstractScope
*/
public $value;

protected $system = false;

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

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

return $this;
}

protected function onChangeModel(): void
{
if ($model = $this->getModel()) {
// if we have a definitive scalar value for a field
// sets it as default value for field and locks it
// new records will automatically get this value assigned for the field
// @todo: consider this when condition is part of OR scope
if ($this->operator === self::OPERATOR_EQUALS && !is_object($this->value) && !is_array($this->value)) {
if ($this->system && $this->setsDefiniteValue()) {
// key containing '/' means chained references and it is handled in toQueryArguments method
if (is_string($field = $this->key) && !$this->isTraversing()) {
$field = $model->getField($field);
Expand Down Expand Up @@ -229,6 +238,14 @@ 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 setsDefiniteValue(): bool
{
return $this->operator === self::OPERATOR_EQUALS && !is_object($this->value) && !is_array($this->value);
}

public function clear()
{
$this->key = $this->operator = $this->value = null;
Expand Down

0 comments on commit 2940903

Please sign in to comment.