/**
  * {@inheritDoc}
  */
 public function loadMetadata($class)
 {
     // Try get object annotation from class
     $objectAnnotation = null;
     $classAnnotations = Reflection::loadClassAnnotations($this->reader, $class, true);
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof ObjectAnnotation) {
             if ($objectAnnotation) {
                 throw new \RuntimeException(sprintf('Many @Transformer\\Object annotation in class "%s".', $class));
             }
             $objectAnnotation = $classAnnotation;
         }
     }
     if (!$objectAnnotation) {
         throw new TransformAnnotationNotFoundException(sprintf('Not found @Object annotations in class "%s".', $class));
     }
     // Try get properties annotations
     $properties = [];
     $classProperties = Reflection::getClassProperties($class, true);
     foreach ($classProperties as $classProperty) {
         $propertyAnnotations = $this->reader->getPropertyAnnotations($classProperty);
         foreach ($propertyAnnotations as $propertyAnnotation) {
             if ($propertyAnnotation instanceof PropertyAnnotation) {
                 $property = new PropertyMetadata($propertyAnnotation->propertyName ?: $classProperty->getName(), $propertyAnnotation->groups, $propertyAnnotation->shouldTransform, $propertyAnnotation->expressionValue);
                 $properties[$classProperty->getName()] = $property;
             }
         }
     }
     return new ObjectMetadata($objectAnnotation->transformedClass, $properties);
 }
Exemplo n.º 2
0
 /**
  * Load metadata class
  *
  * @param \ReflectionClass $class
  * @return ClassMetadata
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $classMetadata = new ClassMetadata($class->name);
     $classMetadata->fileResources[] = $class->getFileName();
     foreach ($this->reader->getClassAnnotations($class) as $annotation) {
         if ($annotation instanceof NamespaceNode) {
             $classMetadata->addGraphNamespace($annotation);
         }
         if ($annotation instanceof GraphNode) {
             $classMetadata->addGraphMetadata($annotation, new MetadataValue($annotation->value));
         }
     }
     foreach ($class->getProperties() as $property) {
         foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
             if ($annotation instanceof GraphNode) {
                 $classMetadata->addGraphMetadata($annotation, new PropertyMetadata($class->name, $property->name));
             }
         }
     }
     foreach ($class->getMethods() as $method) {
         foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
             if ($annotation instanceof GraphNode) {
                 $classMetadata->addGraphMetadata($annotation, new MethodMetadata($class->name, $method->name));
             }
         }
     }
     return $classMetadata;
 }
Exemplo n.º 3
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.º 4
0
 /**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     $reflClass = $metadata->getReflectionClass();
     $className = $reflClass->name;
     $loaded = false;
     foreach ($reflClass->getProperties() as $property) {
         if ($property->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
                 if ($groups instanceof Groups) {
                     foreach ($groups->getGroups() as $group) {
                         $metadata->addAttributeGroup($property->name, $group);
                     }
                 }
                 $loaded = true;
             }
         }
     }
     foreach ($reflClass->getMethods() as $method) {
         if ($method->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getMethodAnnotations($method) as $groups) {
                 if ($groups instanceof Groups) {
                     if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
                         foreach ($groups->getGroups() as $group) {
                             $metadata->addAttributeGroup(lcfirst($matches[2]), $group);
                         }
                     } else {
                         throw new \BadMethodCallException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get" or "is".', $className, $method->name));
                     }
                 }
                 $loaded = true;
             }
         }
     }
     return $loaded;
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
 {
     $reflectionClass = $classMetadata->getReflectionClass();
     $className = $reflectionClass->name;
     $loaded = false;
     $attributesMetadata = $classMetadata->getAttributesMetadata();
     foreach ($reflectionClass->getProperties() as $property) {
         if (!isset($attributesMetadata[$property->name])) {
             $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
             $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
         }
         if ($property->getDeclaringClass()->name === $className) {
             foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
                 if ($annotation instanceof Groups) {
                     foreach ($annotation->getGroups() as $group) {
                         $attributesMetadata[$property->name]->addGroup($group);
                     }
                 } elseif ($annotation instanceof MaxDepth) {
                     $attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
                 }
                 $loaded = true;
             }
         }
     }
     foreach ($reflectionClass->getMethods() as $method) {
         if ($method->getDeclaringClass()->name !== $className) {
             continue;
         }
         $accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
         if ($accessorOrMutator) {
             $attributeName = lcfirst($matches[2]);
             if (isset($attributesMetadata[$attributeName])) {
                 $attributeMetadata = $attributesMetadata[$attributeName];
             } else {
                 $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
                 $classMetadata->addAttributeMetadata($attributeMetadata);
             }
         }
         foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
             if ($annotation instanceof Groups) {
                 if (!$accessorOrMutator) {
                     throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
                 }
                 foreach ($annotation->getGroups() as $group) {
                     $attributeMetadata->addGroup($group);
                 }
             } elseif ($annotation instanceof MaxDepth) {
                 if (!$accessorOrMutator) {
                     throw new MappingException(sprintf('MaxDepth on "%s::%s" cannot be added. MaxDepth can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
                 }
                 $attributeMetadata->setMaxDepth($annotation->getMaxDepth());
             }
             $loaded = true;
         }
     }
     return $loaded;
 }
 public function create($class, \ReflectionProperty $reflectionProperty)
 {
     $annotations = $this->reader->getPropertyAnnotations($reflectionProperty);
     foreach ($annotations as $annotation) {
         if ($annotation instanceof GraphId) {
             return new IdAnnotationMetadata();
         }
     }
 }
Exemplo n.º 7
0
 /**
  * @param \ReflectionClass $reflectionClass
  * @return Annotation[]
  * @throws AnnotationReaderException
  */
 private function readPropertyAnnotations(\ReflectionClass $reflectionClass) : array
 {
     return array_reduce($reflectionClass->getProperties(), function (array $accumulator, \ReflectionProperty $reflectionProperty) {
         /* @var $annotations \Doctrine\Common\Annotations\Annotation[] */
         $annotations = array_filter($this->reader->getPropertyAnnotations($reflectionProperty), function ($annotation) : bool {
             return $annotation instanceof Annotation;
         });
         if (empty($annotations)) {
             return $accumulator;
         }
         return array_merge($accumulator, $this->processPropertyAnnotations($annotations, $reflectionProperty));
     }, []);
 }
