diff --git a/Rest/Metadata/Reference.php b/Rest/Metadata/Reference.php index ce76c4c..94ecebe 100644 --- a/Rest/Metadata/Reference.php +++ b/Rest/Metadata/Reference.php @@ -16,4 +16,27 @@ final class Reference * @var string */ public $field = 'id'; -} \ No newline at end of file + + /** + * @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; + } +} diff --git a/Serializer/Normalizer/AttributeExtractionStrategy.php b/Serializer/Normalizer/AttributeExtractionStrategy.php index 9030049..64fc290 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']) && is_array($context['groups'])) { + if (count(array_intersect($context['groups'], $referenceMetadata->groups)) > 0) { + return $propertyValue; + } + } + if ($propertyValue instanceof \Traversable) { $value = []; @@ -97,4 +103,4 @@ private function isExpand($attribute, $context) return false; } -} \ No newline at end of file +}