Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use property metadata to get/set object values #934

Merged
merged 3 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,76 @@

namespace JMS\Serializer\Accessor;

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inho one shared accessor is enough, no? They are bound to type only.

Copy link
Collaborator Author

@goetas goetas May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body of the closure is different,

function ($o, $name, $value) {
   $o->$name = $value;
}

vs

function ($o, $name) {
   return $o->$name;
}

so cant be done


/**
* @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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. This bunch of IFs calls for a strategy pattern imho, this is a bit too coupled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be fast! very fast! 🤔
not sure if having extra classes to load and call will influence on it

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 ($metadata->readOnly) {
throw new LogicException(sprintf('%s on %s is read only.'), $metadata->name, $metadata->class);
}

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