Exemplo n.º 8
0
 /**
  * @param \ReflectionProperty $property
  *
  * @return array
  */
 private function getPropertyMapping(ReflectionProperty $property)
 {
     $metadata = [];
     foreach ($this->reader->getPropertyAnnotations($property) as $value) {
         if (!$value instanceof RiakAnnotation) {
             continue;
         }
         $class = get_class($value);
         $name = $property->getName();
         $key = lcfirst(substr($class, strrpos($class, '\\') + 1));
         $metadata[$key] = $name;
     }
     return $metadata;
 }
Exemplo n.º 9
0
 /**
  * Reads annotations for a selected property in the class
  *
  * @param ReflectionProperty $property
  * @param ClassMetadataInterface $metadata
  */
 private function readProperty(ReflectionProperty $property, ClassMetadataInterface $metadata)
 {
     // Skip if this property is not from this class
     if ($property->getDeclaringClass()->getName() != $metadata->getClassName()) {
         return;
     }
     //Iterate over all annotations
     foreach ($this->reader->getPropertyAnnotations($property) as $rule) {
         //Skip is its not a rule
         if (!$rule instanceof Rules\Rule) {
             continue;
         }
         //Add Rule
         $metadata->addPropertyRule($property->getName(), $rule);
     }
 }
Exemplo n.º 10
0
 /**
  * {@inheritDoc}
  */
 public function loadMetadataForClass($className, ResourceMetadata $resourceMetadata)
 {
     // get all class annotations
     $reflClass = new \ReflectionClass($className);
     $classAnnotations = $this->reader->getClassAnnotations($reflClass);
     $classAnnotations = $this->indexAnnotationsByType($classAnnotations);
     $resourceAnnotation = $this->getAnnotation($classAnnotations, 'BedRest\\Resource\\Mapping\\Annotation\\Resource');
     if ($resourceAnnotation !== false) {
         if (!empty($resourceAnnotation->name)) {
             $resourceMetadata->setName($resourceAnnotation->name);
         } else {
             $resourceName = Inflector::tableize(substr($className, strrpos($className, '\\') + 1));
             $resourceMetadata->setName($resourceName);
         }
     }
     $handlerAnnotation = $this->getAnnotation($classAnnotations, 'BedRest\\Resource\\Mapping\\Annotation\\Handler');
     if ($handlerAnnotation !== false) {
         if (!empty($handlerAnnotation->service)) {
             $resourceMetadata->setService($handlerAnnotation->service);
         }
     }
     // properties
     $subResources = array();
     foreach ($reflClass->getProperties() as $reflProp) {
         $propAnnotations = $this->reader->getPropertyAnnotations($reflProp);
         $propAnnotations = $this->indexAnnotationsByType($propAnnotations);
         $subResourceAnnotation = $this->getAnnotation($propAnnotations, 'BedRest\\Resource\\Mapping\\Annotation\\SubResource');
         if ($subResourceAnnotation !== false) {
             $subResources[$subResourceAnnotation->name] = array('fieldName' => $reflProp->name, 'service' => $subResourceAnnotation->service);
         }
     }
     $resourceMetadata->setSubResources($subResources);
 }
 /**
  * Get converter annotation
  *
  * @param \ReflectionProperty $property
  *
  * @return Money
  *
  * @throws MoneyAnnotationNotFoundException
  */
 private function getConverterAnnotation(\ReflectionProperty $property)
 {
     $annotations = $this->reader->getPropertyAnnotations($property);
     $moneyAnnotation = null;
     foreach ($annotations as $annotation) {
         if ($annotation instanceof Money) {
             if ($moneyAnnotation) {
                 throw new \RuntimeException(sprintf('Many Money annotation in property %s->%s', $property->getDeclaringClass()->getName(), $property->getName()));
             }
             $moneyAnnotation = $annotation;
         }
     }
     if ($moneyAnnotation) {
         return $moneyAnnotation;
     }
     throw new MoneyAnnotationNotFoundException(sprintf('Not found DateTime annotation in property %s->%s', $property->getDeclaringClass()->getName(), $property->getName()));
 }
