Where::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Common\Field;
14
use RDS\Hydrogen\Criteria\Where\Operator;
15
use RDS\Hydrogen\Query;
16
17
/**
18
 * Class Where
19
 */
20
class Where extends Criterion
21
{
22
    /**
23
     * @var Operator
24
     */
25
    private $operator;
26
27
    /**
28
     * @var Field
29
     */
30
    private $field;
31
32
    /**
33
     * @var mixed
34
     */
35
    private $value;
36
37
    /**
38
     * @var bool
39
     */
40
    private $and;
41
42
    /**
43
     * Where constructor.
44
     * @param Query $query
45
     * @param string $field
46
     * @param string $operator
47
     * @param mixed $value
48
     * @param bool $and
49
     */
50 43
    public function __construct(Query $query, string $field, string $operator, $value, bool $and = true)
51
    {
52 43
        parent::__construct($query);
53
54 43
        $this->field = $this->field($field);
55 43
        $this->value = $this->normalizeValue($value);
56 43
        $this->operator = $this->normalizeOperator(new Operator($operator), $this->value);
57 43
        $this->and = $and;
58 43
    }
59
60
    /**
61
     * @param mixed $value
62
     * @return array|mixed
63
     */
64 43
    private function normalizeValue($value)
65
    {
66
        switch (true) {
67 43
            case $value instanceof Arrayable:
68
                return $value->toArray();
69
70 43
            case $value instanceof \Traversable:
71
                return \iterator_to_array($value);
72
73 43
            case \is_object($value) && \method_exists($value, '__toString'):
74
                return (string)$value;
75
        }
76
77 43
        return $value;
78
    }
79
80
    /**
81
     * @param Operator $operator
82
     * @param mixed $value
83
     * @return Operator
84
     */
85 43
    private function normalizeOperator(Operator $operator, $value): Operator
86
    {
87 43
        if (\is_array($value) && $operator->is(Operator::EQ)) {
88 2
            return $operator->changeTo(Operator::IN);
89
        }
90
91 41
        if (\is_array($value) && $operator->is(Operator::NEQ)) {
92 2
            return $operator->changeTo(Operator::NOT_IN);
93
        }
94
95 39
        return $operator;
96
    }
97
98
    /**
99
     * @param mixed $operator
100
     * @param null $value
101
     * @return array
102
     */
103 41
    public static function completeMissingParameters($operator, $value = null): array
104
    {
105 41
        if ($value === null) {
106 11
            [$value, $operator] = [$operator, Operator::EQ];
107
        }
108
109 41
        return [$operator, $value];
110
    }
111
112
    /**
113
     * @return Field
114
     */
115 43
    public function getField(): Field
116
    {
117 43
        return $this->field;
118
    }
119
120
    /**
121
     * @return Operator
122
     */
123 43
    public function getOperator(): Operator
124
    {
125 43
        return $this->operator;
126
    }
127
128
    /**
129
     * @return mixed
130
     */
131 43
    public function getValue()
132
    {
133 43
        return $this->value;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139 43
    public function isAnd(): bool
140
    {
141 43
        return $this->and;
142
    }
143
}
144