Skip to content

Commit

Permalink
Use propertyAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jul 25, 2020
1 parent 50974bc commit 9aceac7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -39,6 +39,7 @@
"symfony/http-foundation": "^4.4",
"symfony/http-kernel": "^4.4",
"symfony/options-resolver": "^4.4",
"symfony/property-access": "^4.4",
"symfony/security-acl": "^3.0",
"twig/twig": "^2.10"
},
Expand Down
51 changes: 17 additions & 34 deletions src/Model/ModelManager.php
Expand Up @@ -37,23 +37,32 @@
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
use Sonata\Exporter\Source\DoctrineORMQuerySourceIterator;
use Symfony\Component\Form\Exception\PropertyAccessDeniedException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

class ModelManager implements ModelManagerInterface, LockInterface
{
public const ID_SEPARATOR = '~';

/**
* @var ManagerRegistry
*/
protected $registry;

/**
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;

/**
* @var EntityManager[]
*/
protected $cache = [];

public function __construct(ManagerRegistry $registry)
public function __construct(ManagerRegistry $registry, ?PropertyAccessorInterface $propertyAccessor = null)
{
$this->registry = $registry;
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
}

/**
Expand Down Expand Up @@ -591,48 +600,17 @@ public function modelReverseTransform($class, array $array = [])
$instance = $this->getModelInstance($class);
$metadata = $this->getMetadata($class);

$reflClass = $metadata->reflClass;
foreach ($array as $name => $value) {
$reflection_property = false;
// property or association ?
if (\array_key_exists($name, $metadata->fieldMappings)) {
$property = $metadata->fieldMappings[$name]['fieldName'];
$reflection_property = $metadata->reflFields[$name];
} elseif (\array_key_exists($name, $metadata->associationMappings)) {
$property = $metadata->associationMappings[$name]['fieldName'];
} else {
$property = $name;
}

$setter = 'set'.$this->camelize($name);

if ($reflClass->hasMethod($setter)) {
if (!$reflClass->getMethod($setter)->isPublic()) {
throw new PropertyAccessDeniedException(sprintf(
'Method "%s()" is not public in class "%s"',
$setter,
$reflClass->getName()
));
}

$instance->$setter($value);
} elseif ($reflClass->hasMethod('__set')) {
// needed to support magic method __set
$instance->$property = $value;
} elseif ($reflClass->hasProperty($property)) {
if (!$reflClass->getProperty($property)->isPublic()) {
throw new PropertyAccessDeniedException(sprintf(
'Property "%s" is not public in class "%s". Maybe you should create the method "set%s()"?',
$property,
$reflClass->getName(),
ucfirst($property)
));
}

$instance->$property = $value;
} elseif ($reflection_property) {
$reflection_property->setValue($instance, $value);
}
$this->propertyAccessor->setValue($instance, $property, $value);
}

return $instance;
Expand Down Expand Up @@ -664,14 +642,19 @@ public function collectionRemoveElement(&$collection, &$element)
}

/**
* method taken from Symfony\Component\PropertyAccess\PropertyAccessor.
* NEXT_MAJOR: Remove this method
*
* @param string $property
*
* @return mixed
*/
protected function camelize($property)
{
@trigger_error(sprintf(
'Method %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
}

Expand Down

0 comments on commit 9aceac7

Please sign in to comment.