/**
  * @param array $request
  * @param \ReflectionClass $class
  * @param callable $nameMangling
  *
  * @return ConversionResult
  */
 public function convert(array $request, \ReflectionClass $class, callable $nameMangling)
 {
     $ctx = new Context($this, $this->coercer, $nameMangling);
     $object = $this->instance($class);
     $setter = $this->setter($object);
     $errors = [];
     foreach ($class->getProperties() as $prop) {
         $name = $nameMangling($prop->getName());
         if (!isset($request[$name]) && !$this->reader->getPropertyAnnotation($prop, Optional::class)) {
             $errors[] = new MissingFieldError($name);
             continue;
         } elseif (!\array_key_exists($name, $request)) {
             continue;
         }
         $value = $request[$name];
         $typeAnnotation = $this->reader->getPropertyAnnotation($prop, Type::class);
         if ($value !== null && $typeAnnotation instanceof Type) {
             try {
                 $result = $this->coercer->coerce($value, $typeAnnotation->type, $ctx);
             } catch (CoercionException $e) {
                 throw ConverterException::in($class->getName(), $prop->getName(), $e);
             }
             $value = $result->getValue();
             $errors = \array_merge($errors, $result->errorsInField($name));
             $setter->set($prop->getName(), $value);
         } elseif ($value === null) {
             $setter->set($prop->getName(), null);
         }
     }
     return ConversionResult::errors($errors, $object);
 }
Esempio n. 2
0
 /**
  * @param \ReflectionClass $class
  *
  * @return \Metadata\ClassMetadata
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $metadata = new ClassMetadata($class->getName());
     // Resource annotation
     $annotation = $this->reader->getClassAnnotation($class, 'Conjecto\\Nemrod\\ResourceManager\\Annotation\\Resource');
     if (null !== $annotation) {
         $types = $annotation->types;
         $pattern = $annotation->uriPattern;
         if (!is_array($types)) {
             $types = array($types);
         }
         $metadata->types = $types;
         $metadata->uriPattern = $pattern;
     }
     foreach ($class->getProperties() as $reflectionProperty) {
         $propMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
         // Property annotation
         $annotation = $this->reader->getPropertyAnnotation($reflectionProperty, 'Conjecto\\Nemrod\\ResourceManager\\Annotation\\Property');
         if (null !== $annotation) {
             $propMetadata->value = $annotation->value;
             $propMetadata->cascade = $annotation->cascade;
         }
         $metadata->addPropertyMetadata($propMetadata);
     }
     return $metadata;
 }
 /**
  * @param \ReflectionClass $class
  *
  * @return \Metadata\ClassMetadata
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $metadata = $this->driver->loadMetadataForClass($class);
     foreach ($metadata->propertyMetadata as $key => $propertyMetadata) {
         $type = $propertyMetadata->type['name'];
         if (!$propertyMetadata->reflection) {
             continue;
         }
         /** @var PropertyMetadata $propertyMetadata */
         /** @var HandledType $annot */
         $annot = $this->reader->getPropertyAnnotation($propertyMetadata->reflection, HandledType::class);
         if (!$annot) {
             continue;
         }
         $isCollection = false;
         $collectionType = null;
         if (in_array($type, ['array', 'ArrayCollection'], true)) {
             $isCollection = true;
             $collectionType = $type;
             $type = $propertyMetadata->type['params'][0]['name'];
         }
         $handler = $annot->handler ?: 'Relation';
         $newType = sprintf('%s<%s>', $handler, $type);
         if ($isCollection) {
             $newType = sprintf('%s<%s<%s>>', $collectionType, $handler, $type);
         }
         $propertyMetadata->setType($newType);
     }
     return $metadata;
 }
