MediaWiki  REL1_20
GenericArrayObject.php
Go to the documentation of this file.
00001 <?php
00002 
00036 abstract class GenericArrayObject extends ArrayObject {
00037 
00045         public abstract function getObjectType();
00046 
00052         protected $indexOffset = 0;
00053 
00063         protected function getNewOffset() {
00064                 while ( true ) {
00065                         if ( !$this->offsetExists( $this->indexOffset ) ) {
00066                                 return $this->indexOffset;
00067                         }
00068 
00069                         $this->indexOffset++;
00070                 }
00071         }
00072 
00083         public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) {
00084                 parent::__construct( array(), $flags, $iterator_class );
00085 
00086                 if ( !is_null( $input ) ) {
00087                         foreach ( $input as $offset => $value ) {
00088                                 $this->offsetSet( $offset, $value );
00089                         }
00090                 }
00091         }
00092 
00100         public function append( $value ) {
00101                 $this->setElement( null, $value );
00102         }
00103 
00112         public function offsetSet( $index, $value ) {
00113                 $this->setElement( $index, $value );
00114         }
00115 
00126         protected function hasValidType( $value ) {
00127                 $class = $this->getObjectType();
00128                 return $value instanceof $class;
00129         }
00130 
00147         protected function setElement( $index, $value ) {
00148                 if ( !$this->hasValidType( $value ) ) {
00149                         throw new InvalidArgumentException(
00150                                 'Can only add ' . $this->getObjectType() . ' implementing objects to ' . get_called_class() . '.'
00151                         );
00152                 }
00153 
00154                 if ( is_null( $index ) ) {
00155                         $index = $this->getNewOffset();
00156                 }
00157 
00158                 if ( $this->preSetElement( $index, $value ) ) {
00159                         parent::offsetSet( $index, $value );
00160                 }
00161         }
00162 
00179         protected function preSetElement( $index, $value ) {
00180                 return true;
00181         }
00182 
00190         public function serialize() {
00191                 return serialize( $this->getSerializationData() );
00192         }
00193 
00203         protected function getSerializationData() {
00204                 return array(
00205                         'data' => $this->getArrayCopy(),
00206                         'index' => $this->indexOffset,
00207                 );
00208         }
00209 
00219         public function unserialize( $serialization ) {
00220                 $serializationData = unserialize( $serialization );
00221 
00222                 foreach ( $serializationData['data'] as $offset => $value ) {
00223                         // Just set the element, bypassing checks and offset resolving,
00224                         // as these elements have already gone through this.
00225                         parent::offsetSet( $offset, $value );
00226                 }
00227 
00228                 $this->indexOffset = $serializationData['index'];
00229 
00230                 return $serializationData;
00231         }
00232 
00240         public function isEmpty() {
00241                 return $this->count() === 0;
00242         }
00243 
00244 }