diff --git a/src/Model/Scope.php b/src/Model/Scope.php index 42c28f68c..38ee2c688 100644 --- a/src/Model/Scope.php +++ b/src/Model/Scope.php @@ -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; } @@ -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 */