[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-stdlib/src/Hydrator/ -> ObjectProperty.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 Zend\Stdlib\Exception;
  13  use ReflectionClass;
  14  use ReflectionProperty;
  15  
  16  class ObjectProperty extends AbstractHydrator
  17  {
  18      /**
  19       * @var array[] indexed by class name and then property name
  20       */
  21      private static $skippedPropertiesCache = array();
  22  
  23      /**
  24       * {@inheritDoc}
  25       *
  26       * Extracts the accessible non-static properties of the given $object.
  27       *
  28       * @throws Exception\BadMethodCallException for a non-object $object
  29       */
  30      public function extract($object)
  31      {
  32          if (!is_object($object)) {
  33              throw new Exception\BadMethodCallException(
  34                  sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
  35              );
  36          }
  37  
  38          $data   = get_object_vars($object);
  39          $filter = $this->getFilter();
  40  
  41          foreach ($data as $name => $value) {
  42              // Filter keys, removing any we don't want
  43              if (! $filter->filter($name)) {
  44                  unset($data[$name]);
  45                  continue;
  46              }
  47  
  48              // Replace name if extracted differ
  49              $extracted = $this->extractName($name, $object);
  50  
  51              if ($extracted !== $name) {
  52                  unset($data[$name]);
  53                  $name = $extracted;
  54              }
  55  
  56              $data[$name] = $this->extractValue($name, $value, $object);
  57          }
  58  
  59          return $data;
  60      }
  61  
  62      /**
  63       * {@inheritDoc}
  64       *
  65       * Hydrate an object by populating public properties
  66       *
  67       * Hydrates an object by setting public properties of the object.
  68       *
  69       * @throws Exception\BadMethodCallException for a non-object $object
  70       */
  71      public function hydrate(array $data, $object)
  72      {
  73          if (!is_object($object)) {
  74              throw new Exception\BadMethodCallException(
  75                  sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
  76              );
  77          }
  78  
  79          $properties = & self::$skippedPropertiesCache[get_class($object)];
  80  
  81          if (! isset($properties)) {
  82              $reflection = new ReflectionClass($object);
  83              $properties = array_fill_keys(
  84                  array_map(
  85                      function (ReflectionProperty $property) {
  86                          return $property->getName();
  87                      },
  88                      $reflection->getProperties(
  89                          ReflectionProperty::IS_PRIVATE
  90                          + ReflectionProperty::IS_PROTECTED
  91                          + ReflectionProperty::IS_STATIC
  92                      )
  93                  ),
  94                  true
  95              );
  96          }
  97  
  98          foreach ($data as $name => $value) {
  99              $property = $this->hydrateName($name, $data);
 100  
 101              if (isset($properties[$property])) {
 102                  continue;
 103              }
 104  
 105              $object->$property = $this->hydrateValue($property, $value, $data);
 106          }
 107  
 108          return $object;
 109      }
 110  }


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