Esempio n. 4
0
 /**
  * @param $object
  * @param $classRefl
  * @param $segmentData
  * @throws MandatorySegmentPieceMissing
  */
 protected function fillFromArray($object, $classRefl, $segmentData)
 {
     foreach ($classRefl->getProperties() as $propRefl) {
         $isSegmentPiece = $this->annotationReader->getPropertyAnnotation($propRefl, SegmentPiece::class);
         if ($isSegmentPiece) {
             $piece = isset($segmentData[$isSegmentPiece->position]) ? $segmentData[$isSegmentPiece->position] : null;
             $propRefl->setAccessible(true);
             if ($isSegmentPiece->parts) {
                 $value = array();
                 $i = 0;
                 foreach ($isSegmentPiece->parts as $k => $part) {
                     if (!is_numeric($k) && is_array($part)) {
                         $partName = $k;
                         if (!empty($piece) && in_array("@mandatory", $part) && $this->isEmpty($piece[$i])) {
                             throw new MandatorySegmentPieceMissing(sprintf("Segment %s part %s missing value at offset %d", $segmentData[0], $partName, $i));
                         }
                     } else {
                         $partName = $part;
                     }
                     $value[$partName] = isset($piece[$i]) ? $piece[$i] : null;
                     ++$i;
                 }
                 $propRefl->setValue($object, $value);
             } else {
                 $propRefl->setValue($object, $piece);
             }
         }
     }
 }
Esempio n. 5
0
 public function getForm(ResourceInterface $resource)
 {
     $resourceClassName = \Doctrine\Common\Util\ClassUtils::getClass($resource);
     $resourceParts = explode("\\", $resourceClassName);
     $resourceParts[count($resourceParts) - 2] = 'Form';
     $resourceParts[count($resourceParts) - 1] .= 'Type';
     $formType = implode("\\", $resourceParts);
     if (class_exists($formType)) {
         return $this->formFactory->create(new $formType(), $resource);
     }
     $options = array('data_class' => $resourceClassName);
     $builder = $this->formFactory->createBuilder('form', $resource, $options);
     $reflectionClass = new \ReflectionClass($resourceClassName);
     $annotationClass = 'uebb\\HateoasBundle\\Annotation\\FormField';
     foreach ($reflectionClass->getProperties() as $propertyReflection) {
         /**
          * @var \uebb\HateoasBundle\Annotation\FormField $annotation
          */
         $annotation = $this->annotationReader->getPropertyAnnotation($propertyReflection, $annotationClass);
         if ($annotation) {
             $builder->add($propertyReflection->getName(), $annotation->type, is_array($annotation->options) ? $annotation->options : array());
         }
     }
     $form = $builder->getForm();
     return $form;
 }
Esempio n. 6
0
 /**
  * @param ReflectionClass $class
  *
  * @return array
  */
 public function getProperties(ReflectionClass $class)
 {
     $properties = $class->getProperties();
     $propertyAnnotations = [];
     foreach ($properties as $property) {
         $annotationName = $this->reader->getPropertyAnnotation($property, Field::class);
         if ($annotationName) {
             $annotationName = $annotationName->getName();
             $propertyAnnotations[$property->getName()] = $annotationName;
         }
     }
     return array_flip($propertyAnnotations);
 }
Esempio n. 7
0
 /**
  * @param EmbeddedMetadataInterface $embeddedMetadata
  *
  * @return EmbeddedMetadataInterface
  */
 private function addNestedEmbeddedClasses(EmbeddedMetadataInterface $embeddedMetadata)
 {
     $reflClass = new \ReflectionClass($embeddedMetadata->getClassAttribute());
     foreach ($reflClass->getProperties() as $reflProperty) {
         if ($annotation = $this->reader->getPropertyAnnotation($reflProperty, Index::class)) {
             $embeddedMetadata->addEmbeddableClass(new IndexMetadata($reflProperty->class, $reflProperty->name, $annotation->name, $annotation->type));
         }
         if ($annotation = $this->reader->getPropertyAnnotation($reflProperty, Embedded::class)) {
             $nested = $this->addNestedEmbeddedClasses(new EmbeddedMetadata($reflProperty->class, $reflProperty->name, $annotation->class));
             $embeddedMetadata->addEmbeddableClass($nested);
         }
     }
     return $embeddedMetadata;
 }
 /**
  * Attempts to read the uploadable field annotation of the
  * specified property.
  *
  * @param \ReflectionClass $class The class.
  * @param string $field The field
  * @return null|\Vich\UploaderBundle\Annotation\UploadableField The uploadable field.
  */
 public function readUploadableField(\ReflectionClass $class, $field)
 {
     try {
         $prop = $class->getProperty($field);
         $field = $this->reader->getPropertyAnnotation($prop, 'Vich\\UploaderBundle\\Mapping\\Annotation\\UploadableField');
         if (null === $field) {
             return null;
         }
         $field->setPropertyName($prop->getName());
         return $field;
     } catch (\ReflectionException $e) {
         return null;
     }
 }
