Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

Condition::when()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Features;
14
15
use Jaxon\Request\Factory\Parameter;
16
17
trait Condition
18
{
19
    /**
20
     * A condition to check before sending this request
21
     *
22
     * @var string
23
     */
24
    protected $sCondition = null;
25
26
    /**
27
     * Add a condition to the request
28
     *
29
     * The request is sent only if the condition is true.
30
     *
31
     * @param string        $sCondition               The condition to check
32
     *
33
     * @return Request
34
     */
35
    public function when($sCondition)
36
    {
37
        $this->sCondition = Parameter::make($sCondition)->getScript();
38
        return $this;
39
    }
40
41
    /**
42
     * Add a condition to the request
43
     *
44
     * The request is sent only if the condition is false.
45
     *
46
     * @param string        $sCondition               The condition to check
47
     *
48
     * @return Request
49
     */
50
    public function unless($sCondition)
51
    {
52
        $this->sCondition = '!(' . Parameter::make($sCondition)->getScript() . ')';
53
        return $this;
54
    }
55
56
    /**
57
     * Check if a value is equal to another before sending the request
58
     *
59
     * @param string        $sValue1                  The first value to compare
60
     * @param string        $sValue2                  The second value to compare
61
     *
62
     * @return Request
63
     */
64
    public function ifeq($sValue1, $sValue2)
65
    {
66
        $this->sCondition = '(' . Parameter::make($sValue1) . '==' . Parameter::make($sValue2) . ')';
67
        return $this;
68
    }
69
70
    /**
71
     * Check if a value is not equal to another before sending the request
72
     *
73
     * @param string        $sValue1                  The first value to compare
74
     * @param string        $sValue2                  The second value to compare
75
     *
76
     * @return Request
77
     */
78
    public function ifne($sValue1, $sValue2)
79
    {
80
        $this->sCondition = '(' . Parameter::make($sValue1) . '!=' . Parameter::make($sValue2) . ')';
81
        return $this;
82
    }
83
84
    /**
85
     * Check if a value is greater than another before sending the request
86
     *
87
     * @param string        $sValue1                  The first value to compare
88
     * @param string        $sValue2                  The second value to compare
89
     *
90
     * @return Request
91
     */
92
    public function ifgt($sValue1, $sValue2)
93
    {
94
        $this->sCondition = '(' . Parameter::make($sValue1) . '>' . Parameter::make($sValue2) . ')';
95
        return $this;
96
    }
97
98
    /**
99
     * Check if a value is greater or equal to another before sending the request
100
     *
101
     * @param string        $sValue1                  The first value to compare
102
     * @param string        $sValue2                  The second value to compare
103
     *
104
     * @return Request
105
     */
106
    public function ifge($sValue1, $sValue2)
107
    {
108
        $this->sCondition = '(' . Parameter::make($sValue1) . '>=' . Parameter::make($sValue2) . ')';
109
        return $this;
110
    }
111
112
    /**
113
     * Check if a value is lower than another before sending the request
114
     *
115
     * @param string        $sValue1                  The first value to compare
116
     * @param string        $sValue2                  The second value to compare
117
     *
118
     * @return Request
119
     */
120
    public function iflt($sValue1, $sValue2)
121
    {
122
        $this->sCondition = '(' . Parameter::make($sValue1) . '<' . Parameter::make($sValue2) . ')';
123
        return $this;
124
    }
125
126
    /**
127
     * Check if a value is lower or equal to another before sending the request
128
     *
129
     * @param string        $sValue1                  The first value to compare
130
     * @param string        $sValue2                  The second value to compare
131
     *
132
     * @return Request
133
     */
134
    public function ifle($sValue1, $sValue2)
135
    {
136
        $this->sCondition = '(' . Parameter::make($sValue1) . '<=' . Parameter::make($sValue2) . ')';
137
        return $this;
138
    }
139
}
140