/**
  * Completes the metadata by setting missing fields that can be inferred
  * by other fields
  *
  * Furthermore, the type of properties which is returned as string by
  * the metadata driver is replaced with a ClassMetadata instance.
  *
  * @param ClassMetadata $class The metadata to complete
  */
 protected function completeMetadata(ClassMetadata $class)
 {
     $className = $class->getName();
     if (null === $class->getIri()) {
         $class->setIri($this->namingStrategy->classIriFragment($className));
     }
     if (null === $class->getExposeAs()) {
         $class->setExposeAs($this->namingStrategy->classShortName($className));
     }
     // If no title has been set for this class, use it's short name
     if (null === $class->getTitle()) {
         $class->setTitle($this->namingStrategy->classShortName($className));
     }
     foreach ($class->getProperties() as $property) {
         $propertyName = $property->getName();
         if (null === $property->getIri()) {
             $property->setIri($this->namingStrategy->propertyIriFragment($className, $propertyName));
         }
         if (null === $property->getExposeAs()) {
             $property->setExposeAs($this->namingStrategy->propertyShortName($className, $propertyName));
         }
         // If no title has been set for this property, use it's short name
         if (null === $property->getTitle()) {
             $property->setTitle($this->namingStrategy->propertyShortName($className, $propertyName));
         }
     }
 }
 /**
  * Document the properties and methods associated to a class
  *
  * @param ClassMetadata $metadata The class definition
  * @param ReflectionClass $class    The class whose properties and
  *                                  methods should be documented.
  */
 private function documentProperties(ClassMetadata $metadata, ReflectionClass $class)
 {
     /*
     
             $interfaces = $class->getInterfaces();
             $linkRelationMethods = array();
             foreach ($interfaces as $interface) {
                 if (null !== $this->getAnnotation($interface, $linkRelationAnnot)) {
                     if (false === isset($documentation['rels'][$interface->name])) {
                         $documentation['rels'][$interface->name] = array();
                         foreach ($interface->getMethods() as $method) {
                             if ($method->isPublic()) {
                                 $documentation['rels'][$interface->name][$method->name] = $interface->name;
                             }
                         }
                     }
     
                     $linkRelationMethods += $documentation['rels'][$interface->name];
                 }
             }
     */
     $properties = array();
     $elements = array_merge($class->getProperties(), $class->getMethods());
     foreach ($elements as $element) {
         $annotation = $this->getAnnotation($element, 'ML\\HydraBundle\\Mapping\\Expose');
         if (null === $annotation) {
             continue;
         }
         // $exposeAs = $element->name;
         // if ($annotation->as) {
         //     $exposeAs = $annotation->as;
         //     if ($annotation->getIri()) {
         //         $property['iri'] = $annotation->getIri();
         //     } else {
         //         $property['iri'] = $exposeClassAs . '/' . $exposeAs;
         //     }
         // } else {
         //     $exposeAs = $this->propertirize($exposeAs);
         //     if ($annotation->getIri()) {
         //         $property['iri'] = $annotation->getIri();
         //     } else {
         //         $property['iri'] = $this->camelize($exposeAs);
         //         $property['iri'][0] = strtolower($property['iri'][0]);
         //         $property['iri'] =  $exposeClassAs . '/' . $property['iri'];
         //     }
         // }
         $property = new PropertyDefinition($class->name, $element->name);
         $property->setExposeAs($annotation->as);
         $property->setIri($annotation->getIri());
         if (null !== $annotation->required) {
             $property->setRequired($annotation->required);
         }
         if (null !== $annotation->readonly) {
             $property->setReadOnly($annotation->readonly);
         }
         if (null !== $annotation->writeonly) {
             $property->setWriteOnly($annotation->writeonly);
         }
         $tmp = $this->getDocBlockText($element);
         $property->setTitle($tmp['title']);
         $property->setDescription($tmp['description']);
         $tmp = $this->getType($element);
         $property->setType($tmp['type']);
         $this->documentRouteAndOperations($property, $element);
         if (null !== ($annotation = $this->getAnnotation($element, 'ML\\HydraBundle\\Mapping\\Collection'))) {
             // TODO Check for conflicting routes!?
             // TODO Check that the IRI template can be filled!?
             $property->setRoute($this->getRouteMetadata($annotation->route));
             if (false === $property->supportsOperation($annotation->route)) {
                 $property->addOperation($this->getRouteMetadata($annotation->route));
             }
             $property->setType('ML\\HydraBundle\\Entity\\Collection');
             $property->setReadOnly(true);
         }
         /*
                     if ($element instanceof ReflectionMethod) {
                         if (array_key_exists($element->name, $linkRelationMethods)) {
                             $property['original_type'] .= ' --- ' . $linkRelationMethods[$element->name] . '::' . $element->name;
                         }
                     }
         */
         // TODO Validate definition, this here isn't the right place to do so, create a metadata factory
         $properties[] = $property;
     }
     // $documentation['class2type'][$class->name] = $exposeClassAs;
     // $documentation['types'][$exposeClassAs] = $result;
     $metadata->setProperties($properties);
 }
Пример #3
0
 /**
  * Documents the properties of the passed ClassMetadata object
  *
  * @param ML\HydraBundle\Mapping\ClassMetadata $class The class metadata
  *
  * @return array The JSON-LD serialization of the class' properties.
  */
 private function documentClassProperties(\ML\HydraBundle\Mapping\ClassMetadata $class)
 {
     $result = array();
     $propertyDomain = $this->getTypeReferenceIri($class->getName());
     foreach ($class->getProperties() as $property) {
         if (0 === strncmp('@', $property->getExposeAs(), 1)) {
             continue;
             // ignore properties that are mapped to keywords
         }
         $result[] = array('property' => $property->isExternalReference() ? $property->getIri() : array('@id' => 'vocab:' . $property->getIri(), '@type' => $property->getRoute() ? 'hydra:Link' : 'rdf:Property', 'label' => $property->getTitle(), 'description' => $property->getDescription(), 'domain' => $propertyDomain, 'range' => $this->getTypeReferenceIri($property->getType()), 'supportedOperation' => $this->documentOperations($property->getOperations())), 'hydra:title' => $property->getTitle(), 'hydra:description' => $property->getDescription(), 'required' => $property->getRequired(), 'readonly' => $property->isReadOnly(), 'writeonly' => $property->isWriteOnly());
     }
     return $result;
 }