MediaWiki
REL1_22
|
00001 <?php 00002 00035 abstract class GenericArrayObject extends ArrayObject { 00036 00044 abstract public function getObjectType(); 00045 00051 protected $indexOffset = 0; 00052 00062 protected function getNewOffset() { 00063 while ( $this->offsetExists( $this->indexOffset ) ) { 00064 $this->indexOffset++; 00065 } 00066 00067 return $this->indexOffset; 00068 } 00069 00080 public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) { 00081 parent::__construct( array(), $flags, $iterator_class ); 00082 00083 if ( !is_null( $input ) ) { 00084 foreach ( $input as $offset => $value ) { 00085 $this->offsetSet( $offset, $value ); 00086 } 00087 } 00088 } 00089 00097 public function append( $value ) { 00098 $this->setElement( null, $value ); 00099 } 00100 00109 public function offsetSet( $index, $value ) { 00110 $this->setElement( $index, $value ); 00111 } 00112 00123 protected function hasValidType( $value ) { 00124 $class = $this->getObjectType(); 00125 return $value instanceof $class; 00126 } 00127 00144 protected function setElement( $index, $value ) { 00145 if ( !$this->hasValidType( $value ) ) { 00146 throw new InvalidArgumentException( 00147 'Can only add ' . $this->getObjectType() . ' implementing objects to ' . 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 00241 }