GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 64317d...bc2000 )
by De
02:50
created

Operator::addToSetEach()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 15

Duplication

Lines 9
Ratio 31.03 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 29
rs 8.5806
cc 4
eloc 15
nc 3
nop 2
1
<?php
2
3
/**
4
 * This file is part of the PHPMongo package.
5
 *
6
 * (c) Dmytro Sokil <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sokil\Mongo;
13
14
use \Sokil\Mongo\Structure\Arrayable;
15
16
class Operator implements Arrayable
17
{
18
        /**
19
     *
20
     * @var array list of update operations
21
     */
22
    private $_operators = array();
23
    
24
    public function set($fieldName, $value)
25
    {        
26
        if(!isset($this->_operators['$set'])) {
27
            $this->_operators['$set'] = array();
28
        }
29
        
30
        $this->_operators['$set'][$fieldName] = Structure::prepareToStore($value);
31
        
32
        return $this;
33
    }
34
    
35
    public function push($fieldName, $value)
36
    {
37
        // value must be list, not dictionary
38
        if(is_array($value)) {
39
            $value = array_values($value);
40
        }
41
42
        // prepasre to store
43
        $value = Structure::prepareToStore($value);
44
        
45
        // no $push operator found
46
        if(!isset($this->_operators['$push'])) {
47
            $this->_operators['$push'] = array();
48
        }
49
        
50
        // no field name found
51
        if(!isset($this->_operators['$push'][$fieldName])) {
52
            $this->_operators['$push'][$fieldName] = $value;
53
        }
54
        
55
        // field name found and has single value
56
        else if(!is_array($this->_operators['$push'][$fieldName]) || !isset($this->_operators['$push'][$fieldName]['$each'])) {
57
            $oldValue = $this->_operators['$push'][$fieldName];
58
            $this->_operators['$push'][$fieldName] = array(
59
                '$each' => array($oldValue, $value)
60
            );
61
        }
62
        
63
        // field name found and already $each
64
        else {
65
            $this->_operators['$push'][$fieldName]['$each'][] = $value;
66
        }
67
68
        return $this;
69
    }
70
    
71
    public function pushEach($fieldName, array $values)
72
    {
73
        // value must be list, not dictionary
74
        $values = array_values($values);
75
76
        // prepasre to store
77
        $values = Structure::prepareToStore($values);
78
79
        // no $push operator found
80
        if(!isset($this->_operators['$push'])) {
81
            $this->_operators['$push'] = array();
82
        }
83
        
84
        // no field name found
85
        if(!isset($this->_operators['$push'][$fieldName])) {
86
            $this->_operators['$push'][$fieldName] = array(
87
                '$each' => $values
88
            );
89
        }
90
        
91
        // field name found and has single value
92
        else if(!is_array($this->_operators['$push'][$fieldName]) || !isset($this->_operators['$push'][$fieldName]['$each'])) {
93
            $oldValue = $this->_operators['$push'][$fieldName];
94
            $this->_operators['$push'][$fieldName] = array(
95
                '$each' => array_merge(array($oldValue), $values)
96
            );
97
        }
98
        
99
        // field name found and already $each
100
        else {
101
            $this->_operators['$push'][$fieldName]['$each'] = array_merge(
102
                $this->_operators['$push'][$fieldName]['$each'],
103
                $values
104
            );
105
        }
106
107
        return $this;
108
    }
109
110
    /**
111
     * The $slice modifier limits the number of array elements during a
112
     * $push operation. To project, or return, a specified number of array
113
     * elements from a read operation, see the $slice projection operator instead.
114
     * 
115
     * @link http://docs.mongodb.org/manual/reference/operator/update/slice
116
     * @param string $field
117
     * @param int $slice
118
     * @return \Sokil\Mongo\Operator
119
     * @throws \Sokil\Mongo\Exception
120
     */
121
    public function pushEachSlice($field, $slice)
122
    {
123
        $slice = (int) $slice;
124
        
125
        if(!isset($this->_operators['$push'][$field]['$each'])) {
126
            throw new Exception('Field ' . $field . ' must be pushed wit $each modifier');
127
        }
128
        
129
        $this->_operators['$push'][$field]['$slice'] = $slice;
130
        
131
        return $this;
132
    }
