|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Criteria; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
17
|
|
|
use Maslosoft\Mangan\Interfaces\ConditionDecoratorInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
|
19
|
|
|
use Maslosoft\Mangan\Transformers\CriteriaArray; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This class is used to decorate and sanitize conditions. |
|
23
|
|
|
* This should not be used directly. This is internal component of Criteria. |
|
24
|
|
|
* |
|
25
|
|
|
* @internal Criteria sub component |
|
26
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
27
|
|
|
*/ |
|
28
|
|
|
class ConditionDecorator implements ConditionDecoratorInterface |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Model instance |
|
33
|
|
|
* @var AnnotatedInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $model = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Metadata |
|
39
|
|
|
* @var ManganMeta |
|
40
|
|
|
*/ |
|
41
|
|
|
private $meta = null; |
|
42
|
|
|
|
|
43
|
104 |
|
public function __construct(AnnotatedInterface $model = null) |
|
44
|
|
|
{ |
|
45
|
104 |
|
if (!$model || !$model instanceof AnnotatedInterface) |
|
46
|
|
|
{ |
|
47
|
101 |
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
// Clone is to prevent possible required constructor params issues |
|
50
|
102 |
|
$this->model = clone $model; |
|
51
|
102 |
|
$this->meta = ManganMeta::create($this->model); |
|
52
|
102 |
|
} |
|
53
|
|
|
|
|
54
|
96 |
|
/** |
|
55
|
|
|
* @return AnnotatedInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getModel() |
|
58
|
96 |
|
{ |
|
59
|
|
|
return $this->model; |
|
60
|
|
|
} |
|
61
|
81 |
|
|
|
62
|
|
|
public function decorate($field, $value = null) |
|
63
|
|
|
{ |
|
64
|
|
|
// 1. Do not decorate if empty model or dot notation |
|
65
|
84 |
|
// 2. Ignore non existing fields |
|
66
|
84 |
|
if (!$this->model || strstr($field, '.') || $this->meta->$field === false) |
|
67
|
|
|
{ |
|
68
|
84 |
|
return [ |
|
69
|
|
|
$field => $value |
|
70
|
|
|
]; |
|
71
|
84 |
|
} |
|
72
|
|
|
|
|
73
|
84 |
|
$this->model->$field = $value; |
|
74
|
84 |
|
$data = CriteriaArray::fromModel($this->model, [$field]); |
|
75
|
|
|
|
|
76
|
5 |
|
return $this->_flatten($field, $this->model->$field, $data[$field]); |
|
77
|
|
|
} |
|
78
|
1 |
|
|
|
79
|
|
|
private function _flatten($field, $srcValue, $data) |
|
80
|
|
|
{ |
|
81
|
4 |
|
$value = $data; |
|
82
|
|
|
while (is_array($value)) |
|
83
|
4 |
|
{ |
|
84
|
|
|
if(empty($value)) |
|
85
|
4 |
|
{ |
|
86
|
4 |
|
break; |
|
87
|
4 |
|
} |
|
88
|
|
|
// Flat value traverse |
|
89
|
|
|
foreach ($value as $key => $val) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($srcValue === $val) |
|
92
|
|
|
{ |
|
93
|
|
|
$value = $value[$key]; |
|
94
|
|
|
$field = "$field.$key"; |
|
95
|
|
|
break 2; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Nested value |
|
100
|
|
|
$key = key($value); |
|
101
|
|
|
$value = $value[$key]; |
|
102
|
|
|
$field = "$field.$key"; |
|
103
|
84 |
|
if ($srcValue === $value) |
|
104
|
|
|
{ |
|
105
|
|
|
break; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
return [ |
|
111
|
|
|
$field => $value |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|