Exemplo n.º 12
0
 /**
  * Get annotations for property
  *
  * @param \ReflectionProperty $property
  * @return array
  */
 public function getPropertyAnnotations(\ReflectionProperty $property)
 {
     $annotations = array();
     foreach ($this->delegate->getPropertyAnnotations($property) as $annot) {
         $annotations[get_class($annot)] = $annot;
     }
     return $annotations;
 }
Exemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     $reflClass = $metadata->getReflectionClass();
     $className = $reflClass->name;
     $success = false;
     foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
         if ($constraint instanceof GroupSequence) {
             $metadata->setGroupSequence($constraint->groups);
         } elseif ($constraint instanceof GroupSequenceProvider) {
             $metadata->setGroupSequenceProvider(true);
         } elseif ($constraint instanceof Constraint) {
             $metadata->addConstraint($constraint);
         }
         $success = true;
     }
     foreach ($reflClass->getProperties() as $property) {
         if ($property->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
                 if ($constraint instanceof Constraint) {
                     $metadata->addPropertyConstraint($property->name, $constraint);
                 }
                 $success = true;
             }
         }
     }
     foreach ($reflClass->getMethods() as $method) {
         if ($method->getDeclaringClass()->name == $className) {
             foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
                 if ($constraint instanceof Callback) {
                     $constraint->callback = $method->getName();
                     $constraint->methods = null;
                     $metadata->addConstraint($constraint);
                 } elseif ($constraint instanceof Constraint) {
                     if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
                         $metadata->addGetterConstraint(lcfirst($matches[2]), $constraint);
                     } else {
                         throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
                     }
                 }
                 $success = true;
             }
         }
     }
     return $success;
 }
