GetArrayParamsQueryCondition::toArray()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 15
cts 15
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 0
crap 3
1
<?php
2
3
namespace Trucker\Finders\Conditions;
4
5
use Guzzle\Http\Message\Request;
6
use Illuminate\Container\Container;
7
use Trucker\Facades\Config;
8
9
/**
10
 * Class to manage query conditions for a request, where the
11
 * query conditions are passed as HTTP GET array parameters which are
12
 * nested within a particular parameter name (defined in the config).
13
 * The resulting GET params might be something like:.
14
 *
15
 * <code>
16
 * search[0][property]=someProperty
17
 * search[0][operator]=<
18
 * search[0][value]=1234
19
 * search[1][property]=anotherProperty
20
 * search[1][operator]=LIKE
21
 * search[1][value]=someString
22
 * logical_operator=AND
23
 * </code>
24
 */
25
class GetArrayParamsQueryCondition implements QueryConditionInterface
26
{
27
    /**
28
     * Constant to referr to array
29
     * entries that contain a property
30
     * or attribute to which a condition should
31
     * be applied.
32
     */
33
    const PROPERTY = 'property';
34
35
    /**
36
     * Constant to referr to array
37
     * entries that contain the operator
38
     * that a value should be matched against
39
     * on a property.
40
     */
41
    const OPERATOR = 'operator';
42
43
    /**
44
     * Constant to referr to array
45
     * entries that contain a value that
46
     * should be used in a conditional match
47
     * against a property.
48
     */
49
    const VALUE = 'value';
50
51
    /**
52
     * The IoC Container.
53
     *
54
     * @var Container
55
     */
56
    protected $app;
57
58
    /**
59
     * Collection of conditions, each condition
60
     * will be a 3 key array having an entry for
61
     * property, operator and value.
62
     *
63
     * @var array
64
     */
65
    protected $conditions = [];
66
67
    /**
68
     * The logical operator that should be used
69
     * to group the conditions herein together.
70
     *
71
     * @var string
72
     */
73
    protected $logicalOperator;
74
75
    /**
76
     * Build a new GetArrayParamsQueryCondition.
77
     *
78
     * @param Container $app
79
     */
80 3
    public function __construct(Container $app)
81
    {
82 3
        $this->app = $app;
83 3
    }
84
85
    /**
86
     * Function to return a new popuplated instance,
87
     * typically this would be called from the Facade.
88
     *
89
     * @return GetArrayParamsQueryCondition
90
     */
91
    public function newInstance()
92
    {
93
        return new static($this->app);
94
    }
95
96
    /**
97
     * Function to add a query condition.
98
     *
99
     * @param string $property The field the condition operates on
100
     * @param string $operator The operator (=, <, >, <= and so on)
101
     * @param string $value    The value the condition should match
102
     */
103 2
    public function addCondition($property, $operator, $value)
104
    {
105 2
        $this->conditions[] = [
106 2
            self::PROPERTY => $property,
107 2
            self::OPERATOR => $operator,
108 2
            self::VALUE => $value,
109
        ];
110 2
    }
111
112
    /**
113
     * Function to set the logical operator for the
114
     * combination of any conditions that have been passed to the
115
     * addCondition() function.
116
     *
117
     * @param string $operator
118
     *
119
     * @throws \InvalidArgumentException
120
     */
121 2
    public function setLogicalOperator($operator)
122
    {
123 2
        if ($operator != $this->getLogicalOperatorAnd() &&
124 2
            $operator != $this->getLogicalOperatorOr()
125
        ) {
126 1
            throw new \InvalidArgumentException("Invalid logical operator: {$operator}");
127
        }
128
129 1
        $this->logicalOperator = $operator;
130 1
    }
131
132
    /**
133
     * Function to get the string representing
134
     * the AND logical operator.
135
     *
136
     * @return string
137
     */
138 2
    public function getLogicalOperatorAnd()
139
    {
140 2
        return Config::get('query_condition.get_array_params.and_operator');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Trucker\Facades\Config. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
        return Config::/** @scrutinizer ignore-call */ get('query_condition.get_array_params.and_operator');
Loading history...
141
    }
142
143
    /**
144
     * Function to get the string representing
145
     * the OR logical operator.
146
     *
147
     * @return string
148
     */
149 1
    public function getLogicalOperatorOr()
150
    {
151 1
        return Config::get('query_condition.get_array_params.or_operator');
152
    }
153
154
    /**
155
     * Function to add all the conditions that have been
156
     * given to the class to a given request object.
157
     *
158
     * @param Request $request Request passed by reference
159
     */
160 1
    public function addToRequest(Request $request)
161
    {
162 1
        $query = $request->getQuery();
163
164 1
        $conatiner = Config::get('query_condition.get_array_params.container_parameter');
165 1
        $property = Config::get('query_condition.get_array_params.property');
166 1
        $operator = Config::get('query_condition.get_array_params.operator');
167 1
        $value = Config::get('query_condition.get_array_params.value');
168
169 1
        $x = 0;
170 1
        foreach ($this->conditions as $condition) {
171 1
            $query->add(
172 1
                "{$conatiner}[$x][{$property}]",
173 1
                $condition[self::PROPERTY]
174
            );
175 1
            $query->add(
176 1
                "{$conatiner}[$x][{$operator}]",
177 1
                $condition[self::OPERATOR]
178
            );
179 1
            $query->add(
180 1
                "{$conatiner}[$x][{$value}]",
181 1
                $condition[self::VALUE]
182
            );
183
184 1
            ++$x;
185
        }//end foreach $findConditions
186
187 1
        if (isset($this->logicalOperator)) {
188 1
            $query->add(
189 1
                Config::get('query_condition.get_array_params.logical_operator'),
190 1
                $this->logicalOperator
191
            );
192
        }
193 1
    }
194
195
    /**
196
     * Function to convert the conditions and
197
     * logical operator represented in this class
198
     * to an array, this is useful for testing.
199
     *
200
     * @return array
201
     */
202 2
    public function toArray()
203
    {
204 2
        $conatiner = Config::get('query_condition.get_array_params.container_parameter');
205 2
        $property = Config::get('query_condition.get_array_params.property');
206 2
        $operator = Config::get('query_condition.get_array_params.operator');
207 2
        $value = Config::get('query_condition.get_array_params.value');
208
209 2
        $params = [];
210
211 2
        $x = 0;
212 2
        foreach ($this->conditions as $condition) {
213 2
            $params["{$conatiner}[$x][{$property}]"] = $condition[self::PROPERTY];
214 2
            $params["{$conatiner}[$x][{$operator}]"] = $condition[self::OPERATOR];
215 2
            $params["{$conatiner}[$x][{$value}]"] = $condition[self::VALUE];
216
217 2
            ++$x;
218
        }//end foreach $findConditions
219
220 2
        if (isset($this->logicalOperator)) {
221 1
            $params[Config::get('query_condition.get_array_params.logical_operator')] = $this->logicalOperator;
222
        }
223
224 2
        return $params;
225
    }
226
227
    /**
228
     * Function to convert the conditions and logical operator
229
     * represented in this class to a querystring, this is useful
230
     * for testing.
231
     *
232
     * @return string
233
     */
234 2
    public function toQueryString()
235
    {
236 2
        return http_build_query($this->toArray());
237
    }
238
}
239