Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented contains,starts/ends_with in searchCallbacks instead of i… #53

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading