[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Collections/ -> NormalizedList.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2020 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\Collections;
   9  
  10  use InvalidArgumentException;
  11  
  12  class NormalizedList extends NormalizedCollection
  13  {
  14      /**
  15      * Add (append) a value to this list
  16      *
  17      * Alias for append(). Overrides NormalizedCollection::add()
  18      *
  19      * @param  mixed $value Original value
  20      * @param  null  $void  Unused
  21      * @return mixed        Normalized value
  22      */
  23  	public function add($value, $void = null)
  24      {
  25          return $this->append($value);
  26      }
  27  
  28      /**
  29      * Append a value to this list
  30      *
  31      * @param  mixed $value Original value
  32      * @return mixed        Normalized value
  33      */
  34  	public function append($value)
  35      {
  36          $value = $this->normalizeValue($value);
  37  
  38          $this->items[] = $value;
  39  
  40          return $value;
  41      }
  42  
  43      /**
  44      * Delete a value from this list and remove gaps in keys
  45      *
  46      * NOTE: parent::offsetUnset() maps to $this->delete() so this method covers both usages
  47      *
  48      * @param  string $key
  49      * @return void
  50      */
  51  	public function delete($key)
  52      {
  53          parent::delete($key);
  54  
  55          // Reindex the array to eliminate any gaps
  56          $this->items = array_values($this->items);
  57      }
  58  
  59      /**
  60      * Insert a value at an arbitrary 0-based position
  61      *
  62      * @param  integer $offset
  63      * @param  mixed   $value
  64      * @return mixed           Normalized value
  65      */
  66  	public function insert($offset, $value)
  67      {
  68          $offset = $this->normalizeKey($offset);
  69          $value  = $this->normalizeValue($value);
  70  
  71          // Insert the value at given offset. We put the value into an array so that array_splice()
  72          // won't insert it as multiple elements if it happens to be an array
  73          array_splice($this->items, $offset, 0, [$value]);
  74  
  75          return $value;
  76      }
  77  
  78      /**
  79      * Ensure that the key is a valid offset
  80      *
  81      * Negative values count from the end of the list
  82      *
  83      * @param  mixed   $key
  84      * @return integer
  85      */
  86  	public function normalizeKey($key)
  87      {
  88          $normalizedKey = filter_var(
  89              (preg_match('(^-\\d+$)D', $key)) ? count($this->items) + $key : $key,
  90              FILTER_VALIDATE_INT,
  91              [
  92                  'options' => [
  93                      'min_range' => 0,
  94                      'max_range' => count($this->items)
  95                  ]
  96              ]
  97          );
  98  
  99          if ($normalizedKey === false)
 100          {
 101              throw new InvalidArgumentException("Invalid offset '" . $key . "'");
 102          }
 103  
 104          return $normalizedKey;
 105      }
 106  
 107      /**
 108      * Custom offsetSet() implementation to allow assignment with a null offset to append to the
 109      * chain
 110      *
 111      * @param  mixed $offset
 112      * @param  mixed $value
 113      * @return void
 114      */
 115  	public function offsetSet($offset, $value)
 116      {
 117          if ($offset === null)
 118          {
 119              // $list[] = 'foo' maps to $list->append('foo')
 120              $this->append($value);
 121          }
 122          else
 123          {
 124              // Use the default implementation
 125              parent::offsetSet($offset, $value);
 126          }
 127      }
 128  
 129      /**
 130      * Prepend a value to this list
 131      *
 132      * @param  mixed $value
 133      * @return mixed        Normalized value
 134      */
 135  	public function prepend($value)
 136      {
 137          $value = $this->normalizeValue($value);
 138  
 139          array_unshift($this->items, $value);
 140  
 141          return $value;
 142      }
 143  
 144      /**
 145      * Remove all items matching given value
 146      *
 147      * @param  mixed   $value Original value
 148      * @return integer        Number of items removed
 149      */
 150  	public function remove($value)
 151      {
 152          $keys = array_keys($this->items, $this->normalizeValue($value));
 153          foreach ($keys as $k)
 154          {
 155              unset($this->items[$k]);
 156          }
 157  
 158          $this->items = array_values($this->items);
 159  
 160          return count($keys);
 161      }
 162  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1