diff --git a/config/asseco-json-query-builder.php b/config/asseco-json-query-builder.php index c0a6ff0..06c2343 100644 --- a/config/asseco-json-query-builder.php +++ b/config/asseco-json-query-builder.php @@ -11,6 +11,8 @@ use Asseco\JsonQueryBuilder\RequestParameters\SearchParameter; use Asseco\JsonQueryBuilder\RequestParameters\SoftDeletedParameter; use Asseco\JsonQueryBuilder\SearchCallbacks\Between; +use Asseco\JsonQueryBuilder\SearchCallbacks\Contains; +use Asseco\JsonQueryBuilder\SearchCallbacks\EndsWith; use Asseco\JsonQueryBuilder\SearchCallbacks\Equals; use Asseco\JsonQueryBuilder\SearchCallbacks\GreaterThan; use Asseco\JsonQueryBuilder\SearchCallbacks\GreaterThanOrEqual; @@ -18,6 +20,7 @@ use Asseco\JsonQueryBuilder\SearchCallbacks\LessThanOrEqual; use Asseco\JsonQueryBuilder\SearchCallbacks\NotBetween; use Asseco\JsonQueryBuilder\SearchCallbacks\NotEquals; +use Asseco\JsonQueryBuilder\SearchCallbacks\StartsWith; use Asseco\JsonQueryBuilder\Types\BooleanType; use Asseco\JsonQueryBuilder\Types\GenericType; @@ -51,6 +54,9 @@ Equals::class, LessThan::class, GreaterThan::class, + Contains::class, + StartsWith::class, + EndsWith::class, ], /** diff --git a/src/SearchCallbacks/AbstractCallback.php b/src/SearchCallbacks/AbstractCallback.php index 427f6f4..9713237 100644 --- a/src/SearchCallbacks/AbstractCallback.php +++ b/src/SearchCallbacks/AbstractCallback.php @@ -138,6 +138,66 @@ protected function betweenCallback(Builder $builder, string $column, Categorized $builder->{$callback}($column, [$values->and[0], $values->and[1]]); } + /** + * @param Builder $builder + * @param string $column + * @param CategorizedValues $values + * @param string $operator + * + * @throws JsonQueryBuilderException + */ + protected function containsCallback(Builder $builder, string $column, CategorizedValues $values, string $operator) + { + if ($values->andLike) { + $builder->where($column, $this->getLikeOperator(), '%' . $values->andLike[0] . '%'); + } + if ($values->and) { + foreach ($values->and as $andValue) { + $builder->orWhere($column, $this->getLikeOperator(), '%' . $andValue . '%'); + } + } + } + + /** + * @param Builder $builder + * @param string $column + * @param CategorizedValues $values + * @param string $operator + * + * @throws JsonQueryBuilderException + */ + protected function endsWithCallback(Builder $builder, string $column, CategorizedValues $values, string $operator) + { + if ($values->andLike) { + $builder->where($column, $this->getLikeOperator(), '%' . $values->andLike[0]); + } + if ($values->and) { + foreach ($values->and as $andValue) { + $builder->orWhere($column, $this->getLikeOperator(), '%' . $andValue); + } + } + } + + /** + * @param Builder $builder + * @param string $column + * @param CategorizedValues $values + * @param string $operator + * + * @throws JsonQueryBuilderException + */ + protected function startsWithCallback(Builder $builder, string $column, CategorizedValues $values, string $operator) + { + if ($values->andLike) { + $builder->where($column, $this->getLikeOperator(), $values->andLike[0] . '%'); + } + if ($values->and) { + foreach ($values->and as $andValue) { + $builder->orWhere($column, $this->getLikeOperator(), $andValue . '%'); + } + } + } + /** * Should throw exception if anything except '$values->and' is filled out. * diff --git a/src/SearchCallbacks/Contains.php b/src/SearchCallbacks/Contains.php new file mode 100644 index 0000000..803ed84 --- /dev/null +++ b/src/SearchCallbacks/Contains.php @@ -0,0 +1,30 @@ +containsCallback($builder, $column, $values, 'contains'); + } +} diff --git a/src/SearchCallbacks/EndsWith.php b/src/SearchCallbacks/EndsWith.php new file mode 100644 index 0000000..f742b2b --- /dev/null +++ b/src/SearchCallbacks/EndsWith.php @@ -0,0 +1,30 @@ +endsWithCallback($builder, $column, $values, 'ends_with'); + } +} diff --git a/src/SearchCallbacks/StartsWith.php b/src/SearchCallbacks/StartsWith.php new file mode 100644 index 0000000..10994a1 --- /dev/null +++ b/src/SearchCallbacks/StartsWith.php @@ -0,0 +1,30 @@ +startsWithCallback($builder, $column, $values, 'starts_with'); + } +} diff --git a/tests/Unit/Config/OperatorsConfigTest.php b/tests/Unit/Config/OperatorsConfigTest.php index 31967e1..440c6a3 100644 --- a/tests/Unit/Config/OperatorsConfigTest.php +++ b/tests/Unit/Config/OperatorsConfigTest.php @@ -34,7 +34,7 @@ public function returns_registered_operators() { $operatorsConfig = new OperatorsConfig(); - $expected = ['!<>', '<=', '>=', '<>', '!=', '=', '<', '>']; + $expected = ['!<>', '<=', '>=', '<>', '!=', '=', '<', '>', 'contains', 'starts_with', 'ends_with']; $this->assertEquals($expected, $operatorsConfig->getOperators()); }