[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-stdlib/src/Hydrator/NamingStrategy/ -> MapNamingStrategy.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  
  10  namespace Zend\Stdlib\Hydrator\NamingStrategy;
  11  
  12  use Zend\Stdlib\Exception\InvalidArgumentException;
  13  
  14  class MapNamingStrategy implements NamingStrategyInterface
  15  {
  16      /**
  17       * Map for hydrate name conversion.
  18       *
  19       * @var array
  20       */
  21      protected $mapping = array();
  22  
  23      /**
  24       * Reversed map for extract name conversion.
  25       *
  26       * @var array
  27       */
  28      protected $reverse = array();
  29  
  30      /**
  31       * Initialize.
  32       *
  33       * @param array $mapping Map for name conversion on hydration
  34       * @param array $reverse Reverse map for name conversion on extraction
  35       */
  36      public function __construct(array $mapping, array $reverse = null)
  37      {
  38          $this->mapping = $mapping;
  39          $this->reverse = $reverse ?: $this->flipMapping($mapping);
  40      }
  41  
  42      /**
  43       * Safelly flip mapping array.
  44       *
  45       * @param  array                    $array Array to flip
  46       * @return array                    Flipped array
  47       * @throws InvalidArgumentException
  48       */
  49      protected function flipMapping(array $array)
  50      {
  51          array_walk($array, function ($value) {
  52              if (!is_string($value) && !is_int($value)) {
  53                  throw new InvalidArgumentException('Mapping array can\'t be flipped because of invalid value');
  54              }
  55          });
  56  
  57          return array_flip($array);
  58      }
  59  
  60      /**
  61       * Converts the given name so that it can be extracted by the hydrator.
  62       *
  63       * @param  string $name The original name
  64       * @return mixed  The hydrated name
  65       */
  66      public function hydrate($name)
  67      {
  68          if (array_key_exists($name, $this->mapping)) {
  69              return $this->mapping[$name];
  70          }
  71  
  72          return $name;
  73      }
  74  
  75      /**
  76       * Converts the given name so that it can be hydrated by the hydrator.
  77       *
  78       * @param  string $name The original name
  79       * @return mixed  The extracted name
  80       */
  81      public function extract($name)
  82      {
  83          if (array_key_exists($name, $this->reverse)) {
  84              return $this->reverse[$name];
  85          }
  86  
  87          return $name;
  88      }
  89  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1