Exemplo n.º 14
0
 private function parseSingleClassFile($className)
 {
     //annotation on class
     $reflectionClass = new \ReflectionClass($className);
     $classAnnotations = $this->annotationReader->getClassAnnotations($reflectionClass);
     if (!$classAnnotations) {
         return;
     }
     foreach ($classAnnotations as $classAnnotation) {
         //name not set => bean name className?
         if (!isset($classAnnotation->name) or $classAnnotation->name == '') {
             $beanClassName = lcfirst(join('', array_slice(explode('\\', $className), -1)));
             $classAnnotation->name = $beanClassName;
         }
         $bean = array('name' => $classAnnotation->name, 'class' => $className, 'scope' => $classAnnotation->scope, 'lazyInit' => (string) $classAnnotation->lazyInit);
         //annotation on method
         $methods = $reflectionClass->getMethods();
         foreach ($methods as $method) {
             if ($method->name == self::CONSTRUCT_METHOD) {
                 //annotaiton on construct
                 $bean = $this->analyzeConstruct($className, $method, $bean);
             } else {
                 $reflectionMethod = new \ReflectionMethod($className, $method->name);
                 $methodAnnotations = $this->annotationReader->getMethodAnnotations($reflectionMethod);
                 if ($methodAnnotations) {
                     foreach ($methodAnnotations as $pathToClassFile) {
                         if (isset($method->name)) {
                             $bean[get_class($pathToClassFile)] = $method->name;
                         }
                     }
                 }
             }
         }
         //annotation on properties
         $properties = $reflectionClass->getProperties();
         $allProperties = array();
         $temp = array();
         foreach ($properties as $property) {
             $reflectionProperty = new \ReflectionProperty($className, $property->name);
             $propertyAnnotations = $this->annotationReader->getPropertyAnnotations($reflectionProperty);
             if ($propertyAnnotations) {
                 foreach ($propertyAnnotations as $pathToClassFile) {
                     $allProperties[$property->name] = array(get_class($pathToClassFile) => $pathToClassFile->name);
                     if ($pathToClassFile->ref) {
                         $temp[] = array('name' => $property->name, 'ref' => $pathToClassFile->ref);
                     } else {
                         $temp[] = array('name' => $property->name, 'value' => $this->replaceVariable($pathToClassFile->value), 'type' => $pathToClassFile->type);
                     }
                 }
             }
             if ($temp) {
                 $bean['properties'] = $temp;
             }
         }
         $this->context[$classAnnotation->name] = $bean;
     }
 }
