1
|
|
|
<?php |
2
|
|
|
namespace samsonframework\orm; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Query condition arguments group |
6
|
|
|
* @author Vitaly Iegorov <[email protected]> |
7
|
|
|
* @version 2.0 |
8
|
|
|
*/ |
9
|
|
|
class Condition implements ConditionInterface |
10
|
|
|
{ |
11
|
|
|
/** @var string Relation logic between arguments */ |
12
|
|
|
public $relation = ConditionInterface::CONJUNCTION; |
13
|
|
|
|
14
|
|
|
/** @var Argument[] Collection of condition arguments */ |
15
|
|
|
protected $arguments = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Add condition argument to this condition group |
19
|
|
|
* @param ArgumentInterface $argument Condition argument to be added |
20
|
|
|
* @return self Chaining |
21
|
|
|
*/ |
22
|
|
|
public function addArgument(ArgumentInterface $argument) |
23
|
|
|
{ |
24
|
|
|
// Add condition as current condition argument |
25
|
|
|
$this->arguments[] = $argument; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Add condition group to this condition group |
32
|
|
|
* @param ConditionInterface $condition Condition group to be added |
33
|
|
|
* @return self Chaining |
34
|
|
|
*/ |
35
|
|
|
public function addCondition(ConditionInterface $condition) |
36
|
|
|
{ |
37
|
|
|
// Add condition as current condition argument |
38
|
|
|
$this->arguments[] = $condition; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Generic condition addiction function |
45
|
|
|
* @param string $argument Entity for adding to arguments collection |
46
|
|
|
* @param mixed $value Argument value |
47
|
|
|
* @param string $relation Relation between argument and value |
48
|
|
|
* @return self Chaining |
49
|
|
|
*/ |
50
|
|
|
public function add($argument, $value, $relation = ArgumentInterface::EQUAL) |
51
|
|
|
{ |
52
|
|
|
if (is_string($argument)) { |
53
|
|
|
// Add new argument to arguments collection |
54
|
|
|
$this->arguments[] = new Argument($argument, $value, $relation); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Constructor |
62
|
|
|
* @param string $relation Relation type between arguments |
63
|
|
|
*/ |
64
|
|
|
public function __construct($relation = null) |
65
|
|
|
{ |
66
|
|
|
$this->relation = isset($relation) ? $relation : ConditionInterface::CONJUNCTION; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the current element |
71
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
72
|
|
|
* @return mixed Can return any type. |
73
|
|
|
* @since 5.0.0 |
74
|
|
|
*/ |
75
|
|
|
public function current() |
76
|
|
|
{ |
77
|
|
|
return current($this->arguments); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Move forward to next element |
82
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
83
|
|
|
* @return void Any returned value is ignored. |
84
|
|
|
* @since 5.0.0 |
85
|
|
|
*/ |
86
|
|
|
public function next() |
87
|
|
|
{ |
88
|
|
|
next($this->arguments); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the key of the current element |
93
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
94
|
|
|
* @return mixed scalar on success, or null on failure. |
95
|
|
|
* @since 5.0.0 |
96
|
|
|
*/ |
97
|
|
|
public function key() |
98
|
|
|
{ |
99
|
|
|
return key($this->arguments); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Checks if current position is valid |
104
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
105
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
106
|
|
|
* Returns true on success or false on failure. |
107
|
|
|
* @since 5.0.0 |
108
|
|
|
*/ |
109
|
|
|
public function valid() |
110
|
|
|
{ |
111
|
|
|
return key($this->arguments) !== null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Rewind the Iterator to the first element |
116
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
117
|
|
|
* @return void Any returned value is ignored. |
118
|
|
|
* @since 5.0.0 |
119
|
|
|
*/ |
120
|
|
|
public function rewind() |
121
|
|
|
{ |
122
|
|
|
reset($this->arguments); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|