Skip to content

Commit

Permalink
Implemented contains,starts/ends_with in searchCallbacks instead of i… (
Browse files Browse the repository at this point in the history
#53)

* Implemented contains,starts/ends_with in searchCallbacks instead of in getSpecialOperators()

* Apply fixes from StyleCI

* Pipeline fix

* Small fix

* Apply fixes from StyleCI

---------

Co-authored-by: David <david.katalinic@asseco-see.hr>
Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent 531b333 commit fd8f5c3
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/asseco-json-query-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
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;
use Asseco\JsonQueryBuilder\SearchCallbacks\LessThan;
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;

Expand Down Expand Up @@ -51,6 +54,9 @@
Equals::class,
LessThan::class,
GreaterThan::class,
Contains::class,
StartsWith::class,
EndsWith::class,
],

/**
Expand Down
60 changes: 60 additions & 0 deletions src/SearchCallbacks/AbstractCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
30 changes: 30 additions & 0 deletions src/SearchCallbacks/Contains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Asseco\JsonQueryBuilder\SearchCallbacks;

use Asseco\JsonQueryBuilder\CategorizedValues;
use Exception;
use Illuminate\Database\Eloquent\Builder;

class Contains extends AbstractCallback
{
public static function operator(): string
{
return 'contains';
}

/**
* @param Builder $builder
* @param string $column
* @param CategorizedValues $values
* @return void
*
* @throws Exception
*/
public function execute(Builder $builder, string $column, CategorizedValues $values): void
{
$this->containsCallback($builder, $column, $values, 'contains');
}
}
30 changes: 30 additions & 0 deletions src/SearchCallbacks/EndsWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Asseco\JsonQueryBuilder\SearchCallbacks;

use Asseco\JsonQueryBuilder\CategorizedValues;
use Exception;
use Illuminate\Database\Eloquent\Builder;

class EndsWith extends AbstractCallback
{
public static function operator(): string
{
return 'ends_with';
}

/**
* @param Builder $builder
* @param string $column
* @param CategorizedValues $values
* @return void
*
* @throws Exception
*/
public function execute(Builder $builder, string $column, CategorizedValues $values): void
{
$this->endsWithCallback($builder, $column, $values, 'ends_with');
}
}
30 changes: 30 additions & 0 deletions src/SearchCallbacks/StartsWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Asseco\JsonQueryBuilder\SearchCallbacks;

use Asseco\JsonQueryBuilder\CategorizedValues;
use Exception;
use Illuminate\Database\Eloquent\Builder;

class StartsWith extends AbstractCallback
{
public static function operator(): string
{
return 'starts_with';
}

/**
* @param Builder $builder
* @param string $column
* @param CategorizedValues $values
* @return void
*
* @throws Exception
*/
public function execute(Builder $builder, string $column, CategorizedValues $values): void
{
$this->startsWithCallback($builder, $column, $values, 'starts_with');
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Config/OperatorsConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function returns_registered_operators()
{
$operatorsConfig = new OperatorsConfig();

$expected = ['!<>', '<=', '>=', '<>', '!=', '=', '<', '>'];
$expected = ['!<>', '<=', '>=', '<>', '!=', '=', '<', '>', 'contains', 'starts_with', 'ends_with'];

$this->assertEquals($expected, $operatorsConfig->getOperators());
}
Expand Down

0 comments on commit fd8f5c3

Please sign in to comment.