Completed
Branch FET/asset-manager (433489)
by
unknown
32:42 queued 18:11
created
core/services/loaders/ObjectIdentifier.php 1 patch
Indentation   +98 added lines, -98 removed lines patch added patch discarded remove patch
@@ -16,113 +16,113 @@
 block discarded – undo
16 16
 class ObjectIdentifier
17 17
 {
18 18
 
19
-    /**
20
-     * used to separate the FQCN from the class's arguments identifier
21
-     */
22
-    const DELIMITER = '____';
19
+	/**
20
+	 * used to separate the FQCN from the class's arguments identifier
21
+	 */
22
+	const DELIMITER = '____';
23 23
 
24
-    /**
25
-     * @var ClassInterfaceCache $class_cache
26
-     */
27
-    private $class_cache;
24
+	/**
25
+	 * @var ClassInterfaceCache $class_cache
26
+	 */
27
+	private $class_cache;
28 28
 
29 29
 
30
-    /**
31
-     * ObjectIdentifier constructor.
32
-     *
33
-     * @param ClassInterfaceCache $class_cache
34
-     */
35
-    public function __construct(ClassInterfaceCache $class_cache)
36
-    {
37
-        $this->class_cache = $class_cache;
38
-    }
30
+	/**
31
+	 * ObjectIdentifier constructor.
32
+	 *
33
+	 * @param ClassInterfaceCache $class_cache
34
+	 */
35
+	public function __construct(ClassInterfaceCache $class_cache)
36
+	{
37
+		$this->class_cache = $class_cache;
38
+	}
39 39
 
40 40
 
41
-    /**
42
-     * Returns true if the supplied $object_identifier contains
43
-     * the delimiter used to separate an fqcn from the arguments hash
44
-     *
45
-     * @param string $object_identifier
46
-     * @return bool
47
-     */
48
-    public function hasArguments($object_identifier)
49
-    {
50
-        // type casting to bool instead of using strpos() !== false
51
-        // because an object identifier should never begin with the delimiter
52
-        // therefore the delimiter should NOT be found at position 0
53
-        return (bool) strpos($object_identifier, ObjectIdentifier::DELIMITER);
54
-    }
41
+	/**
42
+	 * Returns true if the supplied $object_identifier contains
43
+	 * the delimiter used to separate an fqcn from the arguments hash
44
+	 *
45
+	 * @param string $object_identifier
46
+	 * @return bool
47
+	 */
48
+	public function hasArguments($object_identifier)
49
+	{
50
+		// type casting to bool instead of using strpos() !== false
51
+		// because an object identifier should never begin with the delimiter
52
+		// therefore the delimiter should NOT be found at position 0
53
+		return (bool) strpos($object_identifier, ObjectIdentifier::DELIMITER);
54
+	}
55 55
 
56 56
 
57
-    /**
58
-     * Returns true if the supplied FQCN equals the supplied $object_identifier
59
-     * OR the supplied FQCN matches the FQCN portion of the supplied $object_identifier
60
-     * AND that $object_identifier is for an object with arguments.
61
-     * This allows a request for an object using a FQCN to match
62
-     * a previously instantiated object with arguments
63
-     * without having to know those arguments.
64
-     *
65
-     * @param string $fqcn
66
-     * @param string $object_identifier
67
-     * @return bool
68
-     */
69
-    public function fqcnMatchesObjectIdentifier($fqcn, $object_identifier)
70
-    {
71
-        return $fqcn === $object_identifier
72
-               || strpos($object_identifier, $fqcn . ObjectIdentifier::DELIMITER) === 0;
73
-    }
57
+	/**
58
+	 * Returns true if the supplied FQCN equals the supplied $object_identifier
59
+	 * OR the supplied FQCN matches the FQCN portion of the supplied $object_identifier
60
+	 * AND that $object_identifier is for an object with arguments.
61
+	 * This allows a request for an object using a FQCN to match
62
+	 * a previously instantiated object with arguments
63
+	 * without having to know those arguments.
64
+	 *
65
+	 * @param string $fqcn
66
+	 * @param string $object_identifier
67
+	 * @return bool
68
+	 */
69
+	public function fqcnMatchesObjectIdentifier($fqcn, $object_identifier)
70
+	{
71
+		return $fqcn === $object_identifier
72
+			   || strpos($object_identifier, $fqcn . ObjectIdentifier::DELIMITER) === 0;
73
+	}
74 74
 
75 75
 
76
-    /**
77
-     * build a string representation of an object's FQCN and arguments
78
-     *
79
-     * @param string $fqcn
80
-     * @param array  $arguments
81
-     * @return string
82
-     */
83
-    public function getIdentifier($fqcn, array $arguments = array())
84
-    {
85
-        // only build identifier from arguments if class is not ReservedInstanceInterface
86
-        $identifier = ! $this->class_cache->hasInterface(
87
-            $fqcn,
88
-            'EventEspresso\core\interfaces\ReservedInstanceInterface'
89
-        )
90
-            ? $this->getIdentifierForArguments($arguments)
91
-            : '';
92
-        if (! empty($identifier)) {
93
-            $fqcn .= ObjectIdentifier::DELIMITER . md5($identifier);
94
-        }
95
-        return $fqcn;
96
-    }
76
+	/**
77
+	 * build a string representation of an object's FQCN and arguments
78
+	 *
79
+	 * @param string $fqcn
80
+	 * @param array  $arguments
81
+	 * @return string
82
+	 */
83
+	public function getIdentifier($fqcn, array $arguments = array())
84
+	{
85
+		// only build identifier from arguments if class is not ReservedInstanceInterface
86
+		$identifier = ! $this->class_cache->hasInterface(
87
+			$fqcn,
88
+			'EventEspresso\core\interfaces\ReservedInstanceInterface'
89
+		)
90
+			? $this->getIdentifierForArguments($arguments)
91
+			: '';
92
+		if (! empty($identifier)) {
93
+			$fqcn .= ObjectIdentifier::DELIMITER . md5($identifier);
94
+		}
95
+		return $fqcn;
96
+	}
97 97
 
98 98
 
99
-    /**
100
-     * build a string representation of a object's arguments
101
-     * (mostly because Closures can't be serialized)
102
-     *
103
-     * @param array $arguments
104
-     * @return string
105
-     */
106
-    protected function getIdentifierForArguments(array $arguments)
107
-    {
108
-        if (empty($arguments)) {
109
-            return '';
110
-        }
111
-        $identifier = '';
112
-        foreach ($arguments as $argument) {
113
-            switch (true) {
114
-                case is_object($argument):
115
-                case $argument instanceof Closure:
116
-                    $identifier .= spl_object_hash($argument);
117
-                    break;
118
-                case is_array($argument):
119
-                    $identifier .= $this->getIdentifierForArguments($argument);
120
-                    break;
121
-                default:
122
-                    $identifier .= $argument;
123
-                    break;
124
-            }
125
-        }
126
-        return $identifier;
127
-    }
99
+	/**
100
+	 * build a string representation of a object's arguments
101
+	 * (mostly because Closures can't be serialized)
102
+	 *
103
+	 * @param array $arguments
104
+	 * @return string
105
+	 */
106
+	protected function getIdentifierForArguments(array $arguments)
107
+	{
108
+		if (empty($arguments)) {
109
+			return '';
110
+		}
111
+		$identifier = '';
112
+		foreach ($arguments as $argument) {
113
+			switch (true) {
114
+				case is_object($argument):
115
+				case $argument instanceof Closure:
116
+					$identifier .= spl_object_hash($argument);
117
+					break;
118
+				case is_array($argument):
119
+					$identifier .= $this->getIdentifierForArguments($argument);
120
+					break;
121
+				default:
122
+					$identifier .= $argument;
123
+					break;
124
+			}
125
+		}
126
+		return $identifier;
127
+	}
128 128
 }
