2023-03-11 12:04:29 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Zend Framework (http://framework.zend.com/)
|
|
|
|
*
|
|
|
|
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
2023-04-01 09:03:34 +03:00
|
|
|
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
2023-03-11 12:04:29 +03:00
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Zend\Stdlib\Hydrator;
|
|
|
|
|
|
|
|
use Zend\Stdlib\Exception;
|
2023-04-01 09:03:34 +03:00
|
|
|
use ReflectionClass;
|
|
|
|
use ReflectionProperty;
|
2023-03-11 12:04:29 +03:00
|
|
|
|
|
|
|
class ObjectProperty extends AbstractHydrator
|
|
|
|
{
|
|
|
|
/**
|
2023-04-01 09:03:34 +03:00
|
|
|
* @var array[] indexed by class name and then property name
|
|
|
|
*/
|
|
|
|
private static $skippedPropertiesCache = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2023-03-11 12:04:29 +03:00
|
|
|
*
|
|
|
|
* Extracts the accessible non-static properties of the given $object.
|
|
|
|
*
|
|
|
|
* @throws Exception\BadMethodCallException for a non-object $object
|
|
|
|
*/
|
|
|
|
public function extract($object)
|
|
|
|
{
|
|
|
|
if (!is_object($object)) {
|
2023-04-01 09:03:34 +03:00
|
|
|
throw new Exception\BadMethodCallException(
|
|
|
|
sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
|
|
|
|
);
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
|
2023-04-01 09:03:34 +03:00
|
|
|
$data = get_object_vars($object);
|
2023-03-11 12:04:29 +03:00
|
|
|
$filter = $this->getFilter();
|
2023-04-01 09:03:34 +03:00
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
foreach ($data as $name => $value) {
|
|
|
|
// Filter keys, removing any we don't want
|
2023-04-01 09:03:34 +03:00
|
|
|
if (! $filter->filter($name)) {
|
2023-03-11 12:04:29 +03:00
|
|
|
unset($data[$name]);
|
|
|
|
continue;
|
|
|
|
}
|
2023-04-01 09:03:34 +03:00
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
// Replace name if extracted differ
|
|
|
|
$extracted = $this->extractName($name, $object);
|
2023-04-01 09:03:34 +03:00
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
if ($extracted !== $name) {
|
|
|
|
unset($data[$name]);
|
|
|
|
$name = $extracted;
|
|
|
|
}
|
2023-04-01 09:03:34 +03:00
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
$data[$name] = $this->extractValue($name, $value, $object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-01 09:03:34 +03:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2023-03-11 12:04:29 +03:00
|
|
|
* Hydrate an object by populating public properties
|
|
|
|
*
|
|
|
|
* Hydrates an object by setting public properties of the object.
|
|
|
|
*
|
|
|
|
* @throws Exception\BadMethodCallException for a non-object $object
|
|
|
|
*/
|
|
|
|
public function hydrate(array $data, $object)
|
|
|
|
{
|
|
|
|
if (!is_object($object)) {
|
2023-04-01 09:03:34 +03:00
|
|
|
throw new Exception\BadMethodCallException(
|
|
|
|
sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
|
|
|
|
);
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
2023-04-01 09:03:34 +03:00
|
|
|
|
|
|
|
$properties = & self::$skippedPropertiesCache[get_class($object)];
|
|
|
|
|
|
|
|
if (! isset($properties)) {
|
|
|
|
$reflection = new ReflectionClass($object);
|
|
|
|
$properties = array_fill_keys(
|
|
|
|
array_map(
|
|
|
|
function (ReflectionProperty $property) {
|
|
|
|
return $property->getName();
|
|
|
|
},
|
|
|
|
$reflection->getProperties(
|
|
|
|
ReflectionProperty::IS_PRIVATE
|
|
|
|
+ ReflectionProperty::IS_PROTECTED
|
|
|
|
+ ReflectionProperty::IS_STATIC
|
|
|
|
)
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
foreach ($data as $name => $value) {
|
|
|
|
$property = $this->hydrateName($name, $data);
|
2023-04-01 09:03:34 +03:00
|
|
|
|
|
|
|
if (isset($properties[$property])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
$object->$property = $this->hydrateValue($property, $value, $data);
|
|
|
|
}
|
2023-04-01 09:03:34 +03:00
|
|
|
|
2023-03-11 12:04:29 +03:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
}
|