/www/slight

To get this branch, use:
bzr branch http://bzr.ed.am/www/slight

« back to all changes in this revision

Viewing changes to flight/util/Collection.php

  • Committer: Tim Marston
  • Date: 2015-08-05 18:48:29 UTC
  • Revision ID: tim@ed.am-20150805184829-c3qj13d1h89pkb2l
initial commit

Show diffs side-by-side

added added

removed removed

 
1
<?php
 
2
/**
 
3
 * Flight: An extensible micro-framework.
 
4
 *
 
5
 * @copyright   Copyright (c) 2011, Mike Cao <mike@mikecao.com>
 
6
 * @license     MIT, http://flightphp.com/license
 
7
 */
 
8
 
 
9
namespace flight\util;
 
10
 
 
11
/**
 
12
 * The Collection class allows you to access a set of data
 
13
 * using both array and object notation.
 
14
 */
 
15
class Collection implements \ArrayAccess, \Iterator, \Countable {
 
16
    /**
 
17
     * Collection data.
 
18
     *
 
19
     * @var array
 
20
     */
 
21
    private $data;
 
22
 
 
23
    /**
 
24
     * Constructor.
 
25
     *
 
26
     * @param array $data Initial data
 
27
     */
 
28
    public function __construct(array $data = array()) {
 
29
        $this->data = $data;
 
30
    }
 
31
 
 
32
    /**
 
33
     * Gets an item.
 
34
     *
 
35
     * @param string $key Key
 
36
     * @return mixed Value
 
37
     */
 
38
    public function __get($key) {
 
39
        return isset($this->data[$key]) ? $this->data[$key] : null;
 
40
    }
 
41
 
 
42
    /**
 
43
     * Set an item.
 
44
     *
 
45
     * @param string $key Key
 
46
     * @param mixed $value Value
 
47
     */
 
48
    public function __set($key, $value) {
 
49
        $this->data[$key] = $value;
 
50
    }
 
51
 
 
52
    /**
 
53
     * Checks if an item exists.
 
54
     *
 
55
     * @param string $key Key
 
56
     * @return bool Item status
 
57
     */
 
58
    public function __isset($key) {
 
59
        return isset($this->data[$key]);
 
60
    }
 
61
 
 
62
    /**
 
63
     * Removes an item.
 
64
     *
 
65
     * @param string $key Key
 
66
     */
 
67
    public function __unset($key) {
 
68
        unset($this->data[$key]);
 
69
    }
 
70
 
 
71
    /**
 
72
     * Gets an item at the offset.
 
73
     *
 
74
     * @param string $offset Offset
 
75
     * @return mixed Value
 
76
     */
 
77
    public function offsetGet($offset) {
 
78
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
 
79
    }
 
80
 
 
81
    /**
 
82
     * Sets an item at the offset.
 
83
     *
 
84
     * @param string $offset Offset
 
85
     * @param mixed $value Value
 
86
     */
 
87
    public function offsetSet($offset, $value) {
 
88
        if (is_null($offset)) {
 
89
            $this->data[] = $value;
 
90
        }
 
91
        else {
 
92
            $this->data[$offset] = $value;
 
93
        }
 
94
    }
 
95
 
 
96
    /**
 
97
     * Checks if an item exists at the offset.
 
98
     *
 
99
     * @param string $offset Offset
 
100
     * @return bool Item status
 
101
     */
 
102
    public function offsetExists($offset) {
 
103
        return isset($this->data[$offset]);
 
104
    }
 
105
 
 
106
    /**
 
107
     * Removes an item at the offset.
 
108
     *
 
109
     * @param string $offset Offset
 
110
     */
 
111
    public function offsetUnset($offset) {
 
112
        unset($this->data[$offset]);
 
113
    }
 
114
 
 
115
    /**
 
116
     * Resets the collection.
 
117
     */
 
118
    public function rewind() {
 
119
        reset($this->data);
 
120
    }
 
121
 
 
122
    /**
 
123
     * Gets current collection item.
 
124
     *
 
125
     * @return mixed Value
 
126
     */ 
 
127
    public function current() {
 
128
        return current($this->data);
 
129
    }
 
130
 
 
131
    /**
 
132
     * Gets current collection key.
 
133
     *
 
134
     * @return mixed Value
 
135
     */ 
 
136
    public function key() {
 
137
        return key($this->data);
 
138
    }
 
139
 
 
140
    /**
 
141
     * Gets the next collection value.
 
142
     *
 
143
     * @return mixed Value
 
144
     */ 
 
145
    public function next() 
 
146
    {
 
147
        return next($this->data);
 
148
    }
 
149
 
 
150
    /**
 
151
     * Checks if the current collection key is valid.
 
152
     *
 
153
     * @return bool Key status
 
154
     */ 
 
155
    public function valid()
 
156
    {
 
157
        $key = key($this->data);
 
158
        return ($key !== NULL && $key !== FALSE);
 
159
    }
 
160
 
 
161
    /**
 
162
     * Gets the size of the collection.
 
163
     *
 
164
     * @return int Collection size
 
165
     */
 
166
    public function count() {
 
167
        return sizeof($this->data);
 
168
    }
 
169
 
 
170
    /**
 
171
     * Gets the item keys.
 
172
     *
 
173
     * @return array Collection keys
 
174
     */
 
175
    public function keys() {
 
176
        return array_keys($this->data);
 
177
    }
 
178
 
 
179
    /**
 
180
     * Gets the collection data.
 
181
     *
 
182
     * @return array Collection data
 
183
     */
 
184
    public function getData() {
 
185
        return $this->data;
 
186
    }
 
187
 
 
188
    /**
 
189
     * Sets the collection data.
 
190
     *
 
191
     * @param array $data New collection data
 
192
     */
 
193
    public function setData(array $data) {
 
194
        $this->data = $data;
 
195
    }
 
196
 
 
197
    /**
 
198
     * Removes all items from the collection.
 
199
     */
 
200
    public function clear() {
 
201
        $this->data = array();
 
202
    }
 
203
}