Please login to merge, or discard this patch.
core/services/collections/Collection.php 2 patches
Indentation   +467 added lines, -467 removed lines patch added patch discarded remove patch
@@ -21,471 +21,471 @@
 block discarded – undo
21 21
 class Collection extends SplObjectStorage implements CollectionInterface
22 22
 {
23 23
 
24
-    /**
25
-     * a unique string for identifying this collection
26
-     *
27
-     * @type string $collection_identifier
28
-     */
29
-    protected $collection_identifier;
30
-
31
-    /**
32
-     * an interface (or class) name to be used for restricting the type of objects added to the storage
33
-     * this should be set from within the child class constructor
34
-     *
35
-     * @type string $interface
36
-     */
37
-    protected $collection_interface;
38
-
39
-
40
-    /**
41
-     * Collection constructor
42
-     *
43
-     * @param string $collection_interface
44
-     * @throws InvalidInterfaceException
45
-     */
46
-    public function __construct($collection_interface)
47
-    {
48
-        $this->setCollectionInterface($collection_interface);
49
-        $this->setCollectionIdentifier();
50
-    }
51
-
52
-
53
-    /**
54
-     * @return string
55
-     */
56
-    public function collectionIdentifier()
57
-    {
58
-        return $this->collection_identifier;
59
-    }
60
-
61
-
62
-    /**
63
-     * creates a very readable unique 9 character identifier like:  CF2-532-DAC
64
-     * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC
65
-     *
66
-     * @return void
67
-     */
68
-    protected function setCollectionIdentifier()
69
-    {
70
-        // hash a few collection details
71
-        $identifier = md5(spl_object_hash($this) . $this->collection_interface . time());
72
-        // grab a few characters from the start, middle, and end of the hash
73
-        $id = array();
74
-        for ($x = 0; $x < 19; $x += 9) {
75
-            $id[] = substr($identifier, $x, 3);
76
-        }
77
-        $identifier = basename(str_replace('\\', '/', get_class($this)));
78
-        $identifier .= '-' . strtoupper(implode('-', $id));
79
-        $this->collection_identifier = $identifier;
80
-    }
81
-
82
-
83
-    /**
84
-     * setCollectionInterface
85
-     *
86
-     * @access protected
87
-     * @param  string $collection_interface
88
-     * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
89
-     */
90
-    protected function setCollectionInterface($collection_interface)
91
-    {
92
-        if (! (interface_exists($collection_interface) || class_exists($collection_interface))) {
93
-            throw new InvalidInterfaceException($collection_interface);
94
-        }
95
-        $this->collection_interface = $collection_interface;
96
-    }
97
-
98
-
99
-    /**
100
-     * add
101
-     * attaches an object to the Collection
102
-     * and sets any supplied data associated with the current iterator entry
103
-     * by calling EE_Object_Collection::set_identifier()
104
-     *
105
-     * @access public
106
-     * @param        $object
107
-     * @param  mixed $identifier
108
-     * @return bool
109
-     * @throws InvalidEntityException
110
-     * @throws DuplicateCollectionIdentifierException
111
-     */
112
-    public function add($object, $identifier = null)
113
-    {
114
-        if (! $object instanceof $this->collection_interface) {
115
-            throw new InvalidEntityException($object, $this->collection_interface);
116
-        }
117
-        if ($this->contains($object)) {
118
-            throw new DuplicateCollectionIdentifierException($identifier);
119
-        }
120
-        $this->attach($object);
121
-        $this->setIdentifier($object, $identifier);
122
-        return $this->contains($object);
123
-    }
124
-
125
-
126
-    /**
127
-     * setIdentifier
128
-     * Sets the data associated with an object in the Collection
129
-     * if no $identifier is supplied, then the spl_object_hash() is used
130
-     *
131
-     * @access public
132
-     * @param        $object
133
-     * @param  mixed $identifier
134
-     * @return bool
135
-     */
136
-    public function setIdentifier($object, $identifier = null)
137
-    {
138
-        $identifier = ! empty($identifier)
139
-            ? $identifier
140
-            : spl_object_hash($object);
141
-        $this->rewind();
142
-        while ($this->valid()) {
143
-            if ($object === $this->current()) {
144
-                $this->setInfo($identifier);
145
-                $this->rewind();
146
-                return true;
147
-            }
148
-            $this->next();
149
-        }
150
-        return false;
151
-    }
152
-
153
-
154
-    /**
155
-     * get
156
-     * finds and returns an object in the Collection based on the identifier that was set using addObject()
157
-     * PLZ NOTE: the pointer is reset to the beginning of the collection before returning
158
-     *
159
-     * @access public
160
-     * @param mixed $identifier
161
-     * @return mixed
162
-     */
163
-    public function get($identifier)
164
-    {
165
-        $this->rewind();
166
-        while ($this->valid()) {
167
-            if ($identifier === $this->getInfo()) {
168
-                $object = $this->current();
169
-                $this->rewind();
170
-                return $object;
171
-            }
172
-            $this->next();
173
-        }
174
-        return null;
175
-    }
176
-
177
-
178
-    /**
179
-     * has
180
-     * returns TRUE or FALSE
181
-     * depending on whether the object is within the Collection
182
-     * based on the supplied $identifier
183
-     *
184
-     * @access public
185
-     * @param  mixed $identifier
186
-     * @return bool
187
-     */
188
-    public function has($identifier)
189
-    {
190
-        $this->rewind();
191
-        while ($this->valid()) {
192
-            if ($identifier === $this->getInfo()) {
193
-                $this->rewind();
194
-                return true;
195
-            }
196
-            $this->next();
197
-        }
198
-        return false;
199
-    }
200
-
201
-
202
-    /**
203
-     * hasObject
204
-     * returns TRUE or FALSE depending on whether the supplied object is within the Collection
205
-     *
206
-     * @access public
207
-     * @param $object
208
-     * @return bool
209
-     */
210
-    public function hasObject($object)
211
-    {
212
-        return $this->contains($object);
213
-    }
214
-
215
-
216
-    /**
217
-     * hasObjects
218
-     * returns true if there are objects within the Collection, and false if it is empty
219
-     *
220
-     * @access public
221
-     * @return bool
222
-     */
223
-    public function hasObjects()
224
-    {
225
-        return $this->count() !== 0;
226
-    }
227
-
228
-
229
-    /**
230
-     * isEmpty
231
-     * returns true if there are no objects within the Collection, and false if there are
232
-     *
233
-     * @access public
234
-     * @return bool
235
-     */
236
-    public function isEmpty()
237
-    {
238
-        return $this->count() === 0;
239
-    }
240
-
241
-
242
-    /**
243
-     * remove
244
-     * detaches an object from the Collection
245
-     *
246
-     * @access public
247
-     * @param $object
248
-     * @return bool
249
-     */
250
-    public function remove($object)
251
-    {
252
-        $this->detach($object);
253
-        return true;
254
-    }
255
-
256
-
257
-    /**
258
-     * setCurrent
259
-     * advances pointer to the object whose identifier matches that which was provided
260
-     *
261
-     * @access public
262
-     * @param mixed $identifier
263
-     * @return boolean
264
-     */
265
-    public function setCurrent($identifier)
266
-    {
267
-        $this->rewind();
268
-        while ($this->valid()) {
269
-            if ($identifier === $this->getInfo()) {
270
-                return true;
271
-            }
272
-            $this->next();
273
-        }
274
-        return false;
275
-    }
276
-
277
-
278
-    /**
279
-     * setCurrentUsingObject
280
-     * advances pointer to the provided object
281
-     *
282
-     * @access public
283
-     * @param $object
284
-     * @return boolean
285
-     */
286
-    public function setCurrentUsingObject($object)
287
-    {
288
-        $this->rewind();
289
-        while ($this->valid()) {
290
-            if ($this->current() === $object) {
291
-                return true;
292
-            }
293
-            $this->next();
294
-        }
295
-        return false;
296
-    }
297
-
298
-
299
-    /**
300
-     * Returns the object occupying the index before the current object,
301
-     * unless this is already the first object, in which case it just returns the first object
302
-     *
303
-     * @return mixed
304
-     */
305
-    public function previous()
306
-    {
307
-        $index = $this->indexOf($this->current());
308
-        if ($index === 0) {
309
-            return $this->current();
310
-        }
311
-        $index--;
312
-        return $this->objectAtIndex($index);
313
-    }
314
-
315
-
316
-    /**
317
-     * Returns the index of a given object, or false if not found
318
-     *
319
-     * @see http://stackoverflow.com/a/8736013
320
-     * @param $object
321
-     * @return boolean|int|string
322
-     */
323
-    public function indexOf($object)
324
-    {
325
-        if (! $this->contains($object)) {
326
-            return false;
327
-        }
328
-        foreach ($this as $index => $obj) {
329
-            if ($obj === $object) {
330
-                return $index;
331
-            }
332
-        }
333
-        return false;
334
-    }
335
-
336
-
337
-    /**
338
-     * Returns the object at the given index
339
-     *
340
-     * @see http://stackoverflow.com/a/8736013
341
-     * @param int $index
342
-     * @return mixed
343
-     */
344
-    public function objectAtIndex($index)
345
-    {
346
-        $iterator = new LimitIterator($this, $index, 1);
347
-        $iterator->rewind();
348
-        return $iterator->current();
349
-    }
350
-
351
-
352
-    /**
353
-     * Returns the sequence of objects as specified by the offset and length
354
-     *
355
-     * @see http://stackoverflow.com/a/8736013
356
-     * @param int $offset
357
-     * @param int $length
358
-     * @return array
359
-     */
360
-    public function slice($offset, $length)
361
-    {
362
-        $slice = array();
363
-        $iterator = new LimitIterator($this, $offset, $length);
364
-        foreach ($iterator as $object) {
365
-            $slice[] = $object;
366
-        }
367
-        return $slice;
368
-    }
369
-
370
-
371
-    /**
372
-     * Inserts an object at a certain point
373
-     *
374
-     * @see http://stackoverflow.com/a/8736013
375
-     * @param mixed $object A single object
376
-     * @param int   $index
377
-     * @param mixed $identifier
378
-     * @return bool
379
-     * @throws DuplicateCollectionIdentifierException
380
-     * @throws InvalidEntityException
381
-     */
382
-    public function insertObjectAt($object, $index, $identifier = null)
383
-    {
384
-        // check to ensure that objects don't already exist in the collection
385
-        if ($this->has($identifier)) {
386
-            throw new DuplicateCollectionIdentifierException($identifier);
387
-        }
388
-        // detach any objects at or past this index
389
-        $remaining_objects = array();
390
-        if ($index < $this->count()) {
391
-            $remaining_objects = $this->slice($index, $this->count() - $index);
392
-            foreach ($remaining_objects as $key => $remaining_object) {
393
-                // we need to grab the identifiers for each object and use them as keys
394
-                $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object;
395
-                // and then remove the object from the current tracking array
396
-                unset($remaining_objects[ $key ]);
397
-                // and then remove it from the Collection
398
-                $this->detach($remaining_object);
399
-            }
400
-        }
401
-        // add the new object we're splicing in
402
-        $this->add($object, $identifier);
403
-        // attach the objects we previously detached
404
-        foreach ($remaining_objects as $key => $remaining_object) {
405
-            $this->add($remaining_object, $key);
406
-        }
407
-        return $this->contains($object);
408
-    }
409
-
410
-
411
-    /**
412
-     * Inserts an object (or an array of objects) at a certain point
413
-     *
414
-     * @see http://stackoverflow.com/a/8736013
415
-     * @param mixed $objects A single object or an array of objects
416
-     * @param int   $index
417
-     */
418
-    public function insertAt($objects, $index)
419
-    {
420
-        if (! is_array($objects)) {
421
-            $objects = array($objects);
422
-        }
423
-        // check to ensure that objects don't already exist in the collection
424
-        foreach ($objects as $key => $object) {
425
-            if ($this->contains($object)) {
426
-                unset($objects[ $key ]);
427
-            }
428
-        }
429
-        // do we have any objects left?
430
-        if (! $objects) {
431
-            return;
432
-        }
433
-        // detach any objects at or past this index
434
-        $remaining = array();
435
-        if ($index < $this->count()) {
436
-            $remaining = $this->slice($index, $this->count() - $index);
437
-            foreach ($remaining as $object) {
438
-                $this->detach($object);
439
-            }
440
-        }
441
-        // add the new objects we're splicing in
442
-        foreach ($objects as $object) {
443
-            $this->attach($object);
444
-        }
445
-        // attach the objects we previously detached
446
-        foreach ($remaining as $object) {
447
-            $this->attach($object);
448
-        }
449
-    }
450
-
451
-
452
-    /**
453
-     * Removes the object at the given index
454
-     *
455
-     * @see http://stackoverflow.com/a/8736013
456
-     * @param int $index
457
-     */
458
-    public function removeAt($index)
459
-    {
460
-        $this->detach($this->objectAtIndex($index));
461
-    }
462
-
463
-
464
-    /**
465
-     * detaches ALL objects from the Collection
466
-     */
467
-    public function detachAll()
468
-    {
469
-        $this->rewind();
470
-        while ($this->valid()) {
471
-            $object = $this->current();
472
-            $this->next();
473
-            $this->detach($object);
474
-        }
475
-    }
476
-
477
-
478
-    /**
479
-     * unsets and detaches ALL objects from the Collection
480
-     */
481
-    public function trashAndDetachAll()
482
-    {
483
-        $this->rewind();
484
-        while ($this->valid()) {
485
-            $object = $this->current();
486
-            $this->next();
487
-            $this->detach($object);
488
-            unset($object);
489
-        }
490
-    }
24
+	/**
25
+	 * a unique string for identifying this collection
26
+	 *
27
+	 * @type string $collection_identifier
28
+	 */
29
+	protected $collection_identifier;
30
+
31
+	/**
32
+	 * an interface (or class) name to be used for restricting the type of objects added to the storage
33
+	 * this should be set from within the child class constructor
34
+	 *
35
+	 * @type string $interface
36
+	 */
37
+	protected $collection_interface;
38
+
39
+
40
+	/**
41
+	 * Collection constructor
42
+	 *
43
+	 * @param string $collection_interface
44
+	 * @throws InvalidInterfaceException
45
+	 */
46
+	public function __construct($collection_interface)
47
+	{
48
+		$this->setCollectionInterface($collection_interface);
49
+		$this->setCollectionIdentifier();
50
+	}
51
+
52
+
53
+	/**
54
+	 * @return string
55
+	 */
56
+	public function collectionIdentifier()
57
+	{
58
+		return $this->collection_identifier;
59
+	}
60
+
61
+
62
+	/**
63
+	 * creates a very readable unique 9 character identifier like:  CF2-532-DAC
64
+	 * and appends it to the non-qualified class name, ex: ThingCollection-CF2-532-DAC
65
+	 *
66
+	 * @return void
67
+	 */
68
+	protected function setCollectionIdentifier()
69
+	{
70
+		// hash a few collection details
71
+		$identifier = md5(spl_object_hash($this) . $this->collection_interface . time());
72
+		// grab a few characters from the start, middle, and end of the hash
73
+		$id = array();
74
+		for ($x = 0; $x < 19; $x += 9) {
75
+			$id[] = substr($identifier, $x, 3);
76
+		}
77
+		$identifier = basename(str_replace('\\', '/', get_class($this)));
78
+		$identifier .= '-' . strtoupper(implode('-', $id));
79
+		$this->collection_identifier = $identifier;
80
+	}
81
+
82
+
83
+	/**
84
+	 * setCollectionInterface
85
+	 *
86
+	 * @access protected
87
+	 * @param  string $collection_interface
88
+	 * @throws \EventEspresso\core\exceptions\InvalidInterfaceException
89
+	 */
90
+	protected function setCollectionInterface($collection_interface)
91
+	{
92
+		if (! (interface_exists($collection_interface) || class_exists($collection_interface))) {
93
+			throw new InvalidInterfaceException($collection_interface);
94
+		}
95
+		$this->collection_interface = $collection_interface;
96
+	}
97
+
98
+
99
+	/**
100
+	 * add
101
+	 * attaches an object to the Collection
102
+	 * and sets any supplied data associated with the current iterator entry
103
+	 * by calling EE_Object_Collection::set_identifier()
104
+	 *
105
+	 * @access public
106
+	 * @param        $object
107
+	 * @param  mixed $identifier
108
+	 * @return bool
109
+	 * @throws InvalidEntityException
110
+	 * @throws DuplicateCollectionIdentifierException
111
+	 */
112
+	public function add($object, $identifier = null)
113
+	{
114
+		if (! $object instanceof $this->collection_interface) {
115
+			throw new InvalidEntityException($object, $this->collection_interface);
116
+		}
117
+		if ($this->contains($object)) {
118
+			throw new DuplicateCollectionIdentifierException($identifier);
119
+		}
120
+		$this->attach($object);
121
+		$this->setIdentifier($object, $identifier);
122
+		return $this->contains($object);
123
+	}
124
+
125
+
126
+	/**
127
+	 * setIdentifier
128
+	 * Sets the data associated with an object in the Collection
129
+	 * if no $identifier is supplied, then the spl_object_hash() is used
130
+	 *
131
+	 * @access public
132
+	 * @param        $object
133
+	 * @param  mixed $identifier
134
+	 * @return bool
135
+	 */
136
+	public function setIdentifier($object, $identifier = null)
137
+	{
138
+		$identifier = ! empty($identifier)
139
+			? $identifier
140
+			: spl_object_hash($object);
141
+		$this->rewind();
142
+		while ($this->valid()) {
143
+			if ($object === $this->current()) {
144
+				$this->setInfo($identifier);
145
+				$this->rewind();
146
+				return true;
147
+			}
148
+			$this->next();
149
+		}
150
+		return false;
151
+	}
152
+
153
+
154
+	/**
155
+	 * get
156
+	 * finds and returns an object in the Collection based on the identifier that was set using addObject()
157
+	 * PLZ NOTE: the pointer is reset to the beginning of the collection before returning
158
+	 *
159
+	 * @access public
160
+	 * @param mixed $identifier
161
+	 * @return mixed
162
+	 */
163
+	public function get($identifier)
164
+	{
165
+		$this->rewind();
166
+		while ($this->valid()) {
167
+			if ($identifier === $this->getInfo()) {
168
+				$object = $this->current();
169
+				$this->rewind();
170
+				return $object;
171
+			}
172
+			$this->next();
173
+		}
174
+		return null;
175
+	}
176
+
177
+
178
+	/**
179
+	 * has
180
+	 * returns TRUE or FALSE
181
+	 * depending on whether the object is within the Collection
182
+	 * based on the supplied $identifier
183
+	 *
184
+	 * @access public
185
+	 * @param  mixed $identifier
186
+	 * @return bool
187
+	 */
188
+	public function has($identifier)
189
+	{
190
+		$this->rewind();
191
+		while ($this->valid()) {
192
+			if ($identifier === $this->getInfo()) {
193
+				$this->rewind();
194
+				return true;
195
+			}
196
+			$this->next();
197
+		}
198
+		return false;
199
+	}
200
+
201
+
202
+	/**
203
+	 * hasObject
204
+	 * returns TRUE or FALSE depending on whether the supplied object is within the Collection
205
+	 *
206
+	 * @access public
207
+	 * @param $object
208
+	 * @return bool
209
+	 */
210
+	public function hasObject($object)
211
+	{
212
+		return $this->contains($object);
213
+	}
214
+
215
+
216
+	/**
217
+	 * hasObjects
218
+	 * returns true if there are objects within the Collection, and false if it is empty
219
+	 *
220
+	 * @access public
221
+	 * @return bool
222
+	 */
223
+	public function hasObjects()
224
+	{
225
+		return $this->count() !== 0;
226
+	}
227
+
228
+
229
+	/**
230
+	 * isEmpty
231
+	 * returns true if there are no objects within the Collection, and false if there are
232
+	 *
233
+	 * @access public
234
+	 * @return bool
235
+	 */
236
+	public function isEmpty()
237
+	{
238
+		return $this->count() === 0;
239
+	}
240
+
241
+
242
+	/**
243
+	 * remove
244
+	 * detaches an object from the Collection
245
+	 *
246
+	 * @access public
247
+	 * @param $object
248
+	 * @return bool
249
+	 */
250
+	public function remove($object)
251
+	{
252
+		$this->detach($object);
253
+		return true;
254
+	}
255
+
256
+
257
+	/**
258
+	 * setCurrent
259
+	 * advances pointer to the object whose identifier matches that which was provided
260
+	 *
261
+	 * @access public
262
+	 * @param mixed $identifier
263
+	 * @return boolean
264
+	 */
265
+	public function setCurrent($identifier)
266
+	{
267
+		$this->rewind();
268
+		while ($this->valid()) {
269
+			if ($identifier === $this->getInfo()) {
270
+				return true;
271
+			}
272
+			$this->next();
273
+		}
274
+		return false;
275
+	}
276
+
277
+
278
+	/**
279
+	 * setCurrentUsingObject
280
+	 * advances pointer to the provided object
281
+	 *
282
+	 * @access public
283
+	 * @param $object
284
+	 * @return boolean
285
+	 */
286
+	public function setCurrentUsingObject($object)
287
+	{
288
+		$this->rewind();
289
+		while ($this->valid()) {
290
+			if ($this->current() === $object) {
291
+				return true;
292
+			}
293
+			$this->next();
294
+		}
295
+		return false;
296
+	}
297
+
298
+
299
+	/**
300
+	 * Returns the object occupying the index before the current object,
301
+	 * unless this is already the first object, in which case it just returns the first object
302
+	 *
303
+	 * @return mixed
304
+	 */
305
+	public function previous()
306
+	{
307
+		$index = $this->indexOf($this->current());
308
+		if ($index === 0) {
309
+			return $this->current();
310
+		}
311
+		$index--;
312
+		return $this->objectAtIndex($index);
313
+	}
314
+
315
+
316
+	/**
317
+	 * Returns the index of a given object, or false if not found
318
+	 *
319
+	 * @see http://stackoverflow.com/a/8736013
320
+	 * @param $object
321
+	 * @return boolean|int|string
322
+	 */
323
+	public function indexOf($object)
324
+	{
325
+		if (! $this->contains($object)) {
326
+			return false;
327
+		}
328
+		foreach ($this as $index => $obj) {
329
+			if ($obj === $object) {
330
+				return $index;
331
+			}
332
+		}
333
+		return false;
334
+	}
335
+
336
+
337
+	/**
338
+	 * Returns the object at the given index
339
+	 *
340
+	 * @see http://stackoverflow.com/a/8736013
341
+	 * @param int $index
342
+	 * @return mixed
343
+	 */
344
+	public function objectAtIndex($index)
345
+	{
346
+		$iterator = new LimitIterator($this, $index, 1);
347
+		$iterator->rewind();
348
+		return $iterator->current();
349
+	}
350
+
351
+
352
+	/**
353
+	 * Returns the sequence of objects as specified by the offset and length
354
+	 *
355
+	 * @see http://stackoverflow.com/a/8736013
356
+	 * @param int $offset
357
+	 * @param int $length
358
+	 * @return array
359
+	 */
360
+	public function slice($offset, $length)
361
+	{
362
+		$slice = array();
363
+		$iterator = new LimitIterator($this, $offset, $length);
364
+		foreach ($iterator as $object) {
365
+			$slice[] = $object;
366
+		}
367
+		return $slice;
368
+	}
369
+
370
+
371
+	/**
372
+	 * Inserts an object at a certain point
373
+	 *
374
+	 * @see http://stackoverflow.com/a/8736013
375
+	 * @param mixed $object A single object
376
+	 * @param int   $index
377
+	 * @param mixed $identifier
378
+	 * @return bool
379
+	 * @throws DuplicateCollectionIdentifierException
380
+	 * @throws InvalidEntityException
381
+	 */
382
+	public function insertObjectAt($object, $index, $identifier = null)
383
+	{
384
+		// check to ensure that objects don't already exist in the collection
385
+		if ($this->has($identifier)) {
386
+			throw new DuplicateCollectionIdentifierException($identifier);
387
+		}
388
+		// detach any objects at or past this index
389
+		$remaining_objects = array();
390
+		if ($index < $this->count()) {
391
+			$remaining_objects = $this->slice($index, $this->count() - $index);
392
+			foreach ($remaining_objects as $key => $remaining_object) {
393
+				// we need to grab the identifiers for each object and use them as keys
394
+				$remaining_objects[ $remaining_object->getInfo() ] = $remaining_object;
395
+				// and then remove the object from the current tracking array
396
+				unset($remaining_objects[ $key ]);
397
+				// and then remove it from the Collection
398
+				$this->detach($remaining_object);
399
+			}
400
+		}
401
+		// add the new object we're splicing in
402
+		$this->add($object, $identifier);
403
+		// attach the objects we previously detached
404
+		foreach ($remaining_objects as $key => $remaining_object) {
405
+			$this->add($remaining_object, $key);
406
+		}
407
+		return $this->contains($object);
408
+	}
409
+
410
+
411
+	/**
412
+	 * Inserts an object (or an array of objects) at a certain point
413
+	 *
414
+	 * @see http://stackoverflow.com/a/8736013
415
+	 * @param mixed $objects A single object or an array of objects
416
+	 * @param int   $index
417
+	 */
418
+	public function insertAt($objects, $index)
419
+	{
420
+		if (! is_array($objects)) {
421
+			$objects = array($objects);
422
+		}
423
+		// check to ensure that objects don't already exist in the collection
424
+		foreach ($objects as $key => $object) {
425
+			if ($this->contains($object)) {
426
+				unset($objects[ $key ]);
427
+			}
428
+		}
429
+		// do we have any objects left?
430
+		if (! $objects) {
431
+			return;
432
+		}
433
+		// detach any objects at or past this index
434
+		$remaining = array();
435
+		if ($index < $this->count()) {
436
+			$remaining = $this->slice($index, $this->count() - $index);
437
+			foreach ($remaining as $object) {
438
+				$this->detach($object);
439
+			}
440
+		}
441
+		// add the new objects we're splicing in
442
+		foreach ($objects as $object) {
443
+			$this->attach($object);
444
+		}
445
+		// attach the objects we previously detached
446
+		foreach ($remaining as $object) {
447
+			$this->attach($object);
448
+		}
449
+	}
450
+
451
+
452
+	/**
453
+	 * Removes the object at the given index
454
+	 *
455
+	 * @see http://stackoverflow.com/a/8736013
456
+	 * @param int $index
457
+	 */
458
+	public function removeAt($index)
459
+	{
460
+		$this->detach($this->objectAtIndex($index));
461
+	}
462
+
463
+
464
+	/**
465
+	 * detaches ALL objects from the Collection
466
+	 */
467
+	public function detachAll()
468
+	{
469
+		$this->rewind();
470
+		while ($this->valid()) {
471
+			$object = $this->current();
472
+			$this->next();
473
+			$this->detach($object);
474
+		}
475
+	}
476
+
477
+
478
+	/**
479
+	 * unsets and detaches ALL objects from the Collection
480
+	 */
481
+	public function trashAndDetachAll()
482
+	{
483
+		$this->rewind();
484
+		while ($this->valid()) {
485
+			$object = $this->current();
486
+			$this->next();
487
+			$this->detach($object);
488
+			unset($object);
489
+		}
490
+	}
491 491
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -68,14 +68,14 @@  discard block
 block discarded – undo
68 68
     protected function setCollectionIdentifier()
69 69
     {
70 70
         // hash a few collection details
71
-        $identifier = md5(spl_object_hash($this) . $this->collection_interface . time());
71
+        $identifier = md5(spl_object_hash($this).$this->collection_interface.time());
72 72
         // grab a few characters from the start, middle, and end of the hash
73 73
         $id = array();
74 74
         for ($x = 0; $x < 19; $x += 9) {
75 75
             $id[] = substr($identifier, $x, 3);
76 76
         }
77 77
         $identifier = basename(str_replace('\\', '/', get_class($this)));
78
-        $identifier .= '-' . strtoupper(implode('-', $id));
78
+        $identifier .= '-'.strtoupper(implode('-', $id));
79 79
         $this->collection_identifier = $identifier;
80 80
     }
81 81
 
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
      */
90 90
     protected function setCollectionInterface($collection_interface)
91 91
     {
92
-        if (! (interface_exists($collection_interface) || class_exists($collection_interface))) {
92
+        if ( ! (interface_exists($collection_interface) || class_exists($collection_interface))) {
93 93
             throw new InvalidInterfaceException($collection_interface);
94 94
         }
95 95
         $this->collection_interface = $collection_interface;
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
      */
112 112
     public function add($object, $identifier = null)
113 113
     {
114
-        if (! $object instanceof $this->collection_interface) {
114
+        if ( ! $object instanceof $this->collection_interface) {
115 115
             throw new InvalidEntityException($object, $this->collection_interface);
116 116
         }
117 117
         if ($this->contains($object)) {
@@ -322,7 +322,7 @@  discard block
 block discarded – undo
322 322
      */
323 323
     public function indexOf($object)
324 324
     {
325
-        if (! $this->contains($object)) {
325
+        if ( ! $this->contains($object)) {
326 326
             return false;
327 327
         }
328 328
         foreach ($this as $index => $obj) {
@@ -391,9 +391,9 @@  discard block
 block discarded – undo
391 391
             $remaining_objects = $this->slice($index, $this->count() - $index);
392 392
             foreach ($remaining_objects as $key => $remaining_object) {
393 393
                 // we need to grab the identifiers for each object and use them as keys
394
-                $remaining_objects[ $remaining_object->getInfo() ] = $remaining_object;
394
+                $remaining_objects[$remaining_object->getInfo()] = $remaining_object;
395 395
                 // and then remove the object from the current tracking array
396
-                unset($remaining_objects[ $key ]);
396
+                unset($remaining_objects[$key]);
397 397
                 // and then remove it from the Collection
398 398
                 $this->detach($remaining_object);
399 399
             }
@@ -417,17 +417,17 @@  discard block
 block discarded – undo
417 417
      */
418 418
     public function insertAt($objects, $index)
419 419
     {
420
-        if (! is_array($objects)) {
420
+        if ( ! is_array($objects)) {
421 421
             $objects = array($objects);
422 422
         }
423 423
         // check to ensure that objects don't already exist in the collection
424 424
         foreach ($objects as $key => $object) {
425 425
             if ($this->contains($object)) {
426
-                unset($objects[ $key ]);
426
+                unset($objects[$key]);
427 427
             }
428 428
         }
429 429
         // do we have any objects left?
430
-        if (! $objects) {
430
+        if ( ! $objects) {
431 431
             return;
432 432
         }
433 433
         // detach any objects at or past this index
Please login to merge, or discard this patch.
espresso.php 1 patch
Indentation   +79 added lines, -79 removed lines patch added patch discarded remove patch
@@ -38,103 +38,103 @@
 block discarded – undo
38 38
  * @since       4.0
39 39
  */
40 40
 if (function_exists('espresso_version')) {
41
-    if (! function_exists('espresso_duplicate_plugin_error')) {
42
-        /**
43
-         *    espresso_duplicate_plugin_error
44
-         *    displays if more than one version of EE is activated at the same time
45
-         */
46
-        function espresso_duplicate_plugin_error()
47
-        {
48
-            ?>
41
+	if (! function_exists('espresso_duplicate_plugin_error')) {
42
+		/**
43
+		 *    espresso_duplicate_plugin_error
44
+		 *    displays if more than one version of EE is activated at the same time
45
+		 */
46
+		function espresso_duplicate_plugin_error()
47
+		{
48
+			?>
49 49
             <div class="error">
50 50
                 <p>
51 51
                     <?php
52
-                    echo esc_html__(
53
-                        'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
-                        'event_espresso'
55
-                    ); ?>
52
+					echo esc_html__(
53
+						'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
+						'event_espresso'
55
+					); ?>
56 56
                 </p>
57 57
             </div>
58 58
             <?php
59
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
60
-        }
61
-    }
62
-    add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
59
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
60
+		}
61
+	}
62
+	add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
63 63
 
64 64
 } else {
65
-    define('EE_MIN_PHP_VER_REQUIRED', '5.4.0');
66
-    if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
67
-        /**
68
-         * espresso_minimum_php_version_error
69
-         * @return void
70
-         */
71
-        function espresso_minimum_php_version_error()
72
-        {
73
-            ?>
65
+	define('EE_MIN_PHP_VER_REQUIRED', '5.4.0');
66
+	if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
67
+		/**
68
+		 * espresso_minimum_php_version_error
69
+		 * @return void
70
+		 */
71
+		function espresso_minimum_php_version_error()
72
+		{
73
+			?>
74 74
             <div class="error">
75 75
                 <p>
76 76
                     <?php
77
-                    printf(
78
-                        esc_html__(
79
-                            'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
-                            'event_espresso'
81
-                        ),
82
-                        EE_MIN_PHP_VER_REQUIRED,
83
-                        PHP_VERSION,
84
-                        '<br/>',
85
-                        '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
-                    );
87
-                    ?>
77
+					printf(
78
+						esc_html__(
79
+							'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
+							'event_espresso'
81
+						),
82
+						EE_MIN_PHP_VER_REQUIRED,
83
+						PHP_VERSION,
84
+						'<br/>',
85
+						'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
+					);
87
+					?>
88 88
                 </p>
89 89
             </div>
90 90
             <?php
91
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
92
-        }
91
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
92
+		}
93 93
 
94
-        add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
-    } else {
96
-        define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
-        /**
98
-         * espresso_version
99
-         * Returns the plugin version
100
-         *
101
-         * @return string
102
-         */
103
-        function espresso_version()
104
-        {
105
-            return apply_filters('FHEE__espresso__espresso_version', '4.9.62.rc.033');
106
-        }
94
+		add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
+	} else {
96
+		define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
+		/**
98
+		 * espresso_version
99
+		 * Returns the plugin version
100
+		 *
101
+		 * @return string
102
+		 */
103
+		function espresso_version()
104
+		{
105
+			return apply_filters('FHEE__espresso__espresso_version', '4.9.62.rc.033');
106
+		}
107 107
 
108
-        /**
109
-         * espresso_plugin_activation
110
-         * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
-         */
112
-        function espresso_plugin_activation()
113
-        {
114
-            update_option('ee_espresso_activation', true);
115
-        }
108
+		/**
109
+		 * espresso_plugin_activation
110
+		 * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
+		 */
112
+		function espresso_plugin_activation()
113
+		{
114
+			update_option('ee_espresso_activation', true);
115
+		}
116 116
 
117
-        register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
117
+		register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
118 118
 
119
-        require_once __DIR__ . '/core/bootstrap_espresso.php';
120
-        bootstrap_espresso();
121
-    }
119
+		require_once __DIR__ . '/core/bootstrap_espresso.php';
120
+		bootstrap_espresso();
121
+	}
122 122
 }
123 123
 if (! function_exists('espresso_deactivate_plugin')) {
124
-    /**
125
-     *    deactivate_plugin
126
-     * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
-     *
128
-     * @access public
129
-     * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
-     * @return    void
131
-     */
132
-    function espresso_deactivate_plugin($plugin_basename = '')
133
-    {
134
-        if (! function_exists('deactivate_plugins')) {
135
-            require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
-        }
137
-        unset($_GET['activate'], $_REQUEST['activate']);
138
-        deactivate_plugins($plugin_basename);
139
-    }
124
+	/**
125
+	 *    deactivate_plugin
126
+	 * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
+	 *
128
+	 * @access public
129
+	 * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
+	 * @return    void
131
+	 */
132
+	function espresso_deactivate_plugin($plugin_basename = '')
133
+	{
134
+		if (! function_exists('deactivate_plugins')) {
135
+			require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
+		}
137
+		unset($_GET['activate'], $_REQUEST['activate']);
138
+		deactivate_plugins($plugin_basename);
139
+	}
140 140
 }
Please login to merge, or discard this patch.