Exemplo n.º 15
0
 /**
  * Return the class metadata instance
  * 
  * @param string $entityName
  * 
  * @return ClassMetaDataCollection
  */
 public function getClassMetadata($entityName)
 {
     $r = new ReflectionClass($entityName);
     $instanceMetadataCollection = new ClassMetaDataCollection();
     $instanceMetadataCollection->name = $entityName;
     $classAnnotations = $this->reader->getClassAnnotations($r);
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof RepositoryAttribute) {
             $instanceMetadataCollection->setRepository($classAnnotation->getValue());
         }
         if ($classAnnotation instanceof ObjectClass) {
             $instanceMetadataCollection->setObjectClass($classAnnotation->getValue());
         }
         if ($classAnnotation instanceof SearchDn) {
             $instanceMetadataCollection->setSearchDn($classAnnotation->getValue());
         }
         if ($classAnnotation instanceof Dn) {
             $instanceMetadataCollection->setDn($classAnnotation->getValue());
         }
     }
     foreach ($r->getProperties() as $publicAttr) {
         $annotations = $this->reader->getPropertyAnnotations($publicAttr);
         foreach ($annotations as $annotation) {
             if ($annotation instanceof Attribute) {
                 $varname = $publicAttr->getName();
                 $attribute = $annotation->getName();
                 $instanceMetadataCollection->addMeta($varname, $attribute);
             }
             if ($annotation instanceof DnLinkArray) {
                 $varname = $publicAttr->getName();
                 $instanceMetadataCollection->addArrayOfLink($varname, $annotation->getValue());
             }
             if ($annotation instanceof Sequence) {
                 $varname = $publicAttr->getName();
                 $instanceMetadataCollection->addSequence($varname, $annotation->getValue());
             }
             if ($annotation instanceof DnPregMatch) {
                 $varname = $publicAttr->getName();
                 $instanceMetadataCollection->addRegex($varname, $annotation->getValue());
             }
             if ($annotation instanceof ParentDn) {
                 $varname = $publicAttr->getName();
                 $instanceMetadataCollection->addParentLink($varname, $annotation->getValue());
             }
             if ($annotation instanceof ArrayField) {
                 $instanceMetadataCollection->addArrayField($varname);
             }
             if ($annotation instanceof Must) {
                 $instanceMetadataCollection->addMust($varname);
             }
             if ($annotation instanceof Operational) {
                 $instanceMetadataCollection->addOperational($varname);
             }
         }
     }
     return $instanceMetadataCollection;
 }
 /**
  * @test
  */
 public function load_class_loads_metadata_correctly()
 {
     $reflClass = new \ReflectionClass('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\SimpleObject');
     $metadata = new ClassMetadata($reflClass);
     $this->reader->getClassAnnotations($reflClass)->willReturn([new ClassAnnot(), new NotHandledAnnotation()]);
     $this->reader->getMethodAnnotations($reflClass->getMethod('getAuthor'))->willReturn([new NotHandledAnnotation(), new MethodAnnotation1(), new MethodAnnotation2()]);
     $this->reader->getPropertyAnnotations($reflClass->getProperty('createdAt'))->willReturn([new NotHandledAnnotation()]);
     $this->reader->getPropertyAnnotations($reflClass->getProperty('author'))->willReturn([]);
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\NotHandledAnnotation'))->willReturn();
     $classAnnotationProcessor = $this->prophesize('Kcs\\Metadata\\Loader\\Processor\\ProcessorInterface');
     $classAnnotationProcessor->process(Argument::type('Kcs\\Metadata\\MetadataInterface'), Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\ClassAnnot'))->shouldBeCalledTimes(1);
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\ClassAnnot'))->willReturn($classAnnotationProcessor->reveal());
     $methodAnnotationProcessor = $this->prophesize('Kcs\\Metadata\\Loader\\Processor\\ProcessorInterface');
     $methodAnnotationProcessor->process(Argument::type('Kcs\\Metadata\\MetadataInterface'), Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation1'))->shouldBeCalledTimes(1);
     $methodAnnotationProcessor->process(Argument::type('Kcs\\Metadata\\MetadataInterface'), Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation2'))->shouldBeCalledTimes(1);
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation1'))->willReturn($methodAnnotationProcessor->reveal());
     $this->processorFactory->getProcessor(Argument::type('Kcs\\Metadata\\Tests\\Fixtures\\AnnotationProcessorLoader\\Annotation\\MethodAnnotation2'))->willReturn($methodAnnotationProcessor->reveal());
     $this->loader->loadClassMetadata($metadata);
 }
Exemplo n.º 17
0
 /**
  * {@inheritDoc}
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $metadata = new ClassMetadata($class->getName());
     $metadata->fileResources[] = $class->getFileName();
     $classAnnotations = $this->reader->getClassAnnotations($class);
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof Mergeable) {
             $metadata->accessor = $classAnnotation->accessor;
         } elseif ($classAnnotation instanceof ObjectIdentifier) {
             $metadata->objectIdentifier = $classAnnotation->fields;
         }
     }
     $propertiesMetadata = array();
     $propertiesAnnotations = array();
     foreach ($class->getProperties() as $property) {
         $propertiesMetadata[] = new PropertyMetadata($class->getName(), $property->getName());
         $propertiesAnnotations[] = $this->reader->getPropertyAnnotations($property);
     }
     foreach ($propertiesMetadata as $key => $propertyMetadata) {
         $canAdd = null;
         if (empty($propertiesAnnotations[$key])) {
             continue;
         }
         /** @var PropertyMetadata $propertyMetadata */
         foreach ($propertiesAnnotations[$key] as $propertyAnnotation) {
             $canAdd = true;
             if ($propertyAnnotation instanceof Mergeable) {
                 $propertyMetadata->setType($propertyAnnotation->type);
             } elseif ($propertyAnnotation instanceof CollectionMergeStrategy) {
                 $propertyMetadata->collectionMergeStrategy = $propertyAnnotation->strategies;
             } elseif ($propertyAnnotation instanceof IgnoreNullValue) {
                 $propertyMetadata->ignoreNullValue = $propertyAnnotation->activated;
             } else {
                 $canAdd = false;
             }
         }
         // Only add propertymetadata if it is defined within the mergeannotations.
         if ($canAdd) {
             $metadata->addPropertyMetadata($propertyMetadata);
         }
     }
     return $metadata;
 }
 /**
  * @param LifecycleEventArgs $args
  */
 public function prePersist(LifecycleEventArgs $args)
 {
     $entity = $args->getEntity();
     $em = $args->getEntityManager();
     $meta = $em->getClassMetadata(get_class($entity));
     $this->repo = $em->getRepository($meta->getName());
     $object = new \ReflectionObject($entity);
     foreach ($object->getProperties() as $property) {
         foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
             if ($annotation instanceof GeneratorAnnotation) {
                 $property->setAccessible(true);
                 if (!$annotation->override && $property->getValue($entity)) {
                     break;
                 }
                 $string = $this->generateString($property->name, $annotation, $object);
                 $property->setValue($entity, $string);
             }
         }
     }
 }
 /**
  * @return array|null
  */
 public function getPropertyAnnotations()
 {
     if (null === $this->propertyAnnotations) {
         $this->propertyAnnotations = array();
         /** @var \ReflectionProperty $reflectionProperty */
         foreach ($this->reflectionProperties as $reflectionProperty) {
             $this->propertyAnnotations[$reflectionProperty->getName()] = $this->reader->getPropertyAnnotations($reflectionProperty);
         }
     }
     return $this->propertyAnnotations;
 }
 /**
  * {@inheritDoc}
  */
 public function loadMetadata($class)
 {
     // Try get @Object annotation
     $objectAnnotation = null;
     $classAnnotations = Reflection::loadClassAnnotations($this->reader, $class);
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof ObjectAnnotation) {
             if ($objectAnnotation) {
                 throw new \RuntimeException(sprintf('Many @Normalize\\Object annotations in class "%s".', $class));
             }
             $objectAnnotation = $classAnnotation;
         }
     }
     // Try get @Property annotation from properties
     $properties = [];
     $classProperties = Reflection::getClassProperties($class, true);
     if ($objectAnnotation && $objectAnnotation->allProperties) {
         foreach ($classProperties as $classProperty) {
             /** @var PropertyAnnotation $propertyAnnotation */
             $propertyAnnotation = $this->reader->getPropertyAnnotation($classProperty, 'FivePercent\\Component\\ModelNormalizer\\Annotation\\Property');
             if ($propertyAnnotation) {
                 $properties[$classProperty->getName()] = new PropertyMetadata($propertyAnnotation->fieldName ?: $classProperty->getName(), $propertyAnnotation->groups, $propertyAnnotation->shouldNormalize, $propertyAnnotation->expressionValue, $propertyAnnotation->denormalizerClass);
             } else {
                 $properties[$classProperty->getName()] = new PropertyMetadata($classProperty->getName(), [], false, null);
             }
         }
     } else {
         foreach ($classProperties as $classProperty) {
             $propertyAnnotations = $this->reader->getPropertyAnnotations($classProperty);
             foreach ($propertyAnnotations as $propertyAnnotation) {
                 if ($propertyAnnotation instanceof PropertyAnnotation) {
                     $properties[$classProperty->getName()] = new PropertyMetadata($propertyAnnotation->fieldName ?: $classProperty->getName(), $propertyAnnotation->groups, $propertyAnnotation->shouldNormalize, $propertyAnnotation->expressionValue, $propertyAnnotation->denormalizerClass);
                 }
             }
         }
     }
     if (!count($properties) && !$objectAnnotation) {
         throw new NormalizeAnnotationNotFoundException(sprintf('Not found normalize annotations in class "%s".', $class));
     }
     return new ObjectMetadata($properties);
 }