Esempio n. 9
0
 /**
  * Get the designated class name from the given property
  * 
  * @param mixed $property Can be either a \ReflectionProperty or a primitive string type.
  * @return string If $property was a primitive string type (ie <array>) then that string is returned.
  *					If $property is a \ReflectionProperty, the type of that property is returned, or null if
  *					one could not be determined.
  */
 public function getTypeFromProperty($property)
 {
     if (is_string($property)) {
         throw new \InvalidArgumentException('Parameter $property cannot be a string unless the string is a valid primitive type');
     }
     $typeAnnotation = $this->annotationReader->getPropertyAnnotation($property, 'Rezonant\\MapperBundle\\Annotations\\Type');
     if ($typeAnnotation) {
         return $typeAnnotation->value;
     }
     $typeAnnotation = $this->annotationReader->getPropertyAnnotation($property, 'JMS\\Serializer\\Annotation\\Type');
     if ($typeAnnotation) {
         return $typeAnnotation->name;
     }
     return null;
 }
Esempio n. 10
0
 /**
  * Form annotation converter.
  *
  * @param object $form
  *
  * @return array
  */
 public function convert($form)
 {
     $data = array();
     $reflection = new \ReflectionObject($form);
     foreach ($reflection->getProperties() as $property) {
         $annotation = $this->_reader->getPropertyAnnotation($property, $this->_annotation);
         if ($annotation !== null) {
             if (method_exists($annotation, 'setName')) {
                 $annotation->setName($property->getName());
             }
             $data[] = $annotation;
         }
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function loadMetadataForClass(\ReflectionClass $class)
 {
     $classMetadata = new MergeableClassMetadata($class->getName());
     foreach ($class->getProperties() as $reflectionProperty) {
         $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
         /** @var \Integrated\Bundle\SlugBundle\Mapping\Annotations\Slug $annotation */
         $annotation = $this->reader->getPropertyAnnotation($reflectionProperty, 'Integrated\\Bundle\\SlugBundle\\Mapping\\Annotations\\Slug');
         if (null !== $annotation) {
             $propertyMetadata->slugFields = $annotation->fields;
             $propertyMetadata->slugSeparator = $annotation->separator;
         }
         $classMetadata->addPropertyMetadata($propertyMetadata);
     }
     return $classMetadata;
 }
Esempio n. 12
0
 /**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadataInterface $classMetadata, array $normalizationGroups = null, array $denormalizationGroups = null, array $validationGroups = null)
 {
     $reflectionClass = $classMetadata->getReflectionClass();
     if ($iri = $this->reader->getClassAnnotation($reflectionClass, self::IRI_ANNOTATION_NAME)) {
         $classMetadata = $classMetadata->withIri($iri->value);
     }
     foreach ($classMetadata->getAttributesMetadata() as $attributeName => $attributeMetadata) {
         if ($reflectionProperty = $this->getReflectionProperty($reflectionClass, $attributeName)) {
             if ($iri = $this->reader->getPropertyAnnotation($reflectionProperty, self::IRI_ANNOTATION_NAME)) {
                 $classMetadata = $classMetadata->withAttributeMetadata($attributeName, $attributeMetadata->withIri($iri->value));
             }
         }
     }
     return $classMetadata;
 }
 /**
  * Creates form configuration for $model for given $fields
  *
  * @param object $model
  * @param array $fields
  * @return array
  */
 private function getFieldsConfiguration($model, $fields = array())
 {
     $configuration = $properties = array();
     $ro = new \ReflectionObject($model);
     if (empty($fields)) {
         $properties = $ro->getProperties();
     } else {
         foreach (array_keys($fields) as $field) {
             $properties[] = $ro->getProperty($field);
         }
     }
     foreach ($properties as $property) {
         $propertyIsListed = array_key_exists($property->getName(), $fields);
         if (!empty($fields) && !$propertyIsListed) {
             continue;
         }
         $fieldConfiguration = $this->annotationReader->getPropertyAnnotation($property, 'Codete\\FormGeneratorBundle\\Annotations\\Display');
         if ($fieldConfiguration === null && !$propertyIsListed) {
             continue;
         }
         $configuration[$property->getName()] = (array) $fieldConfiguration;
         if (isset($fields[$property->getName()])) {
             $configuration[$property->getName()] = array_replace_recursive($configuration[$property->getName()], $fields[$property->getName()]);
         }
         // this variable comes from Doctrine\Common\Annotations\Annotation
         unset($configuration[$property->getName()]['value']);
     }
     return $configuration;
 }
 /**
  * Get a list linking an item name with the property it refers to and what kind of collection it is.
  * Ex: [
  *   "byItemName" => "user" => ["property" => "users", "behavior" => "list", "methods" => ["add", "remove"]],
  *   "byProperty" => "users" => ["itemName" => "user", "behavior" => "list", "methods" => ["add", "remove"]]
  * ]
  *
  * @param array  $properties The properties of the object to read.
  * @param \Doctrine\Common\Annotations\Reader $annotationReader The annotation reader to use.
  *
  * @return array The described list.
  */
 public static function getCollectionsItemNames($properties, $annotationReader)
 {
     $objectCollectionsItemNames = array("byProperty" => array(), "byItemName" => array());
     foreach ($properties as $propertyName => $property) {
         $annotation = null;
         $behavior = null;
         foreach (self::$collectionAnnotationClasses as $annotationBehavior => $annotationClass) {
             $annotation = $annotationReader->getPropertyAnnotation($property, $annotationClass);
             if ($annotation !== null) {
                 $behavior = $annotationBehavior;
                 break;
             }
         }
         if ($annotation !== null) {
             // get the item name, or deduce it (singularize the property name)
             $itemName = $annotation->getItemName();
             if ($itemName === null) {
                 $itemName = Inflector::singularize($propertyName);
             }
             $objectCollectionsItemNames["byItemName"][$itemName] = array("property" => $propertyName, "behavior" => $behavior, "methods" => $annotation->getMethods());
             $objectCollectionsItemNames["byProperty"][$propertyName] = array("itemName" => $itemName, "behavior" => $behavior, "methods" => $annotation->getMethods());
         }
     }
     return $objectCollectionsItemNames;
 }
Esempio n. 15
0
 /**
  * reads the entity and returns a set of annotations
  *
  * @param string $entity
  * @param string $type
  *
  * @return Annotation[]
  */
 private function getPropertiesByType($entity, $type)
 {
     $properties = $this->readClassProperties($entity);
     $fields = array();
     foreach ($properties as $property) {
         $annotation = $this->reader->getPropertyAnnotation($property, $type);
         if (null === $annotation) {
             continue;
         }
         $property->setAccessible(true);
         $annotation->value = $property->getValue($entity);
         $annotation->name = $property->getName();
         $fields[] = $annotation;
     }
     return $fields;
 }
 /**
  * @param $annotationName
  * @param CommandMessageInterface $command
  * @param \ReflectionClass $reflectionClass
  * @return mixed|null
  */
 private function getAnnotatedTargetValue($annotationName, CommandMessageInterface $command, \ReflectionClass $reflectionClass)
 {
     foreach (ReflectionUtils::getProperties($reflectionClass) as $property) {
         if (null !== ($annotation = $this->reader->getPropertyAnnotation($property, $annotationName))) {
             $property->setAccessible(true);
             return $property->getValue($command->getPayload());
         }
     }
     foreach (ReflectionUtils::getMethods($reflectionClass) as $method) {
         if (null !== ($annotation = $this->reader->getMethodAnnotation($method, $annotationName))) {
             $method->setAccessible(true);
             return $method->invoke($command->getPayload());
         }
     }
     return null;
 }
Esempio n. 17
0
 public static function fromAnnotation($model, Reader $reader)
 {
     $settings = ['model' => $model];
     $classRefl = new ReflectionClass($model);
     $propertiesRefl = $classRefl->getProperties();
     class_exists(Table::class);
     class_exists(Column::class);
     /* @var \Wandu\Database\Annotations\Table $table */
     if ($table = $reader->getClassAnnotation($classRefl, Table::class)) {
         $settings['identifier'] = $table->identifier;
         $settings['increments'] = $table->increments;
     }
     $columns = [];
     $casts = [];
     foreach ($propertiesRefl as $propertyRefl) {
         /* @var \Wandu\Database\Annotations\Column $column */
         if ($column = $reader->getPropertyAnnotation($propertyRefl, Column::class)) {
             $columns[$column->name] = $propertyRefl->name;
             $casts[$column->name] = $column->cast;
         }
     }
     if (count($columns)) {
         $settings['columns'] = $columns;
     }
     if (count($casts)) {
         $settings['casts'] = $casts;
     }
     return new RepositorySettings($table->name, $settings);
 }
Esempio n. 18
0
 protected function isPropertyExcluded(\ReflectionProperty $property, ClassMetadata $classMetadata)
 {
     if ($classMetadata->exclusionPolicy === Annotation\ExclusionPolicy::ALL) {
         return null === $this->reader->getPropertyAnnotation($property, Annotation\Expose::class);
     }
     return null !== $this->reader->getPropertyAnnotation($property, Annotation\Exclude::class);
 }
 /**
  * Process (encrypt/decrypt) entities fields
  * @param Obj $entity Some doctrine entity
  * @param Boolean $isEncryptOperation If true - encrypt, false - decrypt entity 
  */
 private function processFields($entity, $isEncryptOperation = true)
 {
     $encryptorMethod = $isEncryptOperation ? 'encrypt' : 'decrypt';
     $reflectionClass = new ReflectionClass($entity);
     $properties = $reflectionClass->getProperties();
     $withAnnotation = false;
     foreach ($properties as $refProperty) {
         if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) {
             $withAnnotation = true;
             // we have annotation and if it decrypt operation, we must avoid duble decryption
             $propName = $refProperty->getName();
             if ($refProperty->isPublic()) {
                 $entity->{$propName} = $this->encryptor->{$encryptorMethod}($refProperty->getValue());
             } else {
                 $methodName = self::capitalize($propName);
                 if ($reflectionClass->hasMethod($getter = 'get' . $methodName) && $reflectionClass->hasMethod($setter = 'set' . $methodName)) {
                     $currentPropValue = $this->encryptor->{$encryptorMethod}($entity->{$getter}());
                     $entity->{$setter}($currentPropValue);
                 } else {
                     throw new \RuntimeException(sprintf("Property %s isn't public and doesn't has getter/setter"));
                 }
             }
         }
     }
     return $withAnnotation;
 }
 /**
  * {@inheritdoc}
  */
 public function guessType($class, $property)
 {
     if (!($ret = $this->getMetadata($class))) {
         return null;
     }
     /** @var ClassMetadataInfo $metadata */
     list($metadata, ) = $ret;
     if ($metadata->isAssociationWithSingleJoinColumn($property)) {
         $reflectionProperty = new \ReflectionProperty($class, $property);
         /** @var \Rafrsr\ResourceBundle\Annotations\ResourceAnnotationInterface $config */
         $interface = 'Rafrsr\\ResourceBundle\\Annotations\\ResourceAnnotationInterface';
         if ($config = $this->reader->getPropertyAnnotation($reflectionProperty, $interface)) {
             return new TypeGuess($config->getFormType(), [], Guess::VERY_HIGH_CONFIDENCE);
         }
     }
     return null;
 }
Esempio n. 21
0
 protected function doRead($entity, $property)
 {
     $reflectionClass = new \ReflectionClass($entity);
     $config = ['add' => NULL, 'remove' => NULL, 'set' => NULL, 'get' => NULL];
     if (!$reflectionClass->hasProperty($property)) {
         return $config;
     }
     $class = '\\Librette\\Doctrine\\Annotations\\ManipulateMethods';
     $reflectionProperty = $reflectionClass->getProperty($property);
     $annotation = $this->annotationReader->getPropertyAnnotation($reflectionProperty, $class);
     if ($annotation && $annotation instanceof ManipulateMethods) {
         $config['add'] = $annotation->add;
         $config['remove'] = $annotation->remove;
         $config['set'] = $annotation->set;
         $config['get'] = $annotation->get;
     }
     return $config;
 }
Esempio n. 22
0
 /**
  * Intercepts access to autowired properties and injects specified dependency
  *
  * @Around("@access(Warlock\Annotation\Autowired)")
  *
  * @param FieldAccess $joinpoint Autowiring joinpoint
  *
  * @return mixed
  */
 public function beforeAccessingAutowiredProperty(FieldAccess $joinpoint)
 {
     $obj = $joinpoint->getThis();
     $field = $joinpoint->getField();
     if ($joinpoint->getAccessType() == FieldAccess::READ) {
         /** @var Autowired $autowired */
         $autowired = $this->reader->getPropertyAnnotation($field, self::ANNOTATION_NAME);
         $strategy = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
         if (!$autowired->required) {
             $strategy = ContainerInterface::NULL_ON_INVALID_REFERENCE;
         }
         $value = $this->container->get($autowired, $strategy);
     } else {
         $value = $joinpoint->proceed();
     }
     $field->setValue($obj, $value);
     return $value;
 }
Esempio n. 23
0
 private function parseProperties(\ReflectionClass $class, ClassMetadata $classMetadata)
 {
     foreach ($class->getProperties() as $reflProperty) {
         $propMeta = new PropertyMetadata($class->getName(), $reflProperty->getName());
         // is property exposed?
         /** @var Property\Expose $annotation */
         $annotation = $this->reader->getPropertyAnnotation($reflProperty, Property\Expose::class);
         if ($annotation) {
             $propMeta->setExposed(true);
             if ($annotation->name) {
                 $propMeta->setExposedName($annotation->name);
             }
             if ($annotation->accessRoles) {
                 $propMeta->setAccessRoles($annotation->accessRoles);
             }
             // set property renderer
             /** @var Property\Renderer $annotation */
             $annotation = $this->reader->getPropertyAnnotation($reflProperty, Property\Renderer::class);
             if ($annotation) {
                 if ($annotation->renderService) {
                     $propMeta->setRenderService($annotation->renderService);
                     if ($annotation->renderMethod) {
                         $propMeta->setRenderMethod($annotation->renderMethod);
                     }
                 }
             }
             // property getter?
             /** @var Property\GetterMethod $annotation */
             $annotation = $this->reader->getPropertyAnnotation($reflProperty, Property\GetterMethod::class);
             if ($annotation) {
                 if ($annotation->method) {
                     $propMeta->setGetterMethod($annotation->method);
                 } else {
                     // standard getter
                     $propMeta->setGetterMethod('get' . ucfirst($propMeta->getName()));
                 }
             } elseif (!$reflProperty->isPublic()) {
                 // assuming standard getter
                 $propMeta->setGetterMethod('get' . ucfirst($propMeta->getName()));
             }
         }
         $classMetadata->addPropertyMetadata($propMeta);
     }
 }
 /**
  * Get the list of properties that have to be initialized automatically
  * during the object construction, plus their value.
  *
  * @param array  $properties The properties of the object to read.
  * @param \Doctrine\Common\Annotations\Reader $annotationReader The annotation reader to use.
  *
  * @return array The list of properties and values,
  *               in the form ["property" => "value"].
  */
 public static function getPropertiesToInitialize($properties, $annotationReader)
 {
     $propertiesValues = array();
     foreach ($properties as $propertyName => $property) {
         $initializeAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeAnnotationClass);
         $initializeObjectAnnotation = $annotationReader->getPropertyAnnotation($property, self::$initializeObjectAnnotationClass);
         if ($initializeAnnotation !== null && $initializeObjectAnnotation !== null) {
             throw new \LogicException("Two initial values are given for property {$propertyName}.");
         }
         if ($initializeAnnotation !== null) {
             $propertiesValues[$propertyName] = array('type' => 'initialize', 'value' => $initializeAnnotation->getValue());
         } else {
             if ($initializeObjectAnnotation !== null) {
                 $propertiesValues[$propertyName] = array('type' => 'initializeObject', 'value' => $initializeObjectAnnotation->getClassName());
             }
         }
     }
     return $propertiesValues;
 }
