This repository has been archived by the owner on Apr 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Object into expression context to base expressions off of value…
…s in current object being serialized
- Loading branch information
Showing
3 changed files
with
65 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
|
@@ -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') | ||
); | ||
|
||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/JLM/SerializerExpression/Tests/Model/Nested/Node.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |