MediaWiki  REL1_24
GenericArrayObject.php
Go to the documentation of this file.
00001 <?php
00002 
00035 abstract class GenericArrayObject extends ArrayObject {
00043     abstract public function getObjectType();
00044 
00050     protected $indexOffset = 0;
00051 
00061     protected function getNewOffset() {
00062         while ( $this->offsetExists( $this->indexOffset ) ) {
00063             $this->indexOffset++;
00064         }
00065 
00066         return $this->indexOffset;
00067     }
00068 
00079     public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) {
00080         parent::__construct( array(), $flags, $iterator_class );
00081 
00082         if ( !is_null( $input ) ) {
00083             foreach ( $input as $offset => $value ) {
00084                 $this->offsetSet( $offset, $value );
00085             }
00086         }
00087     }
00088 
00096     public function append( $value ) {
00097         $this->setElement( null, $value );
00098     }
00099 
00108     public function offsetSet( $index, $value ) {
00109         $this->setElement( $index, $value );
00110     }
00111 
00122     protected function hasValidType( $value ) {
00123         $class = $this->getObjectType();
00124         return $value instanceof $class;
00125     }
00126 
00143     protected function setElement( $index, $value ) {
00144         if ( !$this->hasValidType( $value ) ) {
00145             throw new InvalidArgumentException(
00146                 'Can only add ' . $this->getObjectType() . ' implementing objects to '
00147                 . get_called_class() . '.'
00148             );
00149         }
00150 
00151         if ( is_null( $index ) ) {
00152             $index = $this->getNewOffset();
00153         }
00154 
00155         if ( $this->preSetElement( $index, $value ) ) {
00156             parent::offsetSet( $index, $value );
00157         }
00158     }
00159 
00176     protected function preSetElement( $index, $value ) {
00177         return true;
00178     }
00179 
00187     public function serialize() {
00188         return serialize( $this->getSerializationData() );
00189     }
00190 
00200     protected function getSerializationData() {
00201         return array(
00202             'data' => $this->getArrayCopy(),
00203             'index' => $this->indexOffset,
00204         );
00205     }
00206 
00216     public function unserialize( $serialization ) {
00217         $serializationData = unserialize( $serialization );
00218 
00219         foreach ( $serializationData['data'] as $offset => $value ) {
00220             // Just set the element, bypassing checks and offset resolving,
00221             // as these elements have already gone through this.
00222             parent::offsetSet( $offset, $value );
00223         }
00224 
00225         $this->indexOffset = $serializationData['index'];
00226 
00227         return $serializationData;
00228     }
00229 
00237     public function isEmpty() {
00238         return $this->count() === 0;
00239     }
00240 }