public function add($rel, $href, array $embed = null, array $attributes = null, array $excludeIf = null) { $relationMetadata = new RelationMetadata($rel); if (is_array($href)) { if (!isset($href['route'])) { throw new \RuntimeException('href\'s "route" is required.'); } $relationMetadata->setRoute($href['route']); if (isset($href['parameters'])) { $relationMetadata->setParams($href['parameters']); } if (isset($href['options'])) { $relationMetadata->setOptions($href['options']); } } else { $relationMetadata->setUrl($href); } if (null !== $embed) { if (!empty($embed['provider']) && !empty($embed['property'])) { throw new \RuntimeException('Content configuration can only have either a provider or a property.'); } if (empty($embed['provider']) && empty($embed['property'])) { throw new \RuntimeException('Content configuration needs either a provider or a property.'); } if (isset($embed['provider']) && 2 !== count($embed['provider'])) { throw new \RuntimeException('Content "provider" is required, and should be an array of 2 values. [service, method]'); } if (!empty($embed['provider'])) { $providerId = $embed['provider'][0]; $providerMethod = $embed['provider'][1]; } else { $providerId = 'fsc_hateoas.factory.identity'; $providerMethod = 'get'; } $contentMetadata = new RelationContentMetadata($providerId, $providerMethod); if (isset($embed['providerArguments'])) { $contentMetadata->setProviderArguments($embed['providerArguments']); } if (isset($embed['property'])) { $contentMetadata->setProviderArguments(array($embed['property'])); } if (isset($embed['serializerType'])) { $contentMetadata->setSerializerType($embed['serializerType']); } if (isset($embed['serializerXmlElementName'])) { $contentMetadata->setSerializerXmlElementName($embed['serializerXmlElementName']); } if (isset($embed['serializerXmlElementRootMetadata'])) { $contentMetadata->setSerializerXmlElementRootName($embed['serializerXmlElementRootMetadata']); } $relationMetadata->setContent($contentMetadata); } if (null !== $attributes) { $relationMetadata->setAttributes($attributes); } if (null !== $excludeIf) { $relationMetadata->setExcludeIf($excludeIf); } $this->relationsMetadata[] = $relationMetadata; }
/** * {@inheritdoc} */ public function loadMetadataForClass(\ReflectionClass $class) { $annotations = $this->reader->getClassAnnotations($class); if (0 == count($annotations)) { return null; } $classMetadata = new ClassMetadata($name = $class->getName()); $classMetadata->fileResources[] = $class->getFilename(); foreach ($annotations as $annotation) { if ($annotation instanceof Annotation\Relation) { $relationMetadata = new RelationMetadata($annotation->rel); if (null !== $annotation->excludeIf) { $relationMetadata->setExcludeIf($annotation->excludeIf); } if ($annotation->href instanceof Annotation\Route) { $relationMetadata->setRoute($annotation->href->value); if (!empty($annotation->href->parameters)) { $relationMetadata->setParams($annotation->href->parameters); } if (!empty($annotation->href->options)) { $relationMetadata->setOptions($annotation->href->options); } } else { $relationMetadata->setUrl($annotation->href); } if (null !== $annotation->attributes) { $relationMetadata->setAttributes($annotation->attributes); } if (null !== $annotation->embed && $annotation->embed instanceof Annotation\Content) { if (!(empty($annotation->embed->provider) xor empty($annotation->embed->property))) { throw new \RuntimeException('The @Content annotation needs either a provider or a property.'); } $providerId = $providerMethod = $providerArguments = null; if (!empty($annotation->embed->provider)) { if (2 !== count($annotation->embed->provider)) { throw new \RuntimeException('The @Content provider parameters should be an array of 2 values, a service id and a method.'); } $providerId = $annotation->embed->provider[0]; $providerMethod = $annotation->embed->provider[1]; $providerArguments = $annotation->embed->providerArguments ?: array(); } else { $providerId = 'fsc_hateoas.factory.identity'; $providerMethod = 'get'; $providerArguments = array($annotation->embed->property); } $relationContentMetadata = new RelationContentMetadata($providerId, $providerMethod); $relationContentMetadata->setProviderArguments($providerArguments); $relationMetadata->setContent($relationContentMetadata); $relationContentMetadata->setSerializerType($annotation->embed->serializerType); $relationContentMetadata->setSerializerXmlElementName($annotation->embed->serializerXmlElementName); if (null !== $annotation->embed->serializerXmlElementNameRootMetadata) { $relationContentMetadata->setSerializerXmlElementRootName($annotation->embed->serializerXmlElementNameRootMetadata); } } $classMetadata->addRelation($relationMetadata); } } return $classMetadata; }
/** * {@inheritdoc} */ protected function loadMetadataFromFile(\ReflectionClass $class, $file) { $config = Yaml::parse(file_get_contents($file)); if (!isset($config[$name = $class->getName()])) { throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $name, $file)); } $config = $config[$name]; $classMetadata = new ClassMetadata($name); if (isset($config['relations'])) { foreach ($config['relations'] as $relation) { if (!isset($relation['href'])) { throw new \RuntimeException('The "href" relation parameter is required.'); } $relationMetadata = new RelationMetadata($relation['rel']); if (is_array($relation['href'])) { $relationMetadata->setRoute($relation['href']['route']); if (isset($relation['href']['parameters'])) { $relationMetadata->setParams($relation['href']['parameters']); } if (isset($relation['href']['options'])) { $relationMetadata->setOptions($relation['href']['options']); } } else { $relationMetadata->setUrl($relation['href']); } if (!empty($relation['attributes'])) { $relationMetadata->setAttributes($relation['attributes']); } if (!empty($relation['exclude_if'])) { $relationMetadata->setExcludeIf($relation['exclude_if']); } if (!empty($relation['content'])) { $relationContent = $relation['content']; if (!empty($relationContent['provider_id']) && !empty($relationContent['property'])) { throw new \RuntimeException('The content configuration can only have either a provider or a property.'); } if (!empty($relationContent['provider_id']) && empty($relationContent['provider_method'])) { throw new \RuntimeException('The content configuration needs a "provider_method" when using "provider_id".'); } $providerId = $providerMethod = $providerArguments = null; if (!empty($relationContent['provider_id'])) { $providerId = $relationContent['provider_id']; $providerMethod = $relationContent['provider_method']; $providerArguments = isset($relationContent['provider_arguments']) ? $relationContent['provider_arguments'] : array(); } elseif (!empty($relationContent['property'])) { $providerId = 'fsc_hateoas.factory.identity'; $providerMethod = 'get'; $providerArguments = array($relationContent['property']); } $relationContentMetadata = new RelationContentMetadata($providerId, $providerMethod); $relationContentMetadata->setProviderArguments($providerArguments); $relationMetadata->setContent($relationContentMetadata); if (isset($relationContent['serializer_type'])) { $relationContentMetadata->setSerializerType($relationContent['serializer_type']); } if (isset($relationContent['serializer_xml_element_name'])) { $relationContentMetadata->setSerializerXmlElementName($relationContent['serializer_xml_element_name']); } if (array_key_exists('serializer_xml_element_name_root_metadata', $relationContent)) { $relationContentMetadata->setSerializerXmlElementRootName($relationContent['serializer_xml_element_name_root_metadata']); } } $classMetadata->addRelation($relationMetadata); } } return $classMetadata; }