4 * Extends ArrayObject and does two things:
6 * Allows for deriving classes to easily intercept additions
7 * and deletions for purposes such as additional indexing.
9 * Enforces the objects to be of a certain type, so this
10 * can be replied upon, much like if this had true support
11 * for generics, which sadly enough is not possible in PHP.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 * http://www.gnu.org/copyleft/gpl.html
33 * @licence GNU GPL v2+
34 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
36 abstract class GenericArrayObject
extends ArrayObject
{
39 * Returns the name of an interface/class that the element should implement/extend.
45 public abstract function getObjectType();
48 * @see SiteList::getNewOffset()
52 protected $indexOffset = 0;
55 * Finds a new offset for when appending an element.
56 * The base class does this, so it would be better to integrate,
57 * but there does not appear to be any way to do this...
63 protected function getNewOffset() {
65 if ( !$this->offsetExists( $this->indexOffset
) ) {
66 return $this->indexOffset
;
75 * @see ArrayObject::__construct
79 * @param null|array $input
81 * @param string $iterator_class
83 public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) {
84 parent
::__construct( array(), $flags, $iterator_class );
86 if ( !is_null( $input ) ) {
87 foreach ( $input as $offset => $value ) {
88 $this->offsetSet( $offset, $value );
94 * @see ArrayObject::append
100 public function append( $value ) {
101 $this->setElement( null, $value );
105 * @see ArrayObject::offsetSet()
109 * @param mixed $index
110 * @param mixed $value
112 public function offsetSet( $index, $value ) {
113 $this->setElement( $index, $value );
117 * Returns if the provided value has the same type as the elements
118 * that can be added to this ArrayObject.
122 * @param mixed $value
126 protected function hasValidType( $value ) {
127 $class = $this->getObjectType();
128 return $value instanceof $class;
132 * Method that actually sets the element and holds
133 * all common code needed for set operations, including
134 * type checking and offset resolving.
136 * If you want to do additional indexing or have code that
137 * otherwise needs to be executed whenever an element is added,
138 * you can overload @see preSetElement.
142 * @param mixed $index
143 * @param mixed $value
145 * @throws InvalidArgumentException
147 protected function setElement( $index, $value ) {
148 if ( !$this->hasValidType( $value ) ) {
149 throw new InvalidArgumentException(
150 'Can only add ' . $this->getObjectType() . ' implementing objects to ' . get_called_class() . '.'
154 if ( is_null( $index ) ) {
155 $index = $this->getNewOffset();
158 if ( $this->preSetElement( $index, $value ) ) {
159 parent
::offsetSet( $index, $value );
164 * Gets called before a new element is added to the ArrayObject.
166 * At this point the index is always set (ie not null) and the
167 * value is always of the type returned by @see getObjectType.
169 * Should return a boolean. When false is returned the element
170 * does not get added to the ArrayObject.
174 * @param integer|string $index
175 * @param mixed $value
179 protected function preSetElement( $index, $value ) {
184 * @see Serializable::serialize
190 public function serialize() {
191 return serialize( $this->getSerializationData() );
195 * Returns an array holding all the data that should go into serialization calls.
196 * This is intended to allow overloading without having to reimplement the
197 * behaviour of this base class.
203 protected function getSerializationData() {
205 'data' => $this->getArrayCopy(),
206 'index' => $this->indexOffset
,
211 * @see Serializable::unserialize
215 * @param string $serialization
219 public function unserialize( $serialization ) {
220 $serializationData = unserialize( $serialization );
222 foreach ( $serializationData['data'] as $offset => $value ) {
223 // Just set the element, bypassing checks and offset resolving,
224 // as these elements have already gone through this.
225 parent
::offsetSet( $offset, $value );
228 $this->indexOffset
= $serializationData['index'];
230 return $serializationData;
234 * Returns if the ArrayObject has no elements.
240 public function isEmpty() {
241 return $this->count() === 0;