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\Manganel\Interfaces\ManganelAwareInterface; |
12
|
|
|
use Maslosoft\Manganel\Interfaces\QueryBuilder\ConditionDecoratorInterface; |
13
|
|
|
use Maslosoft\Manganel\SearchCriteria; |
14
|
|
|
use Maslosoft\Manganel\Traits\ManganelAwareTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* TagDecorator |
18
|
|
|
* |
19
|
|
|
* NOTE: This decorator must be at beginning, as it modifies criteria! |
20
|
|
|
* |
21
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
22
|
|
|
*/ |
23
|
|
|
class TagDecorator implements ConditionDecoratorInterface, ManganelAwareInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
use ManganelAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Field which should be used as a tag filter |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $field = ''; |
33
|
|
|
|
34
|
47 |
|
public function decorate(&$conditions, SearchCriteria $criteria) |
35
|
|
|
{ |
36
|
47 |
|
assert(!empty($this->field), sprintf('Property `field` is required for `%s`', __CLASS__)); |
37
|
47 |
|
$pattern = '~\[[\s\p{L}]+\]~'; |
38
|
47 |
|
$query = $criteria->getSearch(); |
39
|
47 |
|
$matches = []; |
40
|
47 |
|
if(!preg_match_all($pattern, $query, $matches)) |
41
|
|
|
{ |
42
|
47 |
|
return; |
43
|
|
|
} |
44
|
4 |
|
if(count($matches[0]) > 1) |
45
|
|
|
{ |
46
|
1 |
|
echo ''; |
47
|
|
|
} |
48
|
4 |
|
foreach($matches[0] as $match) |
49
|
|
|
{ |
50
|
4 |
|
$query = str_replace($match, '', $query); |
51
|
4 |
|
$conditions[] = [ |
52
|
|
|
'term' => [ |
53
|
4 |
|
$this->field => trim($match, '[]') |
54
|
|
|
] |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
$criteria->search($query); |
59
|
4 |
|
} |
60
|
|
|
|
61
|
4 |
|
public function getKind() |
62
|
|
|
{ |
63
|
4 |
|
return self::KindFilter; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|