/** * Gets HAL components of the resource: states, links and embedded. * * @param object $object * @param string|null $format * @param array $context * * @return array */ private function getComponents($object, string $format = null, array $context) { if (isset($this->componentsCache[$context['cache_key']])) { return $this->componentsCache[$context['cache_key']]; } $attributes = parent::getAttributes($object, $format, $context); $options = $this->getFactoryOptions($context); $components = ['states' => [], 'links' => [], 'embedded' => []]; foreach ($attributes as $attribute) { $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options); $type = $propertyMetadata->getType(); $isOne = $isMany = false; if (null !== $type) { if ($type->isCollection()) { $valueType = $type->getCollectionValueType(); $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className); } else { $className = $type->getClassName(); $isOne = $className && $this->resourceClassResolver->isResourceClass($className); } } if (!$isOne && !$isMany) { $components['states'][] = $attribute; continue; } $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many']; if ($propertyMetadata->isReadableLink()) { $components['embedded'][] = $relation; } $components['links'][] = $relation; } return $this->componentsCache[$context['cache_key']] = $components; }