Exemplo n.º 21
0
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $metadata = new ClassMetadata($class->getName());
     $propertyMetadata = [];
     foreach ($class->getProperties() as $reflProperty) {
         $annotations = $this->reader->getPropertyAnnotations($reflProperty);
         foreach ($annotations as $annotation) {
             if ($annotation instanceof Annotations\Field) {
                 $propertyMetadata = new PropertyMetadata($class->getName(), $reflProperty->getName(), $annotation->type, $annotation->role, $annotation->group, ['shared' => $annotation->shared, 'form' => $annotation->form, 'view' => $annotation->view, 'storage' => $annotation->storage]);
                 $propertyMetadatas[] = $propertyMetadata;
             }
         }
     }
     if (empty($propertyMetadatas)) {
         return;
     }
     foreach ($propertyMetadatas as $propertyMetadata) {
         $metadata->addPropertyMetadata($propertyMetadata);
     }
     return $metadata;
 }
Exemplo n.º 22
0
 /**
  * {@inheritdoc}
  */
 public function getPropertyKey(\ReflectionProperty $property)
 {
     foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
         if ($annotation instanceof Inject) {
             $value = $annotation->getSingleValue();
             if ($value !== null) {
                 return $value;
             }
         }
     }
     return null;
 }
 function it_can_configure_a_generator(Registry $registry, Reader $reader, LifecycleEventArgs $args, ConfigurableGeneratorInterface $generator)
 {
     $annotation = new GeneratorAnnotation([]);
     $reader->getPropertyAnnotations(Argument::any())->willReturn([$annotation]);
     $registry->get(Argument::any())->willReturn($generator);
     $generator->generate()->shouldBeCalled();
     $generator->setLength(Argument::any())->shouldBeCalled();
     $resolver = new OptionsResolver();
     $generator->getDefaultOptions($resolver)->shouldBeCalled();
     $generator->setOptions(Argument::any())->shouldBeCalled();
     $this->prePersist($args);
 }
