From 3abdba0c202bcc834654d0a5f97bb1a3c8ffdb37 Mon Sep 17 00:00:00 2001 From: Jason McClellan Date: Fri, 15 Jan 2016 18:09:57 -0500 Subject: [PATCH] Added Object into expression context to base expressions off of values in current object being serialized --- .../ExpressionBasedExclusionStrategy.php | 21 ++++++------ .../ExpressionBasedExclusionStrategyTest.php | 33 +++++++++++++++++++ .../Tests/Model/Nested/Node.php | 21 ++++++++++++ 3 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 tests/JLM/SerializerExpression/Tests/Model/Nested/Node.php diff --git a/src/JLM/SerializerExpression/Exclusion/ExpressionBasedExclusionStrategy.php b/src/JLM/SerializerExpression/Exclusion/ExpressionBasedExclusionStrategy.php index 4deedeb..101cb21 100644 --- a/src/JLM/SerializerExpression/Exclusion/ExpressionBasedExclusionStrategy.php +++ b/src/JLM/SerializerExpression/Exclusion/ExpressionBasedExclusionStrategy.php @@ -16,7 +16,7 @@ use Symfony\Component\ExpressionLanguage\Expression; /** - * Exclusion strategy based on Symfony expression langauge component + * Exclusion strategy based on Symfony expression langauge component * * @author Jason McClellan */ @@ -25,6 +25,8 @@ class ExpressionBasedExclusionStrategy implements ExclusionStrategyInterface, Se protected $metadataFactory; protected $expressionLanguage; + protected $currentObject; + public function __construct(MetadataFactory $metadataFactory, ExpressionLanguage $expressionLanguage) { $this->metadataFactory = $metadataFactory; @@ -36,12 +38,12 @@ public function __construct(MetadataFactory $metadataFactory, ExpressionLanguage * * This class only supports property level exclusion * - * @param ClassMetadata $metadata The JMS Serializer metadata for the class + * @param ClassMetadata $metadata The JMS Serializer metadata for the class * * @return boolean */ public function shouldSkipClass(ClassMetadata $class, Context $context) - { + { return false; } @@ -66,17 +68,19 @@ public function shouldSkipProperty(BasePropertyMetadata $property, Context $cont return (bool)$this->expressionLanguage->evaluate($expression, array( 'classMetadata' => $classMetadata, 'propertyMetadata' => $propertyMetadata, + 'object' => $this->currentObject, 'context' => $context)); } elseif (null !== $propertyMetadata->inclusionExpression) { $expression = new Expression($propertyMetadata->inclusionExpression); return (bool)!$this->expressionLanguage->evaluate($expression, array( 'classMetadata' => $classMetadata, 'propertyMetadata' => $propertyMetadata, + 'object' => $this->currentObject, 'context' => $context)); } } - } - + } + return false; } @@ -92,13 +96,12 @@ public function shouldSkipProperty(BasePropertyMetadata $property, Context $cont */ public function onPreSerialize(PreSerializeEvent $event) { - $object = $event->getObject(); + $this->currentObject = $object = $event->getObject(); if(!is_object($object)) { return; } - $class = get_class($object); $classMetadata = $this->metadataFactory->getMetadataForClass($class); $jmsClassMetadata = $event->getContext()->getMetadataFactory()->getMetadataForClass($class); @@ -129,8 +132,6 @@ public static function getSubscribedEvents() return array( array('event' => 'serializer.pre_serialize', 'method' => 'onPreSerialize') ); - } +} - -} \ No newline at end of file diff --git a/tests/JLM/SerializerExpression/Tests/ExpressionBasedExclusionStrategyTest.php b/tests/JLM/SerializerExpression/Tests/ExpressionBasedExclusionStrategyTest.php index 7025ab1..b46f512 100644 --- a/tests/JLM/SerializerExpression/Tests/ExpressionBasedExclusionStrategyTest.php +++ b/tests/JLM/SerializerExpression/Tests/ExpressionBasedExclusionStrategyTest.php @@ -10,6 +10,7 @@ use Doctrine\Common\Annotations\AnnotationReader; +use JLM\SerializerExpression\Tests\Model\Nested\Node; use JLM\SerializerExpression\Tests\Model\NoneExclusionPolicy; use JMS\Serializer\EventDispatcher\EventDispatcher; use JMS\Serializer\Serializer; @@ -81,4 +82,36 @@ public function testNoneExclusionPolicy() $this->assertEquals($expectedKeys, $dataKeys); } + + public function testNestedObjectHasProperObjectContext() + { + $model = new Node("one.one", [ + new Node("two.one", [ + new Node("three.one"), + new Node("three.two"), + ]), + new Node("two.two"), + new Node("two.three"), + ]); + + $data = $this->serialize($model); + $data = json_decode($data, true); + + $expectedData = [ + 'id' => 'one.one', + 'children' => [ + [ + 'id' => 'two.one', + 'children' => [ + ['id' => 'three.one'], + ['id' => 'three.two'], + ], + ], + ['id' => 'two.two'], + ['id' => 'two.three'], + ] + ]; + + $this->assertEquals($expectedData, $data); + } } \ No newline at end of file diff --git a/tests/JLM/SerializerExpression/Tests/Model/Nested/Node.php b/tests/JLM/SerializerExpression/Tests/Model/Nested/Node.php new file mode 100644 index 0000000..d70ea69 --- /dev/null +++ b/tests/JLM/SerializerExpression/Tests/Model/Nested/Node.php @@ -0,0 +1,21 @@ +id = $id; + $this->children = $children; + } +}