|
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 |
|
10
|
|
|
{ |
|
11
|
|
|
/** AND(conjunction) - Condition relation type */ |
|
12
|
|
|
const REL_AND = 'AND'; |
|
13
|
|
|
|
|
14
|
|
|
/** OR(disjunction) - Condition relation type */ |
|
15
|
|
|
const REL_OR = 'OR'; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Argument[] Collection of condition arguments */ |
|
18
|
|
|
public $arguments = array(); |
|
19
|
|
|
|
|
20
|
|
|
/** @var string Relation logic between arguments */ |
|
21
|
|
|
public $relation = self::REL_AND; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add condition argument to this condition group |
|
25
|
|
|
* @param Argument $argument Condition argument to be added |
|
26
|
|
|
*/ |
|
27
|
|
|
public function addArgument(Argument $argument) |
|
28
|
|
|
{ |
|
29
|
|
|
// Add condition as current condition argument |
|
30
|
|
|
$this->arguments[] = $argument; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add condition group to this condition group |
|
35
|
|
|
* @param Condition $condition Condition group to be added |
|
36
|
|
|
*/ |
|
37
|
|
|
public function addCondition(self $condition) |
|
38
|
|
|
{ |
|
39
|
|
|
// Add condition as current condition argument |
|
40
|
|
|
$this->arguments[] = $condition; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Generic condition addiction function |
|
45
|
|
|
* @param self|Argument|string $argument Entity for adding to arguments collection |
|
46
|
|
|
* @param string $value Argument value |
|
47
|
|
|
* @param string $relation Relation between argument and value |
|
48
|
|
|
* @return self Chaining |
|
49
|
|
|
*/ |
|
50
|
|
|
public function add($argument, $value = '', $relation = Relation::EQUAL) |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_string($argument) || is_scalar($argument)) { |
|
53
|
|
|
// Add new argument to arguments collection |
|
54
|
|
|
$this->arguments[] = new Argument($argument, $value, $relation); |
|
55
|
|
|
} elseif (is_a($argument, get_class($this))) { |
|
56
|
|
|
$this->addCondition($argument); |
|
|
|
|
|
|
57
|
|
|
} elseif (is_a($argument, __NAMESPACE__.'Argument')) { |
|
58
|
|
|
$this->addArgument($argument); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor |
|
66
|
|
|
* @param string $relation Relation type between arguments |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct($relation = null) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->relation = isset($relation) ? $relation : self::REL_AND; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: