diff --git a/src/QueryBuilderForm.php b/src/QueryBuilderForm.php index 0186a8d..0bd6423 100644 --- a/src/QueryBuilderForm.php +++ b/src/QueryBuilderForm.php @@ -89,7 +89,7 @@ class QueryBuilderForm extends Widget public $builder; /** - * @var string|array JSON rules representation into array format + * @var string|array JSON rules representation into array format */ public string|array $rules = ''; diff --git a/src/Rule.php b/src/Rule.php index 7d038c2..707c3b1 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -7,27 +7,31 @@ class Rule extends Component { - public string $condition; - public string $operator; - public string $field; - public mixed $value = null; public bool $valid; + public ?string $condition = null; + public string|int $id; + public string $field; public string $type; public string $input; + public string $operator; + public mixed $value = null; + - /** @var array */ + /** @var array $rules raw input rules that we will make to child objects */ public array $rules = []; /** @var self[] */ public array $children = []; - public function init() + public function init() : void { parent::init(); foreach ($this->rules as $ruleAttributes) { - $this->children[] = \Yii::createObject(array_merge([ + /** @var Rule $child */ + $child = \Yii::createObject(array_merge([ 'class' => self::class, ],$ruleAttributes)); + $this->children[] = $child; } } } \ No newline at end of file diff --git a/src/RuleHelper.php b/src/RuleHelper.php index ae03da6..48f6399 100644 --- a/src/RuleHelper.php +++ b/src/RuleHelper.php @@ -7,28 +7,22 @@ class RuleHelper /** * Adds a table prefix for any field in conditions if not existing - * @param array<'rules','condition','field'> $rules for Translator - * @param string $prefix - * @return array */ - public static function addPrefixToRules(array $rules, string $prefix) : array + public static function addPrefixToRules(Rule $rules, string $prefix) : Rule { - if (!array_key_exists('rules', $rules) || !is_array($rules['rules']) || count($rules['rules']) === 0 ) { + if(count($rules->children) === 0) { return $rules; } - $out = []; - foreach ($rules['rules'] as $key => $rule) { - if (isset($rule['condition'])) { - $out[$key] = static::addPrefixToRules($rule, $prefix); + foreach ($rules->children as $key => $child) { + if ($child->condition !== null) { + $rules->children[$key] = static::addPrefixToRules($child, $prefix); } else { - if(!str_contains($rule['field'], ".")) { - $rule['field'] = "$prefix.".$rule['field']; + if(!str_contains($child->field, ".")) { + $child->field = "$prefix.".$child->field; } - $out[$key] = $rule; } } - $rules['rules'] = $out; return $rules; } diff --git a/src/Translator.php b/src/Translator.php index e7b839e..e1ed9ed 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -57,11 +57,12 @@ public function __construct(private readonly array $data, ?string $paramPrefix = if(!is_null($paramPrefix)){ $this->paramPrefix = $paramPrefix; } + /** @var Rule $rules */ $rules = \Yii::createObject(array_merge([ 'class' => Rule::class, ],$this->data)); -// $this->_where = $this->buildWhere($this->data); - $this->_where = $this->buildWhereO($rules); + + $this->_where = $this->buildWhere($rules); } @@ -98,66 +99,27 @@ protected function encodeRule(string $field, string $type, array $params): strin return $field . " " . (is_null($replacement) ? $pattern : str_replace("?", $replacement, $pattern) ); } - /** - * @param array $data rules configuration - * @return string the WHERE clause - */ - protected function buildWhere(array $data) : string - { - if (!array_key_exists('rules', $data) || !is_array($data['rules']) || count($data['rules']) === 0 ) { - return ''; - } - - $where = []; - $condition = " " . $data['condition'] . " "; - - foreach ($data['rules'] as $rule) { - if (isset($rule['condition'])) { - $where[] = $this->buildWhere($rule); - } else { - $params = []; - $operator = $rule['operator']; - $field = $rule['field']; - $value = ArrayHelper::getValue($rule, 'value'); - - if ($value !== null) { - $i = count($this->_params); - if (!is_array($value)) { - $value = [$value]; - } - - foreach ($value as $v) { - $params[":{$this->paramPrefix}_$i"] = $v; - $i++; - } - } - $where[] = $this->encodeRule($field, $operator, $params); - } - } - return "(" . implode($condition, $where) . ")"; - } - /** * @return string the WHERE clause */ - protected function buildWhereO(Rule $inputRule) : string + protected function buildWhere(Rule $rule) : string { - if(count($inputRule->children) === 0) { + if(count($rule->children) === 0) { return ''; } $where = []; - $condition = " " . $inputRule->condition . " "; + $condition = " " . $rule->condition . " "; - foreach ($inputRule->children as $rule) { - if (isset($rule->condition)) { - $where[] = $this->buildWhereO($rule); + foreach ($rule->children as $child) { + if (isset($child->condition)) { + $where[] = $this->buildWhere($child); } else { $params = []; - $operator = $rule->operator; - $field = $rule->field; - $value = ArrayHelper::getValue($rule, 'value'); + $operator = $child->operator; + $field = $child->field; + $value = ArrayHelper::getValue($child, 'value'); if ($value !== null) { $i = count($this->_params);