/
FieldFilterRegistry.php
66 lines (56 loc) · 1.58 KB
/
FieldFilterRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace SilverStripe\GraphQL\Schema\DataObject\Plugin\QueryFilter;
use InvalidArgumentException;
class FieldFilterRegistry implements FilterRegistryInterface
{
/**
* @var array FieldFilterInterface[]
*/
protected array $filters = [];
/**
* FilterRegistry constructor.
* @param mixed ...$filters
*/
public function __construct(...$filters)
{
foreach ($filters as $filter) {
$this->addFilter($filter);
}
}
/**
* @param FieldFilterInterface $filter
* @param string|null $identifier
* @return $this
* @throws InvalidArgumentException
*/
public function addFilter(FieldFilterInterface $filter, ?string $identifier = null): self
{
$id = $identifier ?: $filter->getIdentifier();
if (!preg_match('/^[A-Za-z0-9_]+$/', $id ?? '')) {
throw new InvalidArgumentException(sprintf(
'Filter %s has an invalid identifier. Only alphanumeric characters and underscores allowed.',
get_class($filter)
));
}
$this->filters[$id] = $filter;
return $this;
}
/**
* @param string $identifier
* @return FieldFilterInterface|null
*/
public function getFilterByIdentifier(string $identifier): ?FieldFilterInterface
{
if (isset($this->filters[$identifier])) {
return $this->filters[$identifier];
}
return null;
}
/**
* @return FieldFilterInterface[]
*/
public function getAll(): array
{
return $this->filters;
}
}