133
134
    /**
135
     * The $sort modifier orders the elements of an array during a $push operation.
136
     *
137
     * @link http://docs.mongodb.org/manual/reference/operator/update/sort
138
     * @param string $field
139
     * @param array $sort
140
     * @return \Sokil\Mongo\Operator
141
     * @throws \Sokil\Mongo\Exception
142
     */
143 View Code Duplication
    public function pushEachSort($field, array $sort)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {        
145
        // add modifiers
146
        if(!$sort) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sort of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
147
            throw new Exception('Sort condition is empty');
148
        }
149
        
150
        if(!isset($this->_operators['$push'][$field]['$each'])) {
151
            throw new Exception('Field ' . $field . ' must be pushed with $each modifier');
152
        }
153
        
154
        $this->_operators['$push'][$field]['$sort'] = $sort;
155
        
156
        return $this;
157
    }
158
159
    /**
160
     * The $position modifier specifies the location in the array at which
161
     * the $push operator insert elements. Without the $position modifier,
162
     * the $push operator inserts elements to the end of the array. See
163
     * $push modifiers for more information.
164
     * 
165
     * @link http://docs.mongodb.org/manual/reference/operator/update/position
166
     * @param string $field
167
     * @param int $position non-negative number that corresponds to the position in the array, based on a zero-based index
168
     * @return \Sokil\Mongo\Operator
169
     * @throws \Sokil\Mongo\Exception
170
     */
171 View Code Duplication
    public function pushEachPosition($field, $position)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        $position = (int) $position;
174
        
175
        // add modifiers
176
        if($position <= 0) {
177
            throw new Exception('Position must be greater 0');
178
        }
179
        
180
        if(!isset($this->_operators['$push'][$field]['$each'])) {
181
            throw new Exception('Field ' . $field . ' must be pushed with $each modifier');
182
        }
183
        
184
        $this->_operators['$push'][$field]['$position'] = $position;
185
        
186
        return $this;
187
    }
188
189
    public function addToSet($field, $value)