Esempio n. 25
0
 /**
  * Get value
  *
  * @param mixed $obj
  * @param string $className
  * @return string
  */
 private function findValue($obj, $className)
 {
     $reflClass = new \ReflectionClass($obj);
     // Make sure we are not using a Proxy class
     if ($obj instanceof Proxy) {
         $reflClass = $reflClass->getParentClass();
     }
     // Find on property
     foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
         if ($this->reader->getPropertyAnnotation($property, $className)) {
             return $obj->{$property};
         }
     }
     // Find on method
     foreach ($reflClass->getMethods() as $method) {
         if ($this->reader->getMethodAnnotation($method, $className)) {
             return $obj->{$method->getName()}();
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function parse(Request $request)
 {
     $resource = $request->getResource();
     $classMetadata = $this->classMetadataFactory->getMetadataFor($resource->getEntityClass(), $resource->getNormalizationGroups(), $resource->getDenormalizationGroups(), $resource->getValidationGroups());
     $request->addHeader('Content-Type', 'application/json');
     $request->setDataMode(Request::DATA_MODE_RAW);
     $rawModeData = [];
     foreach ($classMetadata->getAttributes() as $attributeMetadata) {
         if (!$classMetadata->getReflectionClass()->hasProperty($attributeMetadata->getName())) {
             // Attribute is not a property: ignore it
             continue;
         }
         $groups = $this->reader->getPropertyAnnotation($classMetadata->getReflectionClass()->getProperty($attributeMetadata->getName()), 'Symfony\\Component\\Serializer\\Annotation\\Groups');
         if ($attributeMetadata->isIdentifier() || !$attributeMetadata->isReadable() || !count(array_intersect($groups ? $groups->getGroups() : [], $resource->getDenormalizationGroups() ?: []))) {
             continue;
         }
         $rawModeData[$attributeMetadata->getName()] = $this->guesser->guess($attributeMetadata);
     }
     $request->setRawModeData($rawModeData);
 }
 /**
  * {@inheritdoc}
  */
 public function isTransient($className)
 {
     $r = new ReflectionClass($className);
     foreach ($r->getProperties() as $reflectionProperty) {
         $annotation = $this->reader->getPropertyAnnotation($reflectionProperty, 'Rollerworks\\Component\\Search\\Metadata\\Field');
         if (null !== $annotation) {
             return true;
         }
     }
     return false;
 }
 /**
  * {@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);
 }
 /**
  * Returns the setter name for the inverse side.
  *
  * @param $inverseSideEntity
  * @param $owningSideEntity
  *
  * @return bool|string
  */
 protected function getReferenceSetter($inverseSideEntity, $owningSideEntity)
 {
     $inverseSideReflection = new \ReflectionClass($inverseSideEntity);
     $owningSideReflection = new \ReflectionClass($owningSideEntity);
     foreach ($inverseSideReflection->getProperties() as $inverseSideProperty) {
         $oneToManyAssociation = $this->reader->getPropertyAnnotation($inverseSideProperty, 'Doctrine\\ORM\\Mapping\\ManyToOne');
         if ($oneToManyAssociation !== null && $oneToManyAssociation->targetEntity == $owningSideReflection->getName()) {
             return $inverseSideProperty->getName();
         }
     }
     return false;
 }
 /**
  * Get fields annotations
  * 
  * @param  string $annotationName
  * @return array
  */
 protected function readPropertyAnnotations($annotationName)
 {
     $reflection = $this->getClassReflection();
     $annotations = [];
     foreach ($reflection->getProperties() as $property) {
         $annotation = $this->reader->getPropertyAnnotation($property, $annotationName);
         if ($annotation !== null) {
             $annotations[$property->getName()] = $annotation;
         }
     }
     return $annotations;
 }