[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-stdlib/src/Hydrator/ -> AbstractHydrator.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;
  11  
  12  use ArrayObject;
  13  use Zend\Stdlib\Exception;
  14  use Zend\Stdlib\Hydrator\Filter\FilterComposite;
  15  use Zend\Stdlib\Hydrator\NamingStrategy\NamingStrategyInterface;
  16  use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
  17  
  18  abstract class AbstractHydrator implements
  19      HydratorInterface,
  20      StrategyEnabledInterface,
  21      FilterEnabledInterface,
  22      NamingStrategyEnabledInterface
  23  {
  24      /**
  25       * The list with strategies that this hydrator has.
  26       *
  27       * @var ArrayObject
  28       */
  29      protected $strategies;
  30  
  31      /**
  32       * An instance of NamingStrategyInterface
  33       *
  34       * @var NamingStrategyInterface
  35       */
  36      protected $namingStrategy;
  37  
  38      /**
  39       * Composite to filter the methods, that need to be hydrated
  40       *
  41       * @var Filter\FilterComposite
  42       */
  43      protected $filterComposite;
  44  
  45      /**
  46       * Initializes a new instance of this class.
  47       */
  48      public function __construct()
  49      {
  50          $this->strategies = new ArrayObject();
  51          $this->filterComposite = new FilterComposite();
  52      }
  53  
  54      /**
  55       * Gets the strategy with the given name.
  56       *
  57       * @param string $name The name of the strategy to get.
  58       *
  59       * @throws \Zend\Stdlib\Exception\InvalidArgumentException
  60       * @return StrategyInterface
  61       */
  62      public function getStrategy($name)
  63      {
  64          if (isset($this->strategies[$name])) {
  65              return $this->strategies[$name];
  66          }
  67  
  68          if (!isset($this->strategies['*'])) {
  69              throw new Exception\InvalidArgumentException(sprintf(
  70                  '%s: no strategy by name of "%s", and no wildcard strategy present',
  71                  __METHOD__,
  72                  $name
  73              ));
  74          }
  75  
  76          return $this->strategies['*'];
  77      }
  78  
  79      /**
  80       * Checks if the strategy with the given name exists.
  81       *
  82       * @param string $name The name of the strategy to check for.
  83       * @return bool
  84       */
  85      public function hasStrategy($name)
  86      {
  87          return array_key_exists($name, $this->strategies)
  88                 || array_key_exists('*', $this->strategies);
  89      }
  90  
  91      /**
  92       * Adds the given strategy under the given name.
  93       *
  94       * @param string $name The name of the strategy to register.
  95       * @param StrategyInterface $strategy The strategy to register.
  96       * @return HydratorInterface
  97       */
  98      public function addStrategy($name, StrategyInterface $strategy)
  99      {
 100          $this->strategies[$name] = $strategy;
 101          return $this;
 102      }
 103  
 104      /**
 105       * Removes the strategy with the given name.
 106       *
 107       * @param string $name The name of the strategy to remove.
 108       * @return HydratorInterface
 109       */
 110      public function removeStrategy($name)
 111      {
 112          unset($this->strategies[$name]);
 113          return $this;
 114      }
 115  
 116      /**
 117       * Converts a value for extraction. If no strategy exists the plain value is returned.
 118       *
 119       * @param  string $name  The name of the strategy to use.
 120       * @param  mixed  $value  The value that should be converted.
 121       * @param  mixed  $object The object is optionally provided as context.
 122       * @return mixed
 123       */
 124      public function extractValue($name, $value, $object = null)
 125      {
 126          if ($this->hasStrategy($name)) {
 127              $strategy = $this->getStrategy($name);
 128              $value = $strategy->extract($value, $object);
 129          }
 130          return $value;
 131      }
 132  
 133      /**
 134       * Converts a value for hydration. If no strategy exists the plain value is returned.
 135       *
 136       * @param string $name The name of the strategy to use.
 137       * @param mixed $value The value that should be converted.
 138       * @param array $data The whole data is optionally provided as context.
 139       * @return mixed
 140       */
 141      public function hydrateValue($name, $value, $data = null)
 142      {
 143          if ($this->hasStrategy($name)) {
 144              $strategy = $this->getStrategy($name);
 145              $value = $strategy->hydrate($value, $data);
 146          }
 147          return $value;
 148      }
 149  
 150      /**
 151       * Convert a name for extraction. If no naming strategy exists, the plain value is returned.
 152       *
 153       * @param string $name    The name to convert.
 154       * @param null   $object  The object is optionally provided as context.
 155       * @return mixed
 156       */
 157      public function extractName($name, $object = null)
 158      {
 159          if ($this->hasNamingStrategy()) {
 160              $name = $this->getNamingStrategy()->extract($name, $object);
 161          }
 162          return $name;
 163      }
 164  
 165      /**
 166       * Converts a value for hydration. If no naming strategy exists, the plain value is returned.
 167       *
 168       * @param string $name  The name to convert.
 169       * @param array  $data  The whole data is optionally provided as context.
 170       * @return mixed
 171       */
 172      public function hydrateName($name, $data = null)
 173      {
 174          if ($this->hasNamingStrategy()) {
 175              $name = $this->getNamingStrategy()->hydrate($name, $data);
 176          }
 177          return $name;
 178      }
 179  
 180      /**
 181       * Get the filter instance
 182       *
 183       * @return Filter\FilterComposite
 184       */
 185      public function getFilter()
 186      {
 187          return $this->filterComposite;
 188      }
 189  
 190      /**
 191       * Add a new filter to take care of what needs to be hydrated.
 192       * To exclude e.g. the method getServiceLocator:
 193       *
 194       * <code>
 195       * $composite->addFilter("servicelocator",
 196       *     function ($property) {
 197       *         list($class, $method) = explode('::', $property);
 198       *         if ($method === 'getServiceLocator') {
 199       *             return false;
 200       *         }
 201       *         return true;
 202       *     }, FilterComposite::CONDITION_AND
 203       * );
 204       * </code>
 205       *
 206       * @param string $name Index in the composite
 207       * @param callable|Filter\FilterInterface $filter
 208       * @param int $condition
 209       * @return Filter\FilterComposite
 210       */
 211      public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR)
 212      {
 213          return $this->filterComposite->addFilter($name, $filter, $condition);
 214      }
 215  
 216      /**
 217       * Check whether a specific filter exists at key $name or not
 218       *
 219       * @param string $name Index in the composite
 220       * @return bool
 221       */
 222      public function hasFilter($name)
 223      {
 224          return $this->filterComposite->hasFilter($name);
 225      }
 226  
 227      /**
 228       * Remove a filter from the composition.
 229       * To not extract "has" methods, you simply need to unregister it
 230       *
 231       * <code>
 232       * $filterComposite->removeFilter('has');
 233       * </code>
 234       *
 235       * @param $name
 236       * @return Filter\FilterComposite
 237       */
 238      public function removeFilter($name)
 239      {
 240          return $this->filterComposite->removeFilter($name);
 241      }
 242  
 243      /**
 244       * Adds the given naming strategy
 245       *
 246       * @param NamingStrategyInterface $strategy The naming to register.
 247       * @return self
 248       */
 249      public function setNamingStrategy(NamingStrategyInterface $strategy)
 250      {
 251          $this->namingStrategy = $strategy;
 252  
 253          return $this;
 254      }
 255  
 256      /**
 257       * Gets the naming strategy.
 258       *
 259       * @return NamingStrategyInterface
 260       */
 261      public function getNamingStrategy()
 262      {
 263          return $this->namingStrategy;
 264      }
 265  
 266      /**
 267       * Checks if a naming strategy exists.
 268       *
 269       * @return bool
 270       */
 271      public function hasNamingStrategy()
 272      {
 273          return isset($this->namingStrategy);
 274      }
 275  
 276      /**
 277       * Removes the naming strategy
 278       *
 279       * @return self
 280       */
 281      public function removeNamingStrategy()
 282      {
 283          $this->namingStrategy = null;
 284  
 285          return $this;
 286      }
 287  }


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