Skip to content

Commit

Permalink
Remove Translator extends dependency, added Translator $paramPrefix p…
Browse files Browse the repository at this point in the history
…roperty instead of randomly generated param names (testability), enabled rules tests, added merged rules tests
  • Loading branch information
TonisOrmisson committed Oct 12, 2024
1 parent 686b28f commit fd69fc8
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
operating-system: ['ubuntu-latest']
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
php-versions: ['8.0', '8.1', '8.2', '8.3']

steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
],
"require": {
"php" : ">=8.0",
"yiisoft/yii2": "~2.0.0",
"solutosoft/yii2-plugin": "~1.0.0"
},
Expand Down
24 changes: 13 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="QueryBilder Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
colors="true"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerDeprecations="true"
>
<testsuites>
<testsuite name="QueryBuilder Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
87 changes: 42 additions & 45 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace leandrogehlen\querybuilder;

use yii\base\BaseObject;
use yii\base\Security;
use yii\helpers\ArrayHelper;

/**
Expand Down Expand Up @@ -34,7 +32,7 @@
* ```
* @author Leandro Gehlen <[email protected]>
*/
class Translator extends BaseObject
class Translator
{
private string $_where;

Expand All @@ -44,46 +42,23 @@ class Translator extends BaseObject
/** @var array<string, mixed> */
private array $_operators;

/** @var string $paramPrefix prefix added to parameters, to be changed in case of multiple translator params being merged */
private string $paramPrefix = "p";


/**
* Constructors.
* @param array<mixed> $data Rules configuraion
* @param array<string, mixed> $config the configuration array to be applied to this object.
* @param ?string $paramPrefix prefix added to parameters, to be changed in case of multiple translator params being merged
*/
public function __construct($data, $config = [])
public function __construct(private array $data, ?string $paramPrefix = null)
{
parent::__construct($config);
$this->_where = $this->buildWhere($data);
}
$this->init();
if($paramPrefix){
$this->paramPrefix = $paramPrefix;
}
$this->_where = $this->buildWhere($this->data);

/**
* @inheritdoc
*/
public function init() : void
{
$this->_operators = [
'equal' => '= ?',
'not_equal' => '<> ?',
'in' => ['op' => 'IN (?)', 'list' => true, 'sep' => ', ' ],
'not_in' => ['op' => 'NOT IN (?)', 'list' => true, 'sep' => ', '],
'less' => '< ?',
'less_or_equal' => '<= ?',
'greater' => '> ?',
'greater_or_equal' => '>= ?',
'between' => ['op' => 'BETWEEN ?', 'list' => true, 'sep' => ' AND '],
'not_between' => ['op' => 'NOT BETWEEN ?', 'list' => true, 'sep' => ' AND '],
'begins_with' => ['op' => 'LIKE ?', 'fn' => function($value){ return "$value%"; } ],
'not_begins_with' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "$value%"; } ],
'contains' => ['op' => 'LIKE ?', 'fn' => function($value){ return "%$value%"; } ],
'not_contains' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "%$value%"; } ],
'ends_with' => ['op' => 'LIKE ?', 'fn' => function($value){ return "%$value"; } ],
'not_ends_with' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "%$value"; } ],
'is_empty' => '= ""',
'is_not_empty' => '<> ""',
'is_null' => 'IS NULL',
'is_not_null' => 'IS NOT NULL'
];
}


Expand Down Expand Up @@ -142,13 +117,14 @@ protected function buildWhere($data)
$value = ArrayHelper::getValue($rule, 'value');

if ($value !== null) {

$i = count($this->_params);
if (!is_array($value)) {
$value = [$value];
}

foreach ($value as $v) {
$params[":".$this->getNewParamName()] = $v;
$params[":{$this->paramPrefix}_$i"] = $v;
$i++;
}
}
$where[] = $this->encodeRule($field, $operator, $params);
Expand All @@ -174,15 +150,36 @@ public function params()
{
return $this->_params;
}




/**
* Get a param name that should not conflict with any params already set
* @return string
* @inheritdoc
*/
private function getNewParamName(){
/** @var Security $security */
$security = \Yii::createObject(Security::class);
return $security->generateRandomString(20);
private function init() : void
{
$this->_operators = [
'equal' => '= ?',
'not_equal' => '<> ?',
'in' => ['op' => 'IN (?)', 'list' => true, 'sep' => ', ' ],
'not_in' => ['op' => 'NOT IN (?)', 'list' => true, 'sep' => ', '],
'less' => '< ?',
'less_or_equal' => '<= ?',
'greater' => '> ?',
'greater_or_equal' => '>= ?',
'between' => ['op' => 'BETWEEN ?', 'list' => true, 'sep' => ' AND '],
'not_between' => ['op' => 'NOT BETWEEN ?', 'list' => true, 'sep' => ' AND '],
'begins_with' => ['op' => 'LIKE ?', 'fn' => function($value){ return "$value%"; } ],
'not_begins_with' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "$value%"; } ],
'contains' => ['op' => 'LIKE ?', 'fn' => function($value){ return "%$value%"; } ],
'not_contains' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "%$value%"; } ],
'ends_with' => ['op' => 'LIKE ?', 'fn' => function($value){ return "%$value"; } ],
'not_ends_with' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "%$value"; } ],
'is_empty' => '= ""',
'is_not_empty' => '<> ""',
'is_null' => 'IS NULL',
'is_not_null' => 'IS NOT NULL',
];
}

}
}
Loading

0 comments on commit fd69fc8

Please sign in to comment.