[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Collections/ -> NormalizedCollection.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 ArrayAccess;
  11  use InvalidArgumentException;
  12  use RuntimeException;
  13  
  14  class NormalizedCollection extends Collection implements ArrayAccess
  15  {
  16      /**
  17      * @var string Action to take when add() is called with a key that already exists
  18      */
  19      protected $onDuplicateAction = 'error';
  20  
  21      /**
  22      * {@inheritdoc}
  23      */
  24  	public function asConfig()
  25      {
  26          $config = parent::asConfig();
  27          ksort($config);
  28  
  29          return $config;
  30      }
  31  
  32      /**
  33      * Query and set the action to take when add() is called with a key that already exists
  34      *
  35      * @param  string|null $action If specified: either "error", "ignore" or "replace"
  36      * @return string              Old action
  37      */
  38  	public function onDuplicate($action = null)
  39      {
  40          // Save the old action so it can be returned
  41          $old = $this->onDuplicateAction;
  42  
  43          if (func_num_args() && $action !== 'error' && $action !== 'ignore' && $action !== 'replace')
  44          {
  45              throw new InvalidArgumentException("Invalid onDuplicate action '" . $action . "'. Expected: 'error', 'ignore' or 'replace'");
  46          }
  47  
  48          $this->onDuplicateAction = $action;
  49  
  50          return $old;
  51      }
  52  
  53      //==========================================================================
  54      // Overridable methods
  55      //==========================================================================
  56  
  57      /**
  58      * Return the exception that is thrown when creating an item using a key that already exists
  59      *
  60      * @param  string           $key Item's key
  61      * @return RuntimeException
  62      */
  63  	protected function getAlreadyExistsException($key)
  64      {
  65          return new RuntimeException("Item '" . $key . "' already exists");
  66      }
  67  
  68      /**
  69      * Return the exception that is thrown when accessing an item that does not exist
  70      *
  71      * @param  string           $key Item's key
  72      * @return RuntimeException
  73      */
  74  	protected function getNotExistException($key)
  75      {
  76          return new RuntimeException("Item '" . $key . "' does not exist");
  77      }
  78  
  79      /**
  80      * Normalize an item's key
  81      *
  82      * This method can be overridden to implement keys normalization or implement constraints
  83      *
  84      * @param  string $key Original key
  85      * @return string      Normalized key
  86      */
  87  	public function normalizeKey($key)
  88      {
  89          return $key;
  90      }
  91  
  92      /**
  93      * Normalize a value for storage
  94      *
  95      * This method can be overridden to implement value normalization
  96      *
  97      * @param  mixed $value Original value
  98      * @return mixed        Normalized value
  99      */
 100  	public function normalizeValue($value)
 101      {
 102          return $value;
 103      }
 104  
 105      //==========================================================================
 106      // Items access/manipulation
 107      //==========================================================================
 108  
 109      /**
 110      * Add an item to this collection
 111      *
 112      * NOTE: relies on exists() to check the key for invalid values and on set() to normalize it
 113      *
 114      * @param  string $key   Item's key
 115      * @param  mixed  $value Item's value
 116      * @return mixed         Normalized value
 117      */
 118  	public function add($key, $value = null)
 119      {
 120          // Test whether this key is already in use
 121          if ($this->exists($key))
 122          {
 123              // If the action is "ignore" we return the old value, if it's "error" we throw an
 124              // exception. Otherwise, we keep going and replace the value
 125              if ($this->onDuplicateAction === 'ignore')
 126              {
 127                  return $this->get($key);
 128              }
 129              elseif ($this->onDuplicateAction === 'error')
 130              {
 131                  throw $this->getAlreadyExistsException($key);
 132              }
 133          }
 134  
 135          return $this->set($key, $value);
 136      }
 137  
 138      /**
 139      * Test whether a given value is present in this collection
 140      *
 141      * @param  mixed $value Original value
 142      * @return bool         Whether the normalized value was found in this collection
 143      */
 144  	public function contains($value)
 145      {
 146          return in_array($this->normalizeValue($value), $this->items);
 147      }
 148  
 149      /**
 150      * Delete an item from this collection
 151      *
 152      * @param  string $key Item's key
 153      * @return void
 154      */
 155  	public function delete($key)
 156      {
 157          $key = $this->normalizeKey($key);
 158  
 159          unset($this->items[$key]);
 160      }
 161  
 162      /**
 163      * Test whether an item of given key exists
 164      *
 165      * @param  string $key Item's key
 166      * @return bool        Whether this key exists in this collection
 167      */
 168  	public function exists($key)
 169      {
 170          $key = $this->normalizeKey($key);
 171  
 172          return array_key_exists($key, $this->items);
 173      }
 174  
 175      /**
 176      * Return a value from this collection
 177      *
 178      * @param  string $key Item's key
 179      * @return mixed       Normalized value
 180      */
 181  	public function get($key)
 182      {
 183          if (!$this->exists($key))
 184          {
 185              throw $this->getNotExistException($key);
 186          }
 187  
 188          $key = $this->normalizeKey($key);
 189  
 190          return $this->items[$key];
 191      }
 192  
 193      /**
 194      * Find the index of a given value
 195      *
 196      * Will return the first key associated with the given value, or FALSE if the value is not found
 197      *
 198      * @param  mixed $value Original value
 199      * @return mixed        Index of the value, or FALSE if not found
 200      */
 201  	public function indexOf($value)
 202      {
 203          return array_search($this->normalizeValue($value), $this->items);
 204      }
 205  
 206      /**
 207      * Set and overwrite a value in this collection
 208      *
 209      * @param  string $key   Item's key
 210      * @param  mixed  $value Item's value
 211      * @return mixed         Normalized value
 212      */
 213  	public function set($key, $value)
 214      {
 215          $key = $this->normalizeKey($key);
 216  
 217          $this->items[$key] = $this->normalizeValue($value);
 218  
 219          return $this->items[$key];
 220      }
 221  
 222      //==========================================================================
 223      // ArrayAccess stuff
 224      //==========================================================================
 225  
 226      /**
 227      * @param  string|integer $offset
 228      * @return bool
 229      */
 230  	public function offsetExists($offset)
 231      {
 232          return $this->exists($offset);
 233      }
 234  
 235      /**
 236      * @param  string|integer $offset
 237      * @return mixed
 238      */
 239  	public function offsetGet($offset)
 240      {
 241          return $this->get($offset);
 242      }
 243  
 244      /**
 245      * @param  string|integer $offset
 246      * @param  mixed          $value
 247      * @return void
 248      */
 249  	public function offsetSet($offset, $value)
 250      {
 251          $this->set($offset, $value);
 252      }
 253  
 254      /**
 255      * @param  string|integer $offset
 256      * @return void
 257      */
 258  	public function offsetUnset($offset)
 259      {
 260          $this->delete($offset);
 261      }
 262  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1