|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright Humbly Arrogant Ltd 2020-2022. |
|
7
|
|
|
* |
|
8
|
|
|
* Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license. |
|
9
|
|
|
* |
|
10
|
|
|
* Change Date: TBD ( 3 years after 2.0.0 release ) |
|
11
|
|
|
* |
|
12
|
|
|
* On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Parthenon\Athena\Filters; |
|
16
|
|
|
|
|
17
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
|
18
|
|
|
use Doctrine\ORM\Query; |
|
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
20
|
|
|
use MongoDB\BSON\Regex; |
|
21
|
|
|
|
|
22
|
|
|
final class ContainsFilter implements DoctrineFilterInterface, OdmFilterInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use QueryBuilderTrait; |
|
25
|
|
|
|
|
26
|
|
|
public const NAME = 'contains'; |
|
27
|
|
|
|
|
28
|
|
|
protected string $fieldName; |
|
29
|
|
|
|
|
30
|
|
|
private $data; |
|
31
|
|
|
|
|
32
|
|
|
public function getName(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return self::NAME; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function setData($data): FilterInterface |
|
38
|
|
|
{ |
|
39
|
|
|
$this->data = $data; |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function hasData(): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return isset($this->data) && !empty($this->data); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function setFieldName(string $fieldName): FilterInterface |
|
50
|
|
|
{ |
|
51
|
|
|
$this->fieldName = $fieldName; |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function modifyQueryBuilder(QueryBuilder $queryBuilder) |
|
57
|
|
|
{ |
|
58
|
|
|
[$alias, $fieldName] = $this->readyQueryBuilderForAliasAndFieldName($queryBuilder); |
|
59
|
|
|
$queryBuilder->andWhere($alias.'.'.$fieldName.' LIKE :'.$this->getSafeFieldName()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function modifyQuery(Query $query) |
|
63
|
|
|
{ |
|
64
|
|
|
$query->setParameter(':'.$this->getSafeFieldName(), '%'.$this->data.'%'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getData() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->data; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getFieldName(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->fieldName; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getHeaderName(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return ucwords(str_replace('_', ' ', $this->fieldName)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function modifiyOdmQueryBuilder(Builder $builder): Builder |
|
83
|
|
|
{ |
|
84
|
|
|
$builder->field($this->fieldName)->equals(new Regex($this->data, 'i')); |
|
85
|
|
|
|
|
86
|
|
|
return $builder; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|