These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace Compolomus\LSQLQueryBuilder\Parts; |
||
4 | |||
5 | use Compolomus\LSQLQueryBuilder\System\{ |
||
6 | Conditions, |
||
7 | Traits\Helper, |
||
8 | Traits\Caller |
||
9 | }; |
||
10 | |||
11 | class Where |
||
12 | { |
||
13 | use Caller, Helper; |
||
14 | |||
15 | private $whereType; |
||
16 | |||
17 | private $whereTypes = [ |
||
18 | 'and', |
||
19 | 'or' |
||
20 | ]; |
||
21 | |||
22 | private $counter = 0; |
||
23 | |||
24 | private $conditions; |
||
25 | |||
26 | public function where(array $where = [], string $type = 'and'): Where |
||
27 | { |
||
28 | if (!in_array(strtolower($type), $this->whereTypes)) { |
||
29 | throw new \InvalidArgumentException('DIE |WHERE construct|'); |
||
30 | } |
||
31 | $this->whereType[$this->counter] = $type; |
||
32 | $this->conditions[$this->counter] = new Conditions; |
||
33 | if (count($where)) { |
||
34 | foreach ($where as $condition) { |
||
35 | list($field, $cond, $value) = $condition; |
||
36 | $this->add($field, $cond, $value); |
||
37 | } |
||
38 | } |
||
39 | $this->counter++; |
||
40 | return $this; |
||
41 | } |
||
42 | |||
43 | public function condition(): Conditions |
||
44 | { |
||
45 | return end($this->conditions); |
||
46 | } |
||
47 | |||
48 | public function __construct(array $where = [], string $type = 'and') |
||
49 | { |
||
50 | $this->where($where, $type); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string $field |
||
55 | * @param string $cond |
||
56 | * @param mixed $value |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function add(string $field, string $cond, $value): Where |
||
60 | { |
||
61 | $this->condition()->add($field, $cond, $value); |
||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | public function result(): string |
||
66 | { |
||
67 | $array = []; |
||
68 | $placeholders = []; |
||
69 | foreach ($this->conditions as $key => $condition) { |
||
70 | $placeholders += $condition->placeholders()->get(); |
||
71 | $array[] = '(' . $this->concatWhere($condition->conditions(), $this->whereType[$key]) . ')'; |
||
72 | } |
||
73 | $this->addPlaceholders($placeholders); |
||
0 ignored issues
–
show
|
|||
74 | return 'WHERE ' . $this->concatWhere($array, 'and'); |
||
75 | } |
||
76 | } |
||
77 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: