From 38ff0b4fc2507e994ea268b9c2dd800b83a76682 Mon Sep 17 00:00:00 2001 From: Alexander Andrienko Date: Wed, 13 Nov 2019 15:30:02 +0200 Subject: [PATCH 1/3] added ability to specify groups in reference --- Rest/Metadata/Reference.php | 23 +++++++++++++++++++ .../AttributeExtractionStrategy.php | 6 +++++ 2 files changed, 29 insertions(+) diff --git a/Rest/Metadata/Reference.php b/Rest/Metadata/Reference.php index ce76c4c..9be3d7b 100644 --- a/Rest/Metadata/Reference.php +++ b/Rest/Metadata/Reference.php @@ -16,4 +16,27 @@ final class Reference * @var string */ public $field = 'id'; + + /** + * @var string[] + */ + public $groups; + + /** + * @param array $data + * + * @throws InvalidArgumentException + */ + public function __construct(array $data) + { + $groups = isset($data['groups']) ? (array) $data['groups'] : []; + + foreach ($groups as $group) { + if (!\is_string($group)) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of strings.', \get_class($this))); + } + } + + $this->groups = $groups; + } } \ No newline at end of file diff --git a/Serializer/Normalizer/AttributeExtractionStrategy.php b/Serializer/Normalizer/AttributeExtractionStrategy.php index 9030049..d5c2d84 100644 --- a/Serializer/Normalizer/AttributeExtractionStrategy.php +++ b/Serializer/Normalizer/AttributeExtractionStrategy.php @@ -65,6 +65,12 @@ public function getValue($object, $attribute, $format = null, array $context = a $property = $referenceMetadata->field; + if (isset($context['groups'])) { + if (count(array_intersect($context['groups'], $referenceMetadata->groups)) > 0) { + return $propertyValue; + } + } + if ($propertyValue instanceof \Traversable) { $value = []; From ab93439c86c6cab74fb6cb107d7c66f95686a7f1 Mon Sep 17 00:00:00 2001 From: Alexander Andrienko <57679991+alexander-andrienko-req@users.noreply.github.com> Date: Wed, 13 Nov 2019 16:34:34 +0200 Subject: [PATCH 2/3] Update Reference.php --- Rest/Metadata/Reference.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rest/Metadata/Reference.php b/Rest/Metadata/Reference.php index 9be3d7b..94ecebe 100644 --- a/Rest/Metadata/Reference.php +++ b/Rest/Metadata/Reference.php @@ -39,4 +39,4 @@ public function __construct(array $data) $this->groups = $groups; } -} \ No newline at end of file +} From a7ba26c3570f178dfbd5423244f0da0676503abd Mon Sep 17 00:00:00 2001 From: Alexander Andrienko <57679991+alexander-andrienko-req@users.noreply.github.com> Date: Wed, 13 Nov 2019 16:43:07 +0200 Subject: [PATCH 3/3] Update AttributeExtractionStrategy.php --- Serializer/Normalizer/AttributeExtractionStrategy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Serializer/Normalizer/AttributeExtractionStrategy.php b/Serializer/Normalizer/AttributeExtractionStrategy.php index d5c2d84..64fc290 100644 --- a/Serializer/Normalizer/AttributeExtractionStrategy.php +++ b/Serializer/Normalizer/AttributeExtractionStrategy.php @@ -65,7 +65,7 @@ public function getValue($object, $attribute, $format = null, array $context = a $property = $referenceMetadata->field; - if (isset($context['groups'])) { + if (isset($context['groups']) && is_array($context['groups'])) { if (count(array_intersect($context['groups'], $referenceMetadata->groups)) > 0) { return $propertyValue; } @@ -103,4 +103,4 @@ private function isExpand($attribute, $context) return false; } -} \ No newline at end of file +}