forked from leandrogehlen/yii2-querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Translator extends dependency, added Translator $paramPrefix p…
…roperty instead of randomly generated param names (testability), enabled rules tests, added merged rules tests
- Loading branch information
1 parent
686b28f
commit fd69fc8
Showing
5 changed files
with
170 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
} | ||
], | ||
"require": { | ||
"php" : ">=8.0", | ||
"yiisoft/yii2": "~2.0.0", | ||
"solutosoft/yii2-plugin": "~1.0.0" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
|
||
namespace leandrogehlen\querybuilder; | ||
|
||
use yii\base\BaseObject; | ||
use yii\base\Security; | ||
use yii\helpers\ArrayHelper; | ||
|
||
/** | ||
|
@@ -34,7 +32,7 @@ | |
* ``` | ||
* @author Leandro Gehlen <[email protected]> | ||
*/ | ||
class Translator extends BaseObject | ||
class Translator | ||
{ | ||
private string $_where; | ||
|
||
|
@@ -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' | ||
]; | ||
} | ||
|
||
|
||
|
@@ -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); | ||
|
@@ -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', | ||
]; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.