Exemplo n.º 24
0
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $classMetadata = new ClassMetadata($class->getName());
     foreach ($this->reader->getClassAnnotations($class) as $annotation) {
         if ($annotation instanceof Type) {
             $classMetadata->setType($annotation->value, $annotation->options);
         } else {
             if ($annotation instanceof EventSubscribers) {
                 $classMetadata->setEventSubscribers($annotation->getValues());
             } else {
                 if ($annotation instanceof ModelTransformers) {
                     $classMetadata->setModelTransformers($annotation->getValues());
                 } else {
                     if ($annotation instanceof ViewTransformers) {
                         $classMetadata->setViewTransformers($annotation->getValues());
                     }
                 }
             }
         }
     }
     foreach ($class->getMethods() as $reflectionMethod) {
         foreach ($this->reader->getMethodAnnotations($reflectionMethod) as $annotation) {
             if ($annotation instanceof EventListener) {
                 $methodMetadata = new MethodMetadata($class->getName(), $reflectionMethod->getName());
                 $methodMetadata->addEventListener($annotation->event, $annotation->priority);
                 $classMetadata->addMethodMetadata($methodMetadata);
             }
         }
     }
     foreach ($class->getProperties() as $reflectionProperty) {
         foreach ($this->reader->getPropertyAnnotations($reflectionProperty) as $annotation) {
             if ($annotation instanceof Field) {
                 $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
                 $propertyMetadata->setField($annotation->type, $annotation->options);
                 $classMetadata->addPropertyMetadata($propertyMetadata);
             }
         }
     }
     return $classMetadata;
 }
Exemplo n.º 25
0
 /**
  * {@inheritDoc}
  */
 public function getPropertyAnnotations(\ReflectionProperty $property)
 {
     $class = $property->getDeclaringClass();
     $cacheKey = $class->getName() . '$' . $property->getName();
     if (isset($this->loadedAnnotations[$cacheKey])) {
         return $this->loadedAnnotations[$cacheKey];
     }
     if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
         $annots = $this->delegate->getPropertyAnnotations($property);
         $this->saveToCache($cacheKey, $annots);
     }
     return $this->loadedAnnotations[$cacheKey] = $annots;
 }
Exemplo n.º 26
0
 /**
  * @param $objectToValidate
  * @return bool
  * @throws ObjectFailedValidationException
  */
 public function validate($objectToValidate)
 {
     $object = new ReflectionObject($objectToValidate);
     $errors = [];
     foreach ($object->getProperties() as $property) {
         $annotations = $this->annotationReader->getPropertyAnnotations($property);
         $property->setAccessible(true);
         foreach ($annotations as $annotation) {
             if ($annotation instanceof AssertionInterface) {
                 try {
                     $annotation->assert($objectToValidate, $property->getName(), $property->getValue($objectToValidate));
                 } catch (ValidationException $e) {
                     $errors[$property->getName()][] = $e->getMessage();
                 }
             }
         }
     }
     if (count($errors) > 0) {
         throw new ObjectFailedValidationException($objectToValidate, $errors);
     }
     return true;
 }
Exemplo n.º 27
0
 /**
  * Parse JSON API resource metadata
  *
  * @param ApiResource $resource
  * @param \ReflectionClass $class
  * @return ResourceMetadata
  */
 private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class)
 {
     $metadata = new ResourceMetadata($class->name);
     $metadata->setName($resource->name);
     $properties = $class->getProperties();
     foreach ($properties as $property) {
         if ($property->getDeclaringClass()->name !== $class->name) {
             continue;
         }
         foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
             if ($annotation instanceof Attribute) {
                 $metadata->addAttribute($this->loadPropertyMetadata($annotation, $property));
             } elseif ($annotation instanceof Relationship) {
                 $metadata->addRelationship($this->loadPropertyMetadata($annotation, $property));
             } elseif ($annotation instanceof Id) {
                 $metadata->setIdMetadata($this->loadPropertyMetadata($annotation, $property));
             }
         }
     }
     $this->loadDiscriminatorMetadata($resource, $metadata);
     return $metadata;
 }
