-
-
Notifications
You must be signed in to change notification settings - Fork 587
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
||
/** | ||
* @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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has to be fast! very fast! 🤔 |
||
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); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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,
vs
so cant be done