These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * To change this license header, choose License Headers in Project Properties. |
||
5 | * To change this template file, choose Tools | Templates |
||
6 | * and open the template in the editor. |
||
7 | */ |
||
8 | |||
9 | namespace Maslosoft\Manganel\Decorators\QueryBuilder; |
||
10 | |||
11 | use Maslosoft\Mangan\Criteria\ConditionDecorator; |
||
0 ignored issues
–
show
|
|||
12 | use Maslosoft\Mangan\Meta\ManganMeta; |
||
13 | use Maslosoft\Manganel\Interfaces\ManganelAwareInterface; |
||
14 | use Maslosoft\Manganel\Interfaces\QueryBuilder\ConditionDecoratorInterface; |
||
15 | use Maslosoft\Manganel\SearchCriteria; |
||
16 | use Maslosoft\Manganel\Traits\ManganelAwareTrait; |
||
17 | |||
18 | /** |
||
19 | * TagDecorator |
||
20 | * |
||
21 | * NOTE: This decorator must be at beginning, as it modifies criteria! |
||
22 | * |
||
23 | * @author Piotr Maselkowski <pmaselkowski at gmail.com> |
||
24 | */ |
||
25 | class TagDecorator implements ConditionDecoratorInterface, ManganelAwareInterface |
||
26 | { |
||
27 | |||
28 | use ManganelAwareTrait; |
||
29 | |||
30 | /** |
||
31 | * Field which should be used as a tag filter |
||
32 | * @var string |
||
33 | */ |
||
34 | 47 | public $field = ''; |
|
35 | |||
36 | 47 | public function decorate(&$conditions, SearchCriteria $criteria) |
|
37 | 47 | { |
|
38 | 47 | assert(!empty($this->field), sprintf('Property `field` is required for `%s`', __CLASS__)); |
|
39 | 47 | $pattern = '~\[[\s\p{L}]+\]~'; |
|
40 | 47 | $query = $criteria->getSearch(); |
|
41 | $matches = []; |
||
42 | 47 | if(!preg_match_all($pattern, $query, $matches)) |
|
43 | { |
||
44 | 4 | return; |
|
45 | } |
||
46 | 1 | if(count($matches[0]) > 1) |
|
47 | { |
||
48 | 4 | echo ''; |
|
49 | } |
||
50 | 4 | $field = $this->field; |
|
51 | 4 | foreach($criteria->getModels() as $model) |
|
52 | { |
||
53 | 4 | // Skip field if not exists on model |
|
54 | if(ManganMeta::create($model)->{$this->field} === false) |
||
55 | { |
||
56 | continue; |
||
57 | } |
||
58 | 4 | $cd = new ConditionDecorator($model); |
|
59 | 4 | $data = $cd->decorate($this->field); |
|
60 | $field = key($data); |
||
61 | 4 | } |
|
62 | foreach($matches[0] as $match) |
||
63 | 4 | { |
|
64 | $query = str_replace($match, '', $query); |
||
65 | $conditions[] = [ |
||
66 | 'term' => [ |
||
67 | $field => trim($match, '[]') |
||
68 | ] |
||
69 | ]; |
||
70 | } |
||
71 | |||
72 | $criteria->search($query); |
||
73 | } |
||
74 | |||
75 | public function getKind() |
||
76 | { |
||
77 | return self::KindFilter; |
||
78 | } |
||
79 | |||
80 | } |
||
81 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: