Skip to content

Commit

Permalink
feat: and, or, not filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Jan 9, 2024
1 parent 8c29ad8 commit 3bb7e17
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 10 deletions.
31 changes: 31 additions & 0 deletions src/Dto/Search/Query/Filter/AndFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Filter;

use Atoolo\Search\Dto\Search\Query\Filter\Filter;

class AndFilter extends Filter
{
/**
* @param Filter[] $filter
*/
public function __construct(
?string $key,
private readonly array $filter,
array $tags = []
) {
parent::__construct($key, $tags);
}

public function getQuery(): string
{
$query = [];
foreach ($this->filter as $filter) {
$query[] = $filter->getQuery();
}

return '(' . implode(' AND ', $query) . ')';
}
}
8 changes: 6 additions & 2 deletions src/Dto/Search/Query/Filter/ArchiveFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class ArchiveFilter extends Filter
public function __construct()
{
parent::__construct(
'archive',
'-sp_archive:true'
'archive'
);
}

public function getQuery(): string
{
return '-sp_archive:true';
}
}
3 changes: 1 addition & 2 deletions src/Dto/Search/Query/Filter/FieldFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ public function __construct(
$this->values = $values;
parent::__construct(
$key,
$this->toQuery(),
[$key]
);
}

/**
* @param string[] $values
*/
private function toQuery(): string
public function getQuery(): string
{
$filterValue = count($this->values) === 1
? $this->values[0]
Expand Down
8 changes: 2 additions & 6 deletions src/Dto/Search/Query/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Atoolo\Search\Dto\Search\Query\Filter;

class Filter
abstract class Filter
{
/**
* @param string[] $tags
*/
public function __construct(
private readonly ?string $key,
private readonly string $query,
private readonly array $tags = []
) {
}
Expand All @@ -21,10 +20,7 @@ public function getKey(): ?string
return $this->key;
}

public function getQuery(): string
{
return $this->query;
}
abstract public function getQuery(): string;

/**
* @return string[]
Expand Down
21 changes: 21 additions & 0 deletions src/Dto/Search/Query/Filter/NotFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Filter;

class NotFilter extends Filter
{
public function __construct(
?string $key,
private readonly Filter $filter,
array $tags = []
) {
parent::__construct($key, $tags);
}

public function getQuery(): string
{
return 'NOT ' . $this->filter->getQuery();
}
}
29 changes: 29 additions & 0 deletions src/Dto/Search/Query/Filter/OrFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Filter;

class OrFilter extends Filter
{
/**
* @param Filter[] $filter
*/
public function __construct(
?string $key,
private readonly array $filter,
array $tags = []
) {
parent::__construct($key, $tags);
}

public function getQuery(): string
{
$query = [];
foreach ($this->filter as $filter) {
$query[] = $filter->getQuery();
}

return '(' . implode(' OR ', $query) . ')';
}
}

0 comments on commit 3bb7e17

Please sign in to comment.