root/trunk/src/main/php/net/stubbles/lang/stubArrayAccessor.php

Revision 1661, 3.7 kB (checked in by mikey, 1 week ago)

fix coding style issues

Line 
1 <?php
2 /**
3  * Container to allow method chaining for returned arrays.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  lang
8  */
9 stubClassLoader::load('net::stubbles::lang::exceptions::stubIllegalAccessException');
10 /**
11  * Container to allow method chaining for returned arrays.
12  *
13  * In case a method returns an array it may return an instance of this class as
14  * well, constructed with the array intended for return. This will allow other
15  * methods for method chaining. This is required in some circumstances as PHP
16  * does not allow to write code like
17  * <code>
18  * echo asArray()[0]
19  * </code>
20  * which would access the first value of the returned array. A work around is
21  * to write
22  * <code>
23  * $array = asArray();
24  * echo $array[0];
25  * </code>
26  * With this class it is possible to write it as follows:
27  * <code>
28  * echo asArray()->first()
29  * </code>
30  *
31  * @package     stubbles
32  * @subpackage  lang
33  */
34 class stubArrayAccessor extends stubBaseObject implements ArrayAccess, Countable, IteratorAggregate
35 {
36     /**
37      * the wrapped array
38      *
39      * @var  array
40      */
41     protected $data = array();
42
43     /**
44      * constructor
45      *
46      * @param  array  $data  optional
47      */
48     public function __construct(array $data = array())
49     {
50         $this->data = $data;
51     }
52
53     /**
54      * returns value of first element
55      *
56      * Returns null if there is no first element.
57      *
58      * @return  mixed
59      */
60     public function first()
61     {
62         if (count($this->data) === 0) {
63             return null;
64         }
65         
66         reset($this->data);
67         return current($this->data);
68     }
69
70     /**
71      * returns value of last element
72      *
73      * @return  mixed
74      */
75     public function last()
76     {
77         if (count($this->data) === 0) {
78             return null;
79         }
80         
81         return end($this->data);
82     }
83
84     /**
85      * returns value stored under given offset
86      *
87      * @param   string|int  $offset
88      * @return  mixed
89      */
90     public function at($offset)
91     {
92         return $this->offsetGet($offset);
93     }
94
95     /**
96      * replaces current array with another array
97      *
98      * @param  array  $data
99      */
100     public function replace(array $data)
101     {
102         $this->data = $data;
103     }
104
105     /**
106      * returns raw array
107      *
108      * @return  array
109      */
110     public function toArray()
111     {
112         return $this->data;
113     }
114
115     /**
116      * checks whether an entry for given offset exists
117      *
118      * @param   string|int  $offset
119      * @return  bool
120      */
121     public function offsetExists($offset)
122     {
123         return isset($this->data[$offset]);
124     }
125
126     /**
127      * returns value stored under given offset
128      *
129      * @param   string|int  $offset
130      * @return  mixed
131      * @throws  stubIllegalAccessException
132      */
133     public function offsetGet($offset)
134     {
135         if (isset($this->data[$offset]) === true) {
136             return $this->data[$offset];
137         }
138         
139         throw new stubIllegalAccessException('No element for offset ' . $offset);
140     }
141
142     /**
143      * sets value at given offset
144      *
145      * @param  string|int  $offset
146      * @param  mixed       $value
147      */
148     public function offsetSet($offset, $value)
149     {
150         $this->data[$offset] = $value;
151     }
152
153     /**
154      * removes given offset
155      *
156      * @param  string|int  $offset
157      */
158     public function offsetUnset($offset)
159     {
160         unset($this->data[$offset]);
161     }
162
163     /**
164      * returns amount of elements
165      *
166      * @return  int
167      */
168     public function count()
169     {
170         return count($this->data);
171     }
172
173     /**
174      * returns an iterator to be used in foreach
175      *
176      * @return  Iterator
177      */
178     public function getIterator()
179     {
180         return new ArrayIterator($this->data);
181     }
182 }
183 ?>
Note: See TracBrowser for help on using the browser.