Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • packages/doctrine-http-filter
1 result
Show changes
Commits on Source (16)
<?php
declare(strict_types=1);
namespace IQDEV\Packages\DoctrineHttpFilter\Exception;
use Exception;
class FilterParameterValueIsNullException extends Exception
{
public function __construct(string $key)
{
parent::__construct('Filter parameter value of key [' . $key . '] is null.');
}
}
......@@ -5,16 +5,24 @@ declare(strict_types=1);
namespace IQDEV\Packages\DoctrineHttpFilter\Filter;
use Doctrine\ORM\QueryBuilder;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
final class Date extends HttpFilter
{
/** @throws FilterParameterValueIsNullException */
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
$queryBuilder->where(
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') = \'' . $this->getHttpValue() . '\'',
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') = :' . $this->getParameterKey(),
);
if ($this->getHttpValue() === null) {
throw new FilterParameterValueIsNullException($this->field);
}
$queryBuilder->setParameter($this->getParameterKey(), $this->getHttpValue());
return $queryBuilder;
}
}
......@@ -5,16 +5,29 @@ declare(strict_types=1);
namespace IQDEV\Packages\DoctrineHttpFilter\Filter;
use Doctrine\ORM\QueryBuilder;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
final class DateRange extends HttpFilter
{
/** @throws FilterParameterValueIsNullException */
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
$queryBuilder->where(
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') >= \'' . $this->getHttpValue()['from'] . '\' AND ' .
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') <= \'' . $this->getHttpValue()['to'] . '\'',
);
if (! isset($this->getHttpValue()['from']) && ! isset($this->getHttpValue()['to'])) {
throw new FilterParameterValueIsNullException($this->field);
}
if (isset($this->getHttpValue()['from'])) {
$queryBuilder->where(
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') >= \'' . $this->getHttpValue()['from'] . '\'',
);
}
if (isset($this->getHttpValue()['to'])) {
$queryBuilder->andWhere(
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') <= \'' . $this->getHttpValue()['to'] . '\'',
);
}
return $queryBuilder;
}
......
......@@ -5,12 +5,18 @@ declare(strict_types=1);
namespace IQDEV\Packages\DoctrineHttpFilter\Filter;
use Doctrine\ORM\QueryBuilder;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
final class In extends HttpFilter
{
/** @throws FilterParameterValueIsNullException */
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
if ($this->getHttpValue() === null || ! is_array($this->getHttpValue())) {
throw new FilterParameterValueIsNullException($this->field);
}
$values = array_map(fn($value) => '\'' . $value . '\'', $this->getHttpValue());
$stringValues = implode(',', $values);
......
......@@ -5,16 +5,24 @@ declare(strict_types=1);
namespace IQDEV\Packages\DoctrineHttpFilter\Filter;
use Doctrine\ORM\QueryBuilder;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
final class Like extends HttpFilter
{
/** @throws FilterParameterValueIsNullException */
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
$queryBuilder->where(
$this->getColumn() . ' LIKE \'%' . $this->getHttpValue() . '%\'',
$this->getColumn() . ' LIKE :' . $this->getParameterKey(),
);
if ($this->getHttpValue() === null) {
throw new FilterParameterValueIsNullException($this->field);
}
$queryBuilder->setParameter($this->getParameterKey(), '%' . $this->getHttpValue() . '%');
return $queryBuilder;
}
}
......@@ -5,16 +5,30 @@ declare(strict_types=1);
namespace IQDEV\Packages\DoctrineHttpFilter\Filter;
use Doctrine\ORM\QueryBuilder;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
final class Range extends HttpFilter
{
/** @throws FilterParameterValueIsNullException */
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
$queryBuilder->where(
$this->getColumn() . '>= ' . $this->getHttpValue()['min'] . ' AND ' .
$this->getColumn() . ' <= ' . $this->getHttpValue()['max'],
);
if ((! isset($this->getHttpValue()['min']) || ! is_numeric($this->getHttpValue()['min']))
&& (! isset($this->getHttpValue()['max']) || ! is_numeric($this->getHttpValue()['max']))) {
throw new FilterParameterValueIsNullException($this->field);
}
if (isset($this->getHttpValue()['min'])) {
$queryBuilder->where(
$this->getColumn() . ' >= ' . $this->getHttpValue()['min'],
);
}
if (isset($this->getHttpValue()['max'])) {
$queryBuilder->andWhere(
$this->getColumn() . ' <= ' . $this->getHttpValue()['max'],
);
}
return $queryBuilder;
}
......
......@@ -5,16 +5,22 @@ declare(strict_types=1);
namespace IQDEV\Packages\DoctrineHttpFilter\Filter;
use Doctrine\ORM\QueryBuilder;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
final class Where extends HttpFilter
{
/** @throws FilterParameterValueIsNullException */
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
$queryBuilder->where(
$this->getColumn() . ' = :' . $this->getParameterKey(),
);
if ($this->getHttpValue() === null) {
throw new FilterParameterValueIsNullException($this->field);
}
$queryBuilder->setParameter($this->getParameterKey(), $this->getHttpValue());
return $queryBuilder;
......
......@@ -15,7 +15,7 @@ abstract class HttpFilter
public function __construct(
protected string $tableAlias,
protected string $filed,
protected string $field,
?Request $request = null,
) {
$this->request = $request ?? Request::createFromGlobals();
......@@ -23,7 +23,7 @@ abstract class HttpFilter
protected function getColumn(): string
{
return $this->tableAlias . '.' . $this->filed;
return $this->tableAlias . '.' . $this->field;
}
protected function getHttpValue(): mixed
......@@ -37,7 +37,7 @@ abstract class HttpFilter
return null;
}
return $filter[$this->filed] ?? null;
return $filter[$this->field] ?? null;
}
public function getParameterKey(): string
......
......@@ -6,6 +6,7 @@ namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use DateInterval;
use DateTimeImmutable;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\Filter\DateRange;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
......@@ -175,6 +176,7 @@ class FilterByDateRangeTest extends TestCase
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => $firstDate->add(new DateInterval('P1Y'))->format('Y-m-d'),
'to' => $secondDate->add(new DateInterval('P1Y'))->format('Y-m-d'),
],
],
]))
......@@ -183,4 +185,161 @@ class FilterByDateRangeTest extends TestCase
$this->assertEmpty($result);
}
public function testFilterDateRangeWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterDateRangeWithNotDateParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => 'not date',
],
]))
->getQuery()
->getResult();
}
public function testFilterDateRangeWithNotDateParameterValues(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => 'not date',
'to' => 'not date',
],
],
]))
->getQuery()
->getResult();
$this->assertEmpty($result);
}
public function testFilterDateRangeWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'notCreatedAt' => 'Not createdAt value',
],
]))
->getQuery()
->getResult();
}
public function testFilterDateRangeWithOnlyFromParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 13:30:00');
$secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 12:00:00');
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$firstDate,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'from' => $secondDate->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertGreaterThanOrEqual(1, $result);
}
public function testFilterDateRangeWithOnlyToParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-01 13:30:00');
$secondDate = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-05 12:00:00');
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$firstDate,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$secondDate,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => [
'to' => $firstDate->format('Y-m-d'),
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertGreaterThanOrEqual(1, $result);
}
}
......@@ -6,6 +6,7 @@ namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use DateInterval;
use DateTimeImmutable;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\Filter\Date;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
......@@ -131,4 +132,56 @@ class FilterByDateTest extends TestCase
$this->assertEmpty($result);
}
public function testFilterDateWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterDateWithNotDateParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => 'not date',
],
]))
->getQuery()
->getResult();
$this->assertEmpty($result);
}
public function testFilterDateWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'notCreatedAt' => 'Not createdAt value',
],
]))
->getQuery()
->getResult();
}
}
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\Filter\In;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
......@@ -126,4 +127,56 @@ class FilterByInTest extends TestCase
$this->assertEmpty($result);
}
public function testFilterInWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'title' => In::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterInWithNotArrayParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'title' => In::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'title' => 'not array',
],
]))
->getQuery()
->getResult();
}
public function testFilterInWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'title' => In::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'notTitle' => 'Not title value',
],
]))
->getQuery()
->getResult();
}
}
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\Filter\Like;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
......@@ -126,4 +127,38 @@ class FilterByLikeTest extends TestCase
$this->assertEmpty($result);
}
public function testFilterLikeWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'title' => Like::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterDateWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'title' => Like::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'notTitle' => 'Not title value',
],
]))
->getQuery()
->getResult();
}
}
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use DateTimeImmutable;
use IQDEV\Packages\DoctrineHttpFilter\Filter\DateRange;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\Filter\Range;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
......@@ -176,11 +176,12 @@ class FilterByRangeTest extends TestCase
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 100,
'max' => 200,
],
],
]))
......@@ -189,4 +190,165 @@ class FilterByRangeTest extends TestCase
$this->assertEmpty($result);
}
public function testFilterRangeWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterRangeWithNotNumberParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => 'not number',
],
]))
->getQuery()
->getResult();
}
public function testFilterRangeWithNotNumberParameterValues(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 'not number',
'max' => 'not number',
],
],
]))
->getQuery()
->getResult();
}
public function testFilterRangeWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'notCountOfViews' => 'Not countOfViews value',
],
]))
->getQuery()
->getResult();
}
public function testFilterRangeWithOnlyMinParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstCountOfViews = 25;
$secondCountOfViews = 35;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $firstCountOfViews,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $secondCountOfViews,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'min' => 20,
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
}
public function testFilterRangeWithOnlyMaxParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$firstCountOfViews = 25;
$secondCountOfViews = 35;
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $firstCountOfViews,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
countOfViews: $secondCountOfViews,
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'countOfViews' => Range::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'countOfViews' => [
'max' => 40,
],
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
}
}
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace IQDEV\Tests\Packages\DoctrineHttpFilter;
use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException;
use IQDEV\Packages\DoctrineHttpFilter\Filter\Where;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilter;
use IQDEV\Tests\Packages\DoctrineHttpFilter\Entity\Post;
......@@ -110,4 +111,38 @@ class FilterByWhereTest extends TestCase
$this->assertEmpty($result);
}
public function testFilterWhereWithoutParameterValue(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'title' => Where::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [],
]))
->getQuery()
->getResult();
}
public function testFilterWhereWithDifferentKey(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'title' => Where::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'notTitle' => 'Not title value',
],
]))
->getQuery()
->getResult();
}
}