Skip to content

Commit

Permalink
[update] introduce and use Scope::create
Browse files Browse the repository at this point in the history
  • Loading branch information
georgehristov committed Aug 8, 2020
1 parent cea6821 commit 030b5e0
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/Model/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,11 @@ public function __clone()
*/
public function addCondition($field, $operator = null, $value = null)
{
if (func_num_args() === 1 && $field instanceof Scope\AbstractScope) {
$condition = $field;
} elseif (func_num_args() === 1 && is_array($field)) {
$condition = static::createAnd(func_get_args());
} else {
$condition = new Scope\Condition(...func_get_args());
}
$this->add(static::create(...func_get_args()));

return $this;
}

$this->add($condition);

return $this;
}
Expand Down Expand Up @@ -188,6 +184,28 @@ public function toWords(Model $model = null): string
return implode($glue, $parts);
}

/**
* Creates the simplest scope object based on arguments.
*
* @param Scope\AbstractScope|string|array|\atk4\data\Field $field
* @param string $operator
* @param mixed $value
*
* @return Scope\AbstractScope
*/
public static function create($field, $operator = null, $value = null)
{
if (func_num_args() === 1 && $field instanceof Scope\AbstractScope) {
$scope = $field;
} elseif (func_num_args() === 1 && is_array($field)) {
$scope = static::createAnd(func_get_args());
} else {
$scope = new Scope\Condition(...func_get_args());
}

return $scope;
}

/**
* @return static
*/
Expand Down

0 comments on commit 030b5e0

Please sign in to comment.