Exemplo n.º 28
0
 /**
  * Return list of annotations for reflection point
  *
  * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $refPoint Reflection instance
  *
  * @return array list of annotations
  * @throws \InvalidArgumentException if $refPoint is unsupported
  */
 protected function getAnnotations($refPoint)
 {
     switch (true) {
         case $refPoint instanceof \ReflectionClass:
             return $this->annotationReader->getClassAnnotations($refPoint);
         case $refPoint instanceof \ReflectionMethod:
             return $this->annotationReader->getMethodAnnotations($refPoint);
         case $refPoint instanceof \ReflectionProperty:
             return $this->annotationReader->getPropertyAnnotations($refPoint);
         default:
             throw new \InvalidArgumentException("Unsupported reflection point " . get_class($refPoint));
     }
 }
 /**
  * @param string $className
  *
  * @return NodeEntityMetadata|RelationshipEntityMetadata
  */
 public function create($className)
 {
     $reflectionClass = new \ReflectionClass($className);
     $entityIdMetadata = null;
     $propertiesMetadata = [];
     $relationshipsMetadata = [];
     if (null !== ($annotation = $this->reader->getClassAnnotation($reflectionClass, Node::class))) {
         $annotationMetadata = $this->nodeAnnotationMetadataFactory->create($className);
         foreach ($reflectionClass->getProperties() as $reflectionProperty) {
             $propertyAnnotationMetadata = $this->propertyAnnotationMetadataFactory->create($className, $reflectionProperty->getName());
             if (null !== $propertyAnnotationMetadata) {
                 $propertiesMetadata[] = new EntityPropertyMetadata($reflectionProperty->getName(), $reflectionProperty, $propertyAnnotationMetadata);
             } else {
                 $idA = $this->IdAnnotationMetadataFactory->create($className, $reflectionProperty);
                 if (null !== $idA) {
                     $entityIdMetadata = new EntityIdMetadata($reflectionProperty->getName(), $reflectionProperty, $idA);
                 }
             }
             foreach ($this->reader->getPropertyAnnotations($reflectionProperty) as $annot) {
                 if ($annot instanceof Label) {
                     $propertiesMetadata[] = new LabeledPropertyMetadata($reflectionProperty->getName(), $reflectionProperty, $annot);
                 }
                 if ($annot instanceof Relationship) {
                     $isLazy = null !== $this->reader->getPropertyAnnotation($reflectionProperty, Lazy::class);
                     $orderBy = $this->reader->getPropertyAnnotation($reflectionProperty, OrderBy::class);
                     $relationshipsMetadata[] = new RelationshipMetadata($className, $reflectionProperty, $annot, $isLazy, $orderBy);
                 }
             }
         }
         return new NodeEntityMetadata($className, $reflectionClass, $annotationMetadata, $entityIdMetadata, $propertiesMetadata, $relationshipsMetadata);
     } elseif (null !== ($annotation = $this->reader->getClassAnnotation($reflectionClass, RelationshipEntity::class))) {
         return $this->relationshipEntityMetadataFactory->create($className);
     }
     if (null !== get_parent_class($className)) {
         return $this->create(get_parent_class($className));
     }
     throw new MappingException(sprintf('The class "%s" is not a valid OGM entity', $className));
 }
Exemplo n.º 30
0
 /**
  * @param Reader $reader
  * @param ReflectionProperty $property
  */
 function __construct(Reader $reader, ReflectionProperty $property)
 {
     $this->reader = $reader;
     $this->property = $property;
     if ($this->isProperty() || $this->isRelation()) {
         $this->name = $property->getName();
     } else {
         // as far as we know only relation list are collections with names we can 'normalize'
         $this->name = Reflection::singularizeProperty($property->getName());
     }
     if ($this->isProperty()) {
         foreach ($this->reader->getPropertyAnnotations($this->property) as $annotation) {
             if ($annotation instanceof Annotation\Index) {
                 $copy = clone $annotation;
                 $copy->name = $copy->name ?: $this->property->class;
                 $copy->field = $copy->field ?: $this->property->name;
                 $copy->type = $copy->type ?: 'node';
                 $this->indexes[] = $copy;
             }
         }
     }
     $property->setAccessible(true);
 }