1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL-3.0-only, proprietary |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Decorators\QueryBuilder; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Gazebo\PluginFactory; |
16
|
|
|
use Maslosoft\Manganel\Interfaces\ManganelAwareInterface; |
17
|
|
|
use Maslosoft\Manganel\Interfaces\QueryBuilder\BodyDecoratorInterface; |
18
|
|
|
use Maslosoft\Manganel\Interfaces\QueryBuilder\ConditionDecoratorInterface; |
19
|
|
|
use Maslosoft\Manganel\SearchCriteria; |
20
|
|
|
use Maslosoft\Manganel\Traits\ManganelAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This decorator provides filtering capabilities for query builder |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class ConditionsDecorator implements BodyDecoratorInterface, |
28
|
|
|
ManganelAwareInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
use ManganelAwareTrait; |
32
|
|
|
|
33
|
32 |
|
public function decorate(&$body, SearchCriteria $criteria) |
34
|
|
|
{ |
35
|
32 |
|
$decorators = (new PluginFactory())->instance($this->getManganel()->decorators, $criteria, [ |
36
|
32 |
|
ConditionDecoratorInterface::class |
37
|
|
|
]); |
38
|
|
|
|
39
|
32 |
|
$bool = []; |
40
|
32 |
|
foreach ($decorators as $decorator) |
41
|
|
|
{ |
42
|
|
|
/* @var $decorator ConditionDecoratorInterface */ |
43
|
32 |
|
if ($decorator instanceof ManganelAwareInterface) |
44
|
|
|
{ |
45
|
32 |
|
$decorator->setManganel($this->getManganel()); |
46
|
|
|
} |
47
|
32 |
|
$conditions = []; |
48
|
32 |
|
$decorator->decorate($conditions, $criteria); |
49
|
32 |
|
if (empty($conditions)) |
50
|
|
|
{ |
51
|
32 |
|
continue; |
52
|
|
|
} |
53
|
32 |
|
$kind = $decorator->getKind(); |
54
|
32 |
|
if(false === $kind) |
55
|
|
|
{ |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
32 |
|
if (empty($bool[$kind])) |
59
|
|
|
{ |
60
|
32 |
|
$bool[$kind] = []; |
61
|
|
|
} |
62
|
32 |
|
foreach($conditions as $condition) |
63
|
|
|
{ |
64
|
32 |
|
$bool[$kind][] = $condition; |
65
|
|
|
} |
66
|
|
|
} |
67
|
32 |
|
if (!empty($bool)) |
68
|
|
|
{ |
69
|
32 |
|
$body['query'] = [ |
70
|
32 |
|
'bool' => $bool |
71
|
|
|
]; |
72
|
|
|
} |
73
|
32 |
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|