forked from aternosorg/php-model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhereGroup.php
More file actions
139 lines (123 loc) · 2.54 KB
/
WhereGroup.php
File metadata and controls
139 lines (123 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Aternos\Model\Query;
use Countable;
use Iterator;
/**
* Class WhereConditionGroup
*
* @package Aternos\Model\Query
* @implements Iterator<WhereCondition|WhereGroup>
*/
class WhereGroup implements Iterator, Countable, Validatable
{
/**
* Conjunction values
*/
const AND = 0;
const OR = 1;
/**
* Multiple WhereGroup or WhereCondition objects
*
* @var (WhereCondition|WhereGroup)[]
*/
protected array $group = [];
/**
* @var int
*/
public int $conjunction = self:: AND;
/**
* Group iterator
*
* @var int
*/
protected int $iterator = 0;
/**
* WhereGroup constructor.
*
* @param (WhereCondition|WhereGroup)[] $conditions
* @param int $conjunction
*/
public function __construct(array $conditions = [], int $conjunction = self:: AND)
{
$this->group = $conditions;
$this->conjunction = $conjunction;
}
/**
* Add an element to the group
*
* @param WhereCondition|WhereGroup $conditionOrGroup
*/
public function add(WhereCondition|WhereGroup $conditionOrGroup)
{
$this->group[] = $conditionOrGroup;
}
/**
* Get all group elements as array
*
* @return (WhereCondition|WhereGroup)[]
*/
public function getAll(): array
{
return $this->group;
}
public function validate(): void
{
foreach ($this->group as $element) {
$element->validate();
}
}
/**
* Return the current element
*
* @return WhereGroup|WhereCondition
*/
public function current(): WhereGroup|WhereCondition
{
return $this->group[$this->iterator];
}
/**
* Move forward to next element
*
* @return void
*/
public function next(): void
{
$this->iterator++;
}
/**
* Return the key of the current element
*
* @return int
*/
public function key(): int
{
return $this->iterator;
}
/**
* Checks if current position is valid
*
* @return boolean
*/
public function valid(): bool
{
return array_key_exists($this->iterator, $this->group);
}
/**
* Rewind the Iterator to the first element
*
* @return void
*/
public function rewind(): void
{
$this->iterator = 0;
}
/**
* Count elements of an object
*
* @return int
*/
public function count(): int
{
return count($this->group);
}
}