190
    {
191
        // new field
192
        if (!isset($this->_operators['$addToSet'][$field])) {
193
            $this->_operators['$addToSet'][$field] = $value;
194
            return $this;
195
        }
196
197
        // scalar value or array in existed field
198 View Code Duplication
        if (!is_array($this->_operators['$addToSet'][$field]) || !isset($this->_operators['$addToSet'][$field]['$each'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
            $this->_operators['$addToSet'][$field] = array(
200
                '$each' => array(
201
                    $this->_operators['$addToSet'][$field],
202
                    $value,
203
                ),
204
            );
205
            return $this;
206
        }
207
208
        // field already $each
209
        $this->_operators['$addToSet'][$field]['$each'][] = $value;
210
211
        return $this;
212
    }
213
214
    public function addToSetEach($field, array $values)
215
    {
216
        // new field
217
        if (!isset($this->_operators['$addToSet'][$field])) {
218
            $this->_operators['$addToSet'][$field]['$each'] = $values;
219
            return $this;
220
        }
221
222
        // scalar value or array in existed field
223 View Code Duplication
        if (!is_array($this->_operators['$addToSet'][$field]) || !isset($this->_operators['$addToSet'][$field]['$each'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
            $this->_operators['$addToSet'][$field] = array(
225
                '$each' => array_merge(
226
                    [$this->_operators['$addToSet'][$field]],
227
                    $values
228
                ),
229
            );
230
            return $this;
231
        }
232
233
        // field already $each
234
        $this->_operators['$addToSet'][$field] = array(
235
            '$each' => array_merge(
236
                $this->_operators['$addToSet'][$field]['$each'],
237
                $values
238
            ),
239
        );
240
241
        return $this;
242
    }
243
244
    public function increment($fieldName, $value = 1)
245
    {
246
        // check if update operations already added
247
        $oldIncrementValue = $this->get('$inc', $fieldName);
248
        if($oldIncrementValue) {
249
            $value = $oldIncrementValue + $value;
250
        }
251
        
252
        $this->_operators['$inc'][$fieldName] = $value;
253
        
254
        return $this;
255
    }
256
257
    /**
258
     * The $pull operator removes from an existing array all instances of a
259
     * value or values that match a specified query.
260
     *
261
     * @link http://docs.mongodb.org/manual/reference/operator/update/pull
262
     * @param integer|string|\Sokil\Mongo\Expression|callable $expression
263
     * @param mixed|\Sokil\Mongo\Expression|callable $value
264
     * @return \Sokil\Mongo\Operator
265
     */
266
    public function pull($expression, $value = null)
267
    {
268
        // field-value pulling
269
        if($value) {
270
271
            // expression
272
            if(is_callable($value)) {
273
                $configurator = $value;
274
                $value = new Expression();
275
                call_user_func($configurator, $value);
276
            }
277
278
            if($value instanceof Expression) {
279
                $value = $value->toArray();
280
            }
281
            
282
            $this->_operators['$pull'][$expression] = $value;
283
            
284
            return $this;
285
        }
286
287
        // expression
288
        if(is_callable($expression)) {
289
            $configurator = $expression;
290
            $expression = new Expression();
291
            call_user_func($configurator, $expression);
292
        }
293
294
        if($expression instanceof Expression) {
295
            $expression = $expression->toArray();
296
        } elseif(!is_array($expression)) {
297
            throw new \InvalidArgumentException('Expression must be field name, callable or Expression object');
298
        }
299
        
300
        if(!isset($this->_operators['$pull'])) {
301
            // no $pull operator found
302
            $this->_operators['$pull'] = $expression;
303
        } else {
304
            // $pull operator found
305
            $this->_operators['$pull'] = array_merge($this->_operators['$pull'], $expression);
306
        }
307
        
308
        return $this;
309
    }
310
311
    /**
312
     * The $unset operator deletes a particular field
313
     * 
314
     * @link http://docs.mongodb.org/manual/reference/operator/update/unset
315
     * @param string $fieldName
316
     * @return \Sokil\Mongo\Operator
317
     */
318
    public function unsetField($fieldName)
319
    {
320
        $this->_operators['$unset'][$fieldName] = '';
321
        return $this;
322
    }
323
    
324
    public function bitwiceAnd($field, $value)
325
    {
326
        $this->_operators['$bit'][$field]['and'] = (int) $value;
327
        return $this;
328
    }
329
    
330
    public function bitwiceOr($field, $value)
331
    {
332
        $this->_operators['$bit'][$field]['or'] = (int) $value;
333
        return $this;
334
    }
335
    
336
    public function bitwiceXor($field, $value)
337
    {
338
        $this->_operators['$bit'][$field]['xor'] = (int) $value;
339
        
340
        return $this;
341
    }
342
    
343
    public function isDefined()
344
    {
345
        return (bool) $this->_operators;
346
    }
347
    
348
    public function reset()
349
    {
350
        $this->_operators = array();
351
        return $this;
352
    }
353
    
354
    public function get($operation, $fieldName = null)
355
    {
356
        if($fieldName) {
357
            return isset($this->_operators[$operation][$fieldName])
358
                ? $this->_operators[$operation][$fieldName]
359
                : null;
360
        }
361
        
362
        return isset($this->_operators[$operation]) 
363
            ? $this->_operators[$operation]
364
            : null;
365
    }
366
367
    /**
368
     * @deprecated since v.1.13 use Operator::toArray()
369
     * @return array
370
     */
371
    public function getAll()
372
    {
373
        return $this->_operators;
374
    }
375
376
    public function toArray()
377
    {
378
        return $this->_operators;
379
    }
380
    
381
    public function isReloadRequired()
382
    {
383
        return isset($this->_operators['$inc']) || isset($this->_operators['$pull']);
384
    }
385
386
    /**
387
     * Transform operator in different formats to canonical array form
388
     *
389
     * @param mixed $mixed
390
     * @return array
391
     * @throws \Sokil\Mongo\Exception
392
     */
393 View Code Duplication
    public static function convertToArray($mixed)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
394
    {
395
        // get operator from callable
396
        if(is_callable($mixed)) {
397
            $callable = $mixed;
398
            $mixed = new self();
399
            call_user_func($callable, $mixed);
400
        }
401
402
        // get operator array
403
        if($mixed instanceof Arrayable && $mixed instanceof self) {
404
            $mixed = $mixed->toArray();
405
        } elseif(!is_array($mixed)) {
406
            throw new Exception('Mixed must be instance of Operator');
407
        }
408
409
        return $mixed;
410
    }
411
}
412