Skip to content

Commit

Permalink
test Rule as object WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson committed Oct 12, 2024
1 parent 24975d0 commit e636e64
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/QueryBuilderForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class QueryBuilderForm extends Widget
public $builder;

/**
* @var string|array JSON rules representation into array format
* @var string|array<string, mixed> JSON rules representation into array format
*/
public string|array $rules = '';

Expand Down
18 changes: 11 additions & 7 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> */
/** @var array<mixed> $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;
}
}
}
20 changes: 7 additions & 13 deletions src/RuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
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;

}
Expand Down
62 changes: 12 additions & 50 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down Expand Up @@ -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<mixed> $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);
Expand Down

0 comments on commit e636e64

Please sign in to comment.