1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Hydrogen package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace RDS\Hydrogen\Criteria; |
11
|
|
|
|
12
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
13
|
|
|
use RDS\Hydrogen\Criteria\Where\Operator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Where |
17
|
|
|
*/ |
18
|
|
|
class Where extends Criterion |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Operator |
22
|
|
|
*/ |
23
|
|
|
private $operator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var mixed |
27
|
|
|
*/ |
28
|
|
|
private $value; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $and; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Where constructor. |
37
|
|
|
* @param string $field |
38
|
|
|
* @param string $operator |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @param bool $and |
41
|
|
|
*/ |
42
|
35 |
|
public function __construct(string $field, string $operator, $value, bool $and = true) |
43
|
|
|
{ |
44
|
35 |
|
parent::__construct($field); |
45
|
|
|
|
46
|
35 |
|
$this->value = $this->normalizeValue($value); |
47
|
35 |
|
$this->operator = $this->normalizeOperator(new Operator($operator), $this->value); |
48
|
35 |
|
$this->and = $and; |
49
|
35 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $value |
53
|
|
|
* @return array|mixed |
54
|
|
|
*/ |
55
|
35 |
|
private function normalizeValue($value) |
56
|
|
|
{ |
57
|
|
|
switch (true) { |
58
|
35 |
|
case $value instanceof Arrayable: |
59
|
|
|
return $value->toArray(); |
60
|
|
|
|
61
|
35 |
|
case $value instanceof \Traversable: |
62
|
|
|
return \iterator_to_array($value); |
63
|
|
|
|
64
|
35 |
|
case \is_object($value) && \method_exists($value, '__toString'): |
65
|
|
|
return (string)$value; |
66
|
|
|
} |
67
|
|
|
|
68
|
35 |
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Operator $operator |
73
|
|
|
* @param mixed $value |
74
|
|
|
* @return Operator |
75
|
|
|
*/ |
76
|
35 |
|
private function normalizeOperator(Operator $operator, $value): Operator |
77
|
|
|
{ |
78
|
35 |
|
if (\is_array($value) && $operator->is(Operator::EQ)) { |
79
|
2 |
|
return $operator->changeTo(Operator::IN); |
80
|
|
|
} |
81
|
|
|
|
82
|
33 |
|
if (\is_array($value) && $operator->is(Operator::NEQ)) { |
83
|
2 |
|
return $operator->changeTo(Operator::NOT_IN); |
84
|
|
|
} |
85
|
|
|
|
86
|
31 |
|
return $operator; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $operator |
91
|
|
|
* @param null $value |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
25 |
|
public static function completeMissingParameters($operator, $value = null): array |
95
|
|
|
{ |
96
|
25 |
|
if ($value === null) { |
97
|
9 |
|
[$value, $operator] = [$operator, Operator::EQ]; |
98
|
|
|
} |
99
|
|
|
|
100
|
25 |
|
return [$operator, $value]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Operator |
105
|
|
|
*/ |
106
|
35 |
|
public function getOperator(): Operator |
107
|
|
|
{ |
108
|
35 |
|
return $this->operator; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
35 |
|
public function getValue() |
115
|
|
|
{ |
116
|
35 |
|
return $this->value; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
35 |
|
public function isAnd(): bool |
123
|
|
|
{ |
124
|
35 |
|
return $this->and; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|