1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Condition.php - Add conditions to a Jaxon call |
5
|
|
|
* |
6
|
|
|
* @package jaxon-core |
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
8
|
|
|
* @copyright 2019 Thierry Feuzeu <[email protected]> |
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Jaxon\Request\Factory\Traits; |
14
|
|
|
|
15
|
|
|
trait Condition |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* A condition to check before sending this request |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $sCondition = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add a condition to the request |
26
|
|
|
* |
27
|
|
|
* The request is sent only if the condition is true. |
28
|
|
|
* |
29
|
|
|
* @param string $sCondition The condition to check |
30
|
|
|
* |
31
|
|
|
* @return Request |
32
|
|
|
*/ |
33
|
|
|
public function when($sCondition) |
34
|
|
|
{ |
35
|
|
|
$this->sCondition = Parameter::make($sCondition)->getScript(); |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Add a condition to the request |
41
|
|
|
* |
42
|
|
|
* The request is sent only if the condition is false. |
43
|
|
|
* |
44
|
|
|
* @param string $sCondition The condition to check |
45
|
|
|
* |
46
|
|
|
* @return Request |
47
|
|
|
*/ |
48
|
|
|
public function unless($sCondition) |
49
|
|
|
{ |
50
|
|
|
$this->sCondition = '!(' . Parameter::make($sCondition)->getScript() . ')'; |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if a value is equal to another before sending the request |
56
|
|
|
* |
57
|
|
|
* @param string $sValue1 The first value to compare |
58
|
|
|
* @param string $sValue2 The second value to compare |
59
|
|
|
* |
60
|
|
|
* @return Request |
61
|
|
|
*/ |
62
|
|
|
public function ifeq($sValue1, $sValue2) |
63
|
|
|
{ |
64
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '==' . Parameter::make($sValue2) . ')'; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check if a value is not equal to another before sending the request |
70
|
|
|
* |
71
|
|
|
* @param string $sValue1 The first value to compare |
72
|
|
|
* @param string $sValue2 The second value to compare |
73
|
|
|
* |
74
|
|
|
* @return Request |
75
|
|
|
*/ |
76
|
|
|
public function ifne($sValue1, $sValue2) |
77
|
|
|
{ |
78
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '!=' . Parameter::make($sValue2) . ')'; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check if a value is greater than another before sending the request |
84
|
|
|
* |
85
|
|
|
* @param string $sValue1 The first value to compare |
86
|
|
|
* @param string $sValue2 The second value to compare |
87
|
|
|
* |
88
|
|
|
* @return Request |
89
|
|
|
*/ |
90
|
|
|
public function ifgt($sValue1, $sValue2) |
91
|
|
|
{ |
92
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '>' . Parameter::make($sValue2) . ')'; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check if a value is greater or equal to another before sending the request |
98
|
|
|
* |
99
|
|
|
* @param string $sValue1 The first value to compare |
100
|
|
|
* @param string $sValue2 The second value to compare |
101
|
|
|
* |
102
|
|
|
* @return Request |
103
|
|
|
*/ |
104
|
|
|
public function ifge($sValue1, $sValue2) |
105
|
|
|
{ |
106
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '>=' . Parameter::make($sValue2) . ')'; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if a value is lower than another before sending the request |
112
|
|
|
* |
113
|
|
|
* @param string $sValue1 The first value to compare |
114
|
|
|
* @param string $sValue2 The second value to compare |
115
|
|
|
* |
116
|
|
|
* @return Request |
117
|
|
|
*/ |
118
|
|
|
public function iflt($sValue1, $sValue2) |
119
|
|
|
{ |
120
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '<' . Parameter::make($sValue2) . ')'; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Check if a value is lower or equal to another before sending the request |
126
|
|
|
* |
127
|
|
|
* @param string $sValue1 The first value to compare |
128
|
|
|
* @param string $sValue2 The second value to compare |
129
|
|
|
* |
130
|
|
|
* @return Request |
131
|
|
|
*/ |
132
|
|
|
public function ifle($sValue1, $sValue2) |
133
|
|
|
{ |
134
|
|
|
$this->sCondition = '(' . Parameter::make($sValue1) . '<=' . Parameter::make($sValue2) . ')'; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|