-
-
Notifications
You must be signed in to change notification settings - Fork 585
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
[feature] exclusion groups #539
Changes from all commits
931914c
91092a9
c4efccc
48bf176
8281c7a
e6c9a16
6d11336
8ca1299
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 |
---|---|---|
|
@@ -24,4 +24,6 @@ | |
*/ | ||
final class Exclude | ||
{ | ||
/** @var array<string> */ | ||
public $groups; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace JMS\Serializer\Exclusion; | ||
|
||
use JMS\Serializer\Context; | ||
use JMS\Serializer\Metadata\ClassMetadata; | ||
use JMS\Serializer\Metadata\PropertyMetadata; | ||
|
||
class ExclusionGroupsExclusionStrategy implements ExclusionStrategyInterface | ||
{ | ||
private $groups = array(); | ||
|
||
public function __construct(array $groups) | ||
{ | ||
foreach ($groups as $group) { | ||
$this->groups[$group] = true; | ||
} | ||
} | ||
|
||
/** | ||
* Whether the class should be skipped. | ||
* | ||
* @param ClassMetadata $classMetadata | ||
* | ||
* @param Context $context | ||
* @return bool | ||
*/ | ||
public function shouldSkipClass(ClassMetadata $classMetadata, Context $context) | ||
{ | ||
return $this->shouldSkipGroup($classMetadata->exclusionGroups); | ||
} | ||
|
||
/** | ||
* Whether the property should be skipped. | ||
* | ||
* @param PropertyMetadata $propertyMetadata | ||
* | ||
* @param Context $context | ||
* @return bool | ||
*/ | ||
public function shouldSkipProperty(PropertyMetadata $propertyMetadata, Context $context) | ||
{ | ||
return $this->shouldSkipGroup($propertyMetadata->exclusionGroups); | ||
} | ||
|
||
/** | ||
* @param $metadataGroups | ||
* @return bool | ||
*/ | ||
private function shouldSkipGroup($metadataGroups) { | ||
if (false === $metadataGroups) { | ||
return false; | ||
} | ||
if (is_array($metadataGroups) && 0 == count($metadataGroups)) { | ||
return true; | ||
} | ||
if (!empty($this->groups) && array_intersect(array_keys($this->groups), $metadataGroups)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ class ClassMetadata extends MergeableClassMetadata | |
public $accessorOrder; | ||
public $customOrder; | ||
public $handlerCallbacks = array(); | ||
public $exclusionGroups = false; | ||
|
||
public $discriminatorDisabled = false; | ||
public $discriminatorBaseClass; | ||
|
@@ -225,6 +226,7 @@ public function serialize() | |
$this->accessorOrder, | ||
$this->customOrder, | ||
$this->handlerCallbacks, | ||
$this->exclusionGroups, | ||
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 will cause an error if will be deployed without cache cleaning, see https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/Metadata/PropertyMetadata.php#L169 and https://github.com/schmittjoh/serializer/blob/master/src/JMS/Serializer/Metadata/PropertyMetadata.php#L137 for a safer implementation |
||
$this->discriminatorDisabled, | ||
$this->discriminatorBaseClass, | ||
$this->discriminatorFieldName, | ||
|
@@ -246,6 +248,7 @@ public function unserialize($str) | |
$this->accessorOrder, | ||
$this->customOrder, | ||
$this->handlerCallbacks, | ||
$this->exclusionGroups, | ||
$this->discriminatorDisabled, | ||
$this->discriminatorBaseClass, | ||
$this->discriminatorFieldName, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace JMS\Serializer\Tests\Fixtures; | ||
|
||
use JMS\Serializer\Annotation\Exclude; | ||
use JMS\Serializer\Annotation\Type; | ||
|
||
/** | ||
* @Exclude({"noClass"}) | ||
*/ | ||
class ExclusionGroupsObject | ||
{ | ||
/** | ||
* @Type("string") | ||
* @Exclude({"testExclusionGroup1", "testExclusionGroup2"}) | ||
*/ | ||
public $foo = 'foo'; | ||
|
||
/** | ||
* @Type("string") | ||
* @Exclude({"testExclusionGroup1"}) | ||
*/ | ||
public $foo2 = 'foo2'; | ||
|
||
/** | ||
* @Type("string") | ||
*/ | ||
public $bar = 'bar'; | ||
|
||
/** | ||
* @Type("string") | ||
* @Exclude() | ||
*/ | ||
public $neverShown = 'nevershown'; | ||
} |
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.
isn't more appropriate use
count
instead ofempty
?