MediaWiki  1.28.1
GenericArrayObject.php
Go to the documentation of this file.
1 <?php
2 
35 abstract class GenericArrayObject extends ArrayObject {
43  abstract public function getObjectType();
44 
50  protected $indexOffset = 0;
51 
61  protected function getNewOffset() {
62  while ( $this->offsetExists( $this->indexOffset ) ) {
63  $this->indexOffset++;
64  }
65 
66  return $this->indexOffset;
67  }
68 
79  public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) {
80  parent::__construct( [], $flags, $iterator_class );
81 
82  if ( !is_null( $input ) ) {
83  foreach ( $input as $offset => $value ) {
84  $this->offsetSet( $offset, $value );
85  }
86  }
87  }
88 
96  public function append( $value ) {
97  $this->setElement( null, $value );
98  }
99 
108  public function offsetSet( $index, $value ) {
109  $this->setElement( $index, $value );
110  }
111 
122  protected function hasValidType( $value ) {
123  $class = $this->getObjectType();
124  return $value instanceof $class;
125  }
126 
143  protected function setElement( $index, $value ) {
144  if ( !$this->hasValidType( $value ) ) {
145  throw new InvalidArgumentException(
146  'Can only add ' . $this->getObjectType() . ' implementing objects to '
147  . static::class . '.'
148  );
149  }
150 
151  if ( is_null( $index ) ) {
152  $index = $this->getNewOffset();
153  }
154 
155  if ( $this->preSetElement( $index, $value ) ) {
156  parent::offsetSet( $index, $value );
157  }
158  }
159 
176  protected function preSetElement( $index, $value ) {
177  return true;
178  }
179 
187  public function serialize() {
188  return serialize( $this->getSerializationData() );
189  }
190 
200  protected function getSerializationData() {
201  return [
202  'data' => $this->getArrayCopy(),
203  'index' => $this->indexOffset,
204  ];
205  }
206 
216  public function unserialize( $serialization ) {
217  $serializationData = unserialize( $serialization );
218 
219  foreach ( $serializationData['data'] as $offset => $value ) {
220  // Just set the element, bypassing checks and offset resolving,
221  // as these elements have already gone through this.
222  parent::offsetSet( $offset, $value );
223  }
224 
225  $this->indexOffset = $serializationData['index'];
226 
227  return $serializationData;
228  }
229 
237  public function isEmpty() {
238  return $this->count() === 0;
239  }
240 }
getObjectType()
Returns the name of an interface/class that the element should implement/extend.
$value
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2703
isEmpty()
Returns if the ArrayObject has no elements.
preSetElement($index, $value)
Gets called before a new element is added to the ArrayObject.
getSerializationData()
Returns an array holding all the data that should go into serialization calls.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getNewOffset()
Finds a new offset for when appending an element.
offsetSet($index, $value)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
unserialize($serialization)
hasValidType($value)
Returns if the provided value has the same type as the elements that can be added to this ArrayObject...
__construct($input=null, $flags=0, $iterator_class= 'ArrayIterator')
Constructor.
setElement($index, $value)
Method that actually sets the element and holds all common code needed for set operations, including type checking and offset resolving.