Skip to content

Commit

Permalink
Do not use property metadata to get/set object values
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed May 2, 2018
1 parent 9314472 commit d68ec99
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 110 deletions.
53 changes: 51 additions & 2 deletions src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,70 @@

namespace JMS\Serializer\Accessor;

use JMS\Serializer\Exception\ExpressionLanguageRequiredException;
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Metadata\StaticPropertyMetadata;

/**
* @author Asmir Mustafic <[email protected]>
*/
final class DefaultAccessorStrategy implements AccessorStrategyInterface
{
private $readAccessors = array();
private $writeAccessors = array();

/**
* @var ExpressionEvaluatorInterface
*/
private $evaluator;

public function __construct(ExpressionEvaluatorInterface $evaluator = null)
{
$this->evaluator = $evaluator;
}

public function getValue(object $object, PropertyMetadata $metadata)
{
return $metadata->getValue($object);
if ($metadata instanceof StaticPropertyMetadata) {
return $metadata->getValue(null);
}

if ($metadata instanceof ExpressionPropertyMetadata) {
if ($this->evaluator === null) {
throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $metadata->name, $metadata->class));
}

return $this->evaluator->evaluate($metadata->expression, ['object' => $object]);
}

if (null === $metadata->getter) {
if (!isset($this->readAccessors[$metadata->class])) {
$this->readAccessors[$metadata->class] = \Closure::bind(function ($o, $name) {
return $o->$name;
}, null, $metadata->class);
}

return $this->readAccessors[$metadata->class]($object, $metadata->name);
}

return $object->{$metadata->getter}();
}

public function setValue(object $object, $value, PropertyMetadata $metadata): void
{
$metadata->setValue($object, $value);
if (null === $metadata->setter) {
if (!isset($this->writeAccessors[$metadata->class])) {
$this->writeAccessors[$metadata->class] = \Closure::bind(function ($o, $name, $value) {
$o->$name = $value;
}, null, $metadata->class);
}

$this->writeAccessors[$metadata->class]($object, $metadata->name, $value);
return;
}

$object->{$metadata->setter}($value);
}
}
59 changes: 0 additions & 59 deletions src/Accessor/ExpressionAccessorStrategy.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Metadata/ExpressionPropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ public function setAccessor($type, $getter = null, $setter = null)
{
}

/**
* @param object $object
* @return mixed
*/
public function getValue($object)
{
throw new ExpressionLanguageRequiredException(sprintf('The property %s on %s requires the expression accessor strategy to be enabled.', $this->name, $this->class));
}

public function setValue($obj, $value)
{
throw new LogicException('ExpressionPropertyMetadata is immutable.');
Expand Down
19 changes: 0 additions & 19 deletions src/Metadata/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,9 @@ class PropertyMetadata extends BasePropertyMetadata
public $maxDepth = null;
public $excludeIf = null;

private $closureAccessor;

public function __construct($class, $name)
{
parent::__construct($class, $name);
$this->closureAccessor = \Closure::bind(function ($o, $name) {
return $o->$name;
}, null, $class);
}

public function setAccessor($type, $getter = null, $setter = null)
Expand Down Expand Up @@ -93,16 +88,6 @@ public function setAccessor($type, $getter = null, $setter = null)
$this->setter = $setter;
}

public function getValue($obj)
{
if (null === $this->getter) {
$accessor = $this->closureAccessor;
return $accessor($obj, $this->name);
}

return $obj->{$this->getter}();
}

public function setValue($obj, $value)
{
if (null === $this->setter) {
Expand Down Expand Up @@ -190,9 +175,5 @@ public function unserialize($str)
}

parent::unserialize($parentStr);

$this->closureAccessor = \Closure::bind(function ($o, $name) {
return $o->$name;
}, null, $this->class);
}
}
5 changes: 0 additions & 5 deletions src/Metadata/StaticPropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public function getValue($obj)
return $this->value;
}

public function setValue($obj, $value)
{
throw new LogicException('StaticPropertyMetadata is immutable.');
}

public function setAccessor($type, $getter = null, $setter = null)
{
}
Expand Down
7 changes: 0 additions & 7 deletions src/Metadata/VirtualPropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

namespace JMS\Serializer\Metadata;

use JMS\Serializer\Exception\LogicException;

class VirtualPropertyMetadata extends PropertyMetadata
{
public function __construct($class, $methodName)
Expand All @@ -38,11 +36,6 @@ public function __construct($class, $methodName)
$this->readOnly = true;
}

public function setValue($obj, $value)
{
throw new LogicException('VirtualPropertyMetadata is immutable.');
}

public function setAccessor($type, $getter = null, $setter = null)
{
}
Expand Down
6 changes: 1 addition & 5 deletions src/SerializerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ public function setAccessorStrategy(AccessorStrategyInterface $accessorStrategy)
private function getAccessorStrategy(): AccessorStrategyInterface
{
if (!$this->accessorStrategy) {
$this->accessorStrategy = new DefaultAccessorStrategy();

if ($this->expressionEvaluator) {
$this->accessorStrategy = new ExpressionAccessorStrategy($this->expressionEvaluator, $this->accessorStrategy);
}
$this->accessorStrategy = new DefaultAccessorStrategy($this->expressionEvaluator);
}
return $this->accessorStrategy;
}
Expand Down
4 changes: 0 additions & 4 deletions tests/Metadata/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ public function testAccessorTypePublicMethod($property, $getterInit, $setterInit

self::assertEquals($getterName, $metadata->getter);
self::assertEquals($setterName, $metadata->setter);

$metadata->setValue($object, 'x');

self::assertEquals(sprintf('%1$s:%1$s:x', strtoupper($property)), $metadata->getValue($object));
}

/**
Expand Down

0 comments on commit d68ec99

Please sign in to comment.