1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Objects; |
4
|
|
|
|
5
|
|
|
use Mado\QueryBundle\Vocabulary\Operators; |
6
|
|
|
|
7
|
|
|
final class Operator |
8
|
|
|
{ |
9
|
|
|
private $operator; |
10
|
|
|
|
11
|
6 |
|
private function __construct(array $params) |
12
|
|
|
{ |
13
|
6 |
|
$this->operator = $params['operator']; |
14
|
6 |
|
} |
15
|
|
|
|
16
|
3 |
|
public static function getDefault() : Operator |
17
|
|
|
{ |
18
|
3 |
|
$operator = Operators::getDefaultOperator(); |
19
|
|
|
|
20
|
3 |
|
return new self([ |
21
|
3 |
|
'operator' => $operator, |
22
|
|
|
]); |
23
|
|
|
} |
24
|
|
|
|
25
|
2 |
|
public function getMeta() : string |
26
|
|
|
{ |
27
|
2 |
|
$this->ensureMetaIsDefined(); |
28
|
|
|
|
29
|
2 |
|
return $this->operator['meta']; |
30
|
|
|
} |
31
|
|
|
|
32
|
4 |
|
public static function fromRawValue(array $operator) : Operator |
33
|
|
|
{ |
34
|
4 |
|
if (!isset($operator['meta'])) { |
35
|
1 |
|
throw new \RuntimeException( |
36
|
1 |
|
'Oops! Raw operator must contain `meta` parameter' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
return new self([ |
41
|
3 |
|
'operator' => $operator, |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public static function fromFilteringObject( |
46
|
|
|
FilteringObject $filteringObject |
47
|
|
|
) : Operator { |
48
|
2 |
|
if(true === $filteringObject->hasOperator()){ |
49
|
1 |
|
$operatorName = $filteringObject->getOperator(); |
50
|
1 |
|
$operator = Operators::get($operatorName); |
51
|
|
|
|
52
|
1 |
|
return new self([ |
53
|
1 |
|
'operator' => $operator, |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return Operator::getDefault(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getRawValue() : array |
61
|
|
|
{ |
62
|
|
|
return $this->operator; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
private function ensureMetaIsDefined() : void |
66
|
|
|
{ |
67
|
2 |
|
if (!isset($this->operator['meta'])) { |
68
|
|
|
throw new \RuntimeException( |
69
|
|
|
'Oops! Invalid meta value' |
70
|
|
|
); |
71
|
|
|
} |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
public function haveSubstitutionPattern() : bool |
75
|
|
|
{ |
76
|
2 |
|
return isset($this->operator['substitution_pattern']); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function getSubstitutionPattern() : string |
80
|
|
|
{ |
81
|
2 |
|
if ($this->haveSubstitutionPattern()) { |
82
|
1 |
|
return $this->operator['substitution_pattern']; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
throw new \RuntimeException( |
86
|
1 |
|
'Oops! Current operator have not substitution pattern.' |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|