Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ public function initialize(string $format, VisitorInterface $visitor, GraphNavig
$this->metadataStack = new \SplStack();

if (isset($this->attributes['groups'])) {
$this->addExclusionStrategy(new GroupsExclusionStrategy($this->attributes['groups']));
$strategy = new GroupsExclusionStrategy($this->attributes['groups']);
Copy link
Copy Markdown
Collaborator

@goetas goetas Feb 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not

$context = // ... get the context somehow
$g = new GroupsExclusionStrategy(['a', 'b'], true);
$context->addExclusionStrategy($g);

?

In that way we do not need to add the enableInheritGroups method in the context. Ideally the context should know the less possible about the various exclusion strategies.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i see, but i can't add 2 GroupsExclusionStrategy, one with inheritedGroups = false and an another one with inheritedGroups = true.

How can i configure this option with JMSerializerBundle for example ?

Thanks for you help


if (isset($this->attributes['inherited_groups']) && is_bool($this->attributes['inherited_groups'])) {
Comment thread
Xen3r0 marked this conversation as resolved.
Outdated
$strategy->setInheritedGroups($this->attributes['inherited_groups']);
}

$this->addExclusionStrategy($strategy);
}

if (isset($this->attributes['version'])) {
Expand Down Expand Up @@ -204,6 +210,16 @@ public function enableMaxDepthChecks(): self
return $this;
}

/**
* @return $this
*/
public function enableInheritGroups(): self
{
$this->attributes['inherit_groups'] = true;
Comment thread
Xen3r0 marked this conversation as resolved.
Outdated

return $this;
}

public function getFormat(): string
{
return $this->format;
Expand Down
11 changes: 10 additions & 1 deletion src/Exclusion/GroupsExclusionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class GroupsExclusionStrategy implements ExclusionStrategyInterface
*/
private $nestedGroups = false;

private $inheritedGroups = false;

public function __construct(array $groups)
{
if (empty($groups)) {
Expand All @@ -44,6 +46,13 @@ public function __construct(array $groups)
}
}

public function setInheritedGroups(bool $inheritedGroups): self
{
$this->inheritedGroups = $inheritedGroups;

return $this;
}

public function shouldSkipClass(ClassMetadata $metadata, Context $navigatorContext): bool
{
return false;
Expand Down Expand Up @@ -96,7 +105,7 @@ public function getGroupsFor(Context $navigatorContext): array
foreach ($paths as $index => $path) {
if (!array_key_exists($path, $groups)) {
if ($index > 0) {
$groups = [self::DEFAULT_GROUP];
$groups = $this->inheritedGroups ? $groups : [self::DEFAULT_GROUP];
} else {
$groups = array_filter($groups, 'is_string') ?: [self::DEFAULT_GROUP];
}
Expand Down
27 changes: 17 additions & 10 deletions tests/Exclusion/GroupsExclusionStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getExclusionRules()
/**
* @dataProvider getGroupsFor
*/
public function testGroupsFor(array $groups, array $propsVisited, array $resultingGroups)
public function testGroupsFor(array $groups, array $propsVisited, bool $inheritGroups, array $resultingGroups)
{
$exclusion = new GroupsExclusionStrategy($groups);
$context = SerializationContext::create();
Expand All @@ -67,27 +67,34 @@ public function testGroupsFor(array $groups, array $propsVisited, array $resulti
$context->pushPropertyMetadata($metadata);
}

$exclusion->setInheritedGroups($inheritGroups);
$groupsFor = $exclusion->getGroupsFor($context);
self::assertEquals($groupsFor, $resultingGroups);
}

public function getGroupsFor()
{
return [
[['foo'], ['prop'], ['foo']],
[[], ['prop'], ['Default']],
[['foo'], ['prop'], false, ['foo']],
[[], ['prop'], false, ['Default']],

[['foo', 'prop' => ['bar']], ['prop'], ['bar']],
[['foo', 'prop' => ['bar']], ['prop2'], ['foo']],
[['foo', 'prop' => ['bar']], ['prop'], false, ['bar']],
[['foo', 'prop' => ['bar']], ['prop2'], false, ['foo']],

[['prop' => ['bar']],['prop2'],['Default']],
[['prop' => ['bar']],['prop2'], false, ['Default']],

[['foo', 'prop' => ['bar']], ['prop', 'prop2'], ['Default']],
[['foo', 'prop' => ['bar']], ['prop', 'prop2'], false, ['Default']],

[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], ['Default']],
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], ['def', 'prop3' => ['def']]],
[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], false, ['Default']],
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], false, ['def', 'prop3' => ['def']]],

[['foo', 'prop' => ['prop2' => ['prop3' => ['def']]]], ['prop', 'prop2'], ['Default', 'prop3' => ['def']]],
[['foo', 'prop' => ['prop2' => ['prop3' => ['def']]]], ['prop', 'prop2'], false, ['Default', 'prop3' => ['def']]],

[['foo'], ['prop'], true, ['foo']],
[[], ['prop'], true, ['Default']],
[['foo', 'prop' => ['xx', 'prop2' => ['def'], 'prop3' => ['def']]], ['prop', 'prop2', 'propB'], true, ['def']],
[['foo', 'prop' => ['xx', 'prop2' => ['def', 'prop3' => ['def']]]], ['prop', 'prop2'], true, ['def', 'prop3' => ['def']]],
[['foo', 'prop' => ['prop2' => ['prop3' => ['def']]]], ['prop', 'prop2'], true, ['Default', 'prop3' => ['def']]],
];
}
}