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

Make access strategies aware of the context #962

Merged
merged 3 commits into from
Jun 12, 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
6 changes: 4 additions & 2 deletions src/Accessor/AccessorStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace JMS\Serializer\Accessor;

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\SerializationContext;

/**
* @author Asmir Mustafic <[email protected]>
Expand All @@ -16,13 +18,13 @@ interface AccessorStrategyInterface
* @param PropertyMetadata $metadata
* @return mixed
*/
public function getValue(object $object, PropertyMetadata $metadata);
public function getValue(object $object, PropertyMetadata $metadata, SerializationContext $context);

/**
* @param object $object
* @param mixed $value
* @param PropertyMetadata $metadata
* @return void
*/
public function setValue(object $object, $value, PropertyMetadata $metadata): void;
public function setValue(object $object, $value, PropertyMetadata $metadata, DeserializationContext $context): void;
}
9 changes: 5 additions & 4 deletions src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace JMS\Serializer\Accessor;

use JMS\Serializer\DeserializationContext;
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\SerializationContext;

/**
* @author Asmir Mustafic <[email protected]>
Expand All @@ -30,7 +32,7 @@ public function __construct(ExpressionEvaluatorInterface $evaluator = null)
$this->evaluator = $evaluator;
}

public function getValue(object $object, PropertyMetadata $metadata)
public function getValue(object $object, PropertyMetadata $metadata, SerializationContext $context)
{
if ($metadata instanceof StaticPropertyMetadata) {
return $metadata->getValue(null);
Expand All @@ -40,8 +42,7 @@ public function getValue(object $object, PropertyMetadata $metadata)
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]);
return $this->evaluator->evaluate($metadata->expression, ['object' => $object, 'context' => $context, 'property_metadata' => $metadata ]);
}

if (null === $metadata->getter) {
Expand Down Expand Up @@ -71,7 +72,7 @@ public function getValue(object $object, PropertyMetadata $metadata)
return $object->{$metadata->getter}();
}

public function setValue(object $object, $value, PropertyMetadata $metadata): void
public function setValue(object $object, $value, PropertyMetadata $metadata, DeserializationContext $context): void
{
if ($metadata->readOnly) {
throw new LogicException(sprintf('%s on %s is read only.', $metadata->name, $metadata->class));
Expand Down
2 changes: 1 addition & 1 deletion src/GraphNavigator/DeserializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function accept($data, array $type = null)
$this->context->pushPropertyMetadata($propertyMetadata);
try {
$v = $this->visitor->visitProperty($propertyMetadata, $data);
$this->accessor->setValue($object, $v, $propertyMetadata);
$this->accessor->setValue($object, $v, $propertyMetadata, $this->context);
} catch (NotAcceptableException $e) {

}
Expand Down
2 changes: 1 addition & 1 deletion src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function accept($data, array $type = null)
continue;
}

$v = $this->accessor->getValue($data, $propertyMetadata);
$v = $this->accessor->getValue($data, $propertyMetadata, $this->context);

if (null === $v && $this->shouldSerializeNull !== true) {
continue;
Expand Down
30 changes: 30 additions & 0 deletions tests/Fixtures/AuthorExpressionAccessContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation as Serializer;

/**
* @Serializer\VirtualProperty("firstName", exp="object.getFirstName()")
* @Serializer\VirtualProperty("direction", exp="context.getDirection()")
* @Serializer\VirtualProperty("name", exp="property_metadata.name")
*/
class AuthorExpressionAccessContext
{
/**
* @Serializer\Exclude()
*/
private $firstName;

public function __construct($firstName)
{
$this->firstName = $firstName;
}

public function getFirstName()
{
return $this->firstName;
}
}
14 changes: 14 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use JMS\Serializer\Tests\Fixtures\Article;
use JMS\Serializer\Tests\Fixtures\Author;
use JMS\Serializer\Tests\Fixtures\AuthorExpressionAccess;
use JMS\Serializer\Tests\Fixtures\AuthorExpressionAccessContext;
use JMS\Serializer\Tests\Fixtures\AuthorList;
use JMS\Serializer\Tests\Fixtures\AuthorReadOnly;
use JMS\Serializer\Tests\Fixtures\AuthorReadOnlyPerClass;
Expand Down Expand Up @@ -741,6 +742,19 @@ public function testExpressionAuthor()
self::assertEquals($this->getContent('author_expression'), $serializer->serialize($author, $this->getFormat()));
}

public function testExpressionAuthorWithContextVars()
{
$evaluator = new ExpressionEvaluator(new ExpressionLanguage());

$builder = SerializerBuilder::create();
$builder->setExpressionEvaluator($evaluator);
$serializer = $builder->build();

$author = new AuthorExpressionAccessContext("Ruud");
self::assertEquals($this->getContent('author_expression_context'), $serializer->serialize($author, $this->getFormat()));
}


/**
* @expectedException \JMS\Serializer\Exception\ExpressionLanguageRequiredException
* @expectedExceptionMessage The property firstName on JMS\Serializer\Tests\Fixtures\AuthorExpressionAccess requires the expression accessor strategy to be enabled.
Expand Down
1 change: 1 addition & 0 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ protected function getContent($key)
$outputs['object_with_object_property_no_array_to_author'] = '{"foo": "bar", "author": "baz"}';
$outputs['object_with_object_property'] = '{"foo": "bar", "author": {"full_name": "baz"}}';
$outputs['author_expression'] = '{"my_first_name":"Ruud","last_name":"Kamphuis","id":123}';
$outputs['author_expression_context'] = '{"first_name":"Ruud","direction":1,"name":"name"}';
$outputs['maxdepth_skippabe_object'] = '{"a":{"xxx":"yyy"}}';
$outputs['array_objects_nullable'] = '[]';
$outputs['type_casting'] = '{"as_string":"8"}';
Expand Down
6 changes: 6 additions & 0 deletions tests/Serializer/xml/author_expression_context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<first_name><![CDATA[Ruud]]></first_name>
<direction>1</direction>
<name><![CDATA[name]]></name>
</result>