Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Commit

Permalink
Added Object into expression context to base expressions off of value…
Browse files Browse the repository at this point in the history
…s in current object being serialized
  • Loading branch information
jmcclell committed Jan 16, 2016
1 parent 7416581 commit 3abdba0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <jason_mcclellan@jasonmcclellan.io>
*/
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -129,8 +132,6 @@ public static function getSubscribedEvents()
return array(
array('event' => 'serializer.pre_serialize', 'method' => 'onPreSerialize')
);

}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
21 changes: 21 additions & 0 deletions tests/JLM/SerializerExpression/Tests/Model/Nested/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace JLM\SerializerExpression\Tests\Model\Nested;

use JLM\SerializerExpression\Annotation\ExcludeIf;

class Node
{
public $id;

/**
* @ExcludeIf("object.children === []")
*/
public $children = [];

public function __construct($id, array $children = [])
{
$this->id = $id;
$this->children = $children;
}
}

0 comments on commit 3abdba0

Please sign in to comment.