1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace GV; |
3
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
6
|
|
|
die(); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A generic Collection base class. |
11
|
|
|
*/ |
12
|
|
|
class Collection { |
13
|
|
|
/** |
14
|
|
|
* @var array Main storage for objects in this collection. |
15
|
|
|
*/ |
16
|
|
|
private $storage = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Add an object to this collection. |
20
|
|
|
* |
21
|
|
|
* @param mixed $value The object to be added. |
22
|
|
|
* |
23
|
|
|
* @api |
24
|
|
|
* @since 2.0 |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
153 |
|
public function add( $value ) { |
28
|
153 |
|
$this->storage []= $value; |
|
|
|
|
29
|
153 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Clear this collection. |
33
|
|
|
* |
34
|
|
|
* @api |
35
|
|
|
* @since 2.0 |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
19 |
|
public function clear() { |
39
|
19 |
|
$this->count() && ( $this->storage = array() ); |
40
|
19 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Merge another collection into here. |
44
|
|
|
* |
45
|
|
|
* @param \GV\Collection $collection The collection to be merged. |
46
|
|
|
* |
47
|
|
|
* @api |
48
|
|
|
* @since 2.0 |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
2 |
|
public function merge( \GV\Collection $collection ) { |
52
|
2 |
|
array_map( array( $this, 'add'), $collection->all() ); |
|
|
|
|
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns all the objects in this collection as an an array. |
57
|
|
|
* |
58
|
|
|
* @api |
59
|
|
|
* @since 2.0 |
60
|
|
|
* @return array The objects in this collection. |
61
|
|
|
*/ |
62
|
153 |
|
public function all() { |
63
|
153 |
|
return $this->storage; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the last added object. |
68
|
|
|
* |
69
|
|
|
* @api |
70
|
|
|
* @since 2.0 |
71
|
|
|
* @return mixed|null The last item in here, or null if there are none. |
72
|
|
|
*/ |
73
|
1 |
|
public function last() { |
74
|
1 |
|
return end( $this->storage ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the first added object. |
79
|
|
|
* |
80
|
|
|
* @api |
81
|
|
|
* @since 2.0 |
82
|
|
|
* @return mixed|null The first item in here, or null if there are none. |
83
|
|
|
*/ |
84
|
4 |
|
public function first() { |
85
|
4 |
|
return reset( $this->storage ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the count of the objects in this collection. |
90
|
|
|
* |
91
|
|
|
* @api |
92
|
|
|
* @since 2.0 |
93
|
|
|
* @return int The size of this collection. |
94
|
|
|
*/ |
95
|
157 |
|
public function count() { |
96
|
157 |
|
return count( $this->storage ); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.