Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $annotations = $this->reader->getClassAnnotations($class);
     if (0 === count($annotations)) {
         return null;
     }
     $classMetadata = new ClassMetadata($class->getName());
     $classMetadata->fileResources[] = $class->getFileName();
     foreach ($annotations as $annotation) {
         if ($annotation instanceof Annotation\Resource) {
             // auto transform type from class name
             if (!$annotation->type) {
                 $annotation->type = String::dasherize($class->getShortName());
             }
             $classMetadata->setResource(new Resource($annotation->type, $annotation->showLinkSelf));
         }
     }
     $classProperties = $class->getProperties();
     foreach ($classProperties as $property) {
         $annotations = $this->reader->getPropertyAnnotations($property);
         foreach ($annotations as $annotation) {
             if ($annotation instanceof Annotation\Id) {
                 $classMetadata->setIdField($property->getName());
             } else {
                 if ($annotation instanceof Annotation\Relationship) {
                     $classMetadata->addRelationship(new Relationship($property->getName(), $annotation->includeByDefault, $annotation->showLinkSelf, $annotation->showLinkRelated));
                 }
             }
         }
     }
     return $classMetadata;
 }
Exemplo n.º 2
0
 /**
  * {@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];
     if (isset($config['resource'])) {
         $classMetadata = new ClassMetadata($name);
         $classMetadata->fileResources[] = $file;
         $classMetadata->fileResources[] = $class->getFileName();
         $classMetadata->setResource($this->parseResource($config, $class));
         if (isset($config['relations'])) {
             foreach ($config['relations'] as $name => $relation) {
                 $classMetadata->addRelationship(new Relationship($name, isset($relation['includeByDefault']) ? $relation['includeByDefault'] : false, isset($relation['showLinkSelf']) ? $relation['showLinkSelf'] : false, isset($relation['showLinkRelated']) ? $relation['showLinkRelated'] : false));
             }
         }
         return $classMetadata;
     }
 }
 /**
  * @param ClassMetadata $classMetadata
  * @param               $id
  *
  * @return bool
  */
 protected function canIncludeRelationship(ClassMetadata $classMetadata, $id)
 {
     foreach ($this->includedRelationships as $includedRelationship) {
         if ($includedRelationship['type'] === $classMetadata->getResource()->getType() && $includedRelationship['id'] === $id) {
             return false;
         }
     }
     return true;
 }