|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Bluz\Db\Query; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Expression Builder |
|
15
|
|
|
* |
|
16
|
|
|
* @package Bluz\Db\Query |
|
17
|
|
|
*/ |
|
18
|
|
|
class CompositeBuilder implements \Countable |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string type AND|OR |
|
22
|
|
|
*/ |
|
23
|
|
|
private $type; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array parts of the composite expression |
|
27
|
|
|
*/ |
|
28
|
|
|
private $parts = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $parts parts of the composite expression |
|
34
|
|
|
* @param string $type AND|OR |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function __construct(array $parts = [], string $type = 'AND') |
|
37
|
|
|
{ |
|
38
|
3 |
|
$type = strtoupper($type); |
|
39
|
3 |
|
$this->type = $type === 'OR' ? 'OR' : 'AND'; |
|
40
|
3 |
|
$this->addParts($parts); |
|
41
|
3 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Adds a set of expressions to composite expression |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $parts |
|
47
|
|
|
* |
|
48
|
|
|
* @return CompositeBuilder |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function addParts($parts) : CompositeBuilder |
|
51
|
|
|
{ |
|
52
|
3 |
|
foreach ($parts as $part) { |
|
53
|
3 |
|
$this->addPart($part); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Adds an expression to composite expression |
|
61
|
|
|
* |
|
62
|
|
|
* @param mixed $part |
|
63
|
|
|
* |
|
64
|
|
|
* @return CompositeBuilder |
|
65
|
|
|
*/ |
|
66
|
3 |
|
public function addPart($part) : CompositeBuilder |
|
67
|
|
|
{ |
|
68
|
3 |
|
if (!empty($part) || ($part instanceof self && $part->count() > 0)) { |
|
69
|
3 |
|
$this->parts[] = $part; |
|
70
|
|
|
} |
|
71
|
3 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return type of this composite |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function getType() : string |
|
80
|
|
|
{ |
|
81
|
3 |
|
return $this->type; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Retrieves the amount of expressions on composite expression. |
|
86
|
|
|
* |
|
87
|
|
|
* @return integer |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function count() : int |
|
90
|
|
|
{ |
|
91
|
3 |
|
return count($this->parts); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Retrieve the string representation of this composite expression. |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
3 |
|
public function __toString() |
|
100
|
|
|
{ |
|
101
|
3 |
|
if ($this->count() === 1) { |
|
102
|
|
|
return (string) $this->parts[0]; |
|
103
|
|
|
} |
|
104
|
3 |
|
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|