예제 #1
6
 /**
  * @param Request         $request
  * @param FilterInterface $filter
  * @param Criteria        $criteria
  * @param ClassMetadata   $embedClassMeta
  *
  * @return null
  */
 protected function applyFilter(Request $request, FilterInterface $filter, Criteria $criteria, ClassMetadata $embedClassMeta)
 {
     $properties = $filter->getRequestProperties($request);
     if ($filter instanceof OrderFilter && !empty($properties)) {
         $criteria->orderBy($properties);
         return null;
     }
     if ($filter instanceof SearchFilter) {
         foreach ($properties as $name => $propertie) {
             if (in_array($name, $embedClassMeta->getIdentifier())) {
                 continue;
             }
             $expCriterial = Criteria::expr();
             if ($embedClassMeta->hasAssociation($name)) {
                 $associationTargetClass = $embedClassMeta->getAssociationTargetClass($name);
                 $propertyResource = $this->resourceResolver->getResourceForEntity($associationTargetClass);
                 $propertyObj = $this->dataProviderChain->getItem($propertyResource, (int) $propertie['value'], true);
                 if ($propertyObj && $propertyResource instanceof ResourceInterface) {
                     $whereCriteria = $expCriterial->in($name, [$propertyObj]);
                     $criteria->where($whereCriteria);
                 }
             } else {
                 if ($embedClassMeta->hasField($name)) {
                     $fieldMapping = $embedClassMeta->getFieldMapping($name);
                     $type = isset($fieldMapping['type']) ? $fieldMapping['type'] : null;
                     $value = isset($this->mappingFilterVar[$type]) ? filter_var($propertie['value'], $this->mappingFilterVar[$type]) : $propertie['value'];
                     $whereCriteria = isset($propertie['precision']) && $propertie['precision'] === 'exact' ? $expCriterial->eq($name, $value) : $expCriterial->contains($name, $propertie['value']);
                     $criteria->where($whereCriteria);
                 }
             }
         }
     }
 }
 protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, PropertyMetadata $propertyMetadata)
 {
     /** @var \Doctrine\ODM\PHPCR\Mapping\ClassMetadata $doctrineMetadata */
     $propertyName = $propertyMetadata->name;
     if ($doctrineMetadata->hasField($propertyName) && ($fieldType = $this->normalizeFieldType($doctrineMetadata->getTypeOfField($propertyName)))) {
         $field = $doctrineMetadata->getFieldMapping($propertyName);
         if (!empty($field['multivalue'])) {
             $fieldType = 'array';
         }
         $propertyMetadata->setType($fieldType);
     } elseif ($doctrineMetadata->hasAssociation($propertyName)) {
         try {
             $targetEntity = $doctrineMetadata->getAssociationTargetClass($propertyName);
         } catch (\Exception $e) {
             return;
         }
         if (null === $this->tryLoadingDoctrineMetadata($targetEntity)) {
             return;
         }
         if (!$doctrineMetadata->isSingleValuedAssociation($propertyName)) {
             $targetEntity = "ArrayCollection<{$targetEntity}>";
         }
         $propertyMetadata->setType($targetEntity);
     }
 }
예제 #3
0
 public function readExtendedMetadata(ClassMetadata $meta, array &$config)
 {
     // load our available annotations
     require_once __DIR__ . '/../Annotations.php';
     $reader = new AnnotationReader();
     // set annotation namespace and alias
     //$reader->setAnnotationNamespaceAlias('Gedmo\Mapping\Mock\Extension\Encoder\Mapping\\', 'ext');
     $class = $meta->getReflectionClass();
     // check only property annotations
     foreach ($class->getProperties() as $property) {
         // skip inherited properties
         if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) {
             continue;
         }
         // now lets check if property has our annotation
         if ($encode = $reader->getPropertyAnnotation($property, 'Gedmo\\Mapping\\Mock\\Extension\\Encoder\\Mapping\\Encode')) {
             $field = $property->getName();
             // check if field is mapped
             if (!$meta->hasField($field)) {
                 throw new \Exception("Field is not mapped as object property");
             }
             // allow encoding only strings
             if (!in_array($encode->type, array('sha1', 'md5'))) {
                 throw new \Exception("Invalid encoding type supplied");
             }
             // validate encoding type
             $mapping = $meta->getFieldMapping($field);
             if ($mapping['type'] != 'string') {
                 throw new \Exception("Only strings can be encoded");
             }
             // store the metadata
             $config['encode'][$field] = array('type' => $encode->type, 'secret' => $encode->secret);
         }
     }
 }
예제 #4
0
 /**
  * {@inheritDoc}
  */
 public function getDateValue(ClassMetadata $meta, $field)
 {
     $mapping = $meta->getFieldMapping($field);
     if (isset($mapping['type']) && $mapping['type'] === 'timestamp') {
         return time();
     }
     return new \DateTime();
 }
예제 #5
0
 public static function validateField(ClassMetadata $meta, $field)
 {
     if ($meta->isMappedSuperclass) {
         return;
     }
     $fieldMapping = $meta->getFieldMapping($field);
     if (!in_array($fieldMapping['type'], self::$validTypes)) {
         throw new InvalidMappingException(sprintf('Field "%s" must be of one of the following types: "%s"', $fieldMapping['type'], implode(', ', self::$validTypes)));
     }
 }
예제 #6
0
 public static function validateField(ClassMetadata $meta, $field)
 {
     if ($meta->isMappedSuperclass) {
         return;
     }
     $fieldMapping = $meta->getFieldMapping($field);
     if (!$fieldMapping['type'] == self::$validTypes) {
         throw new InvalidMappingException(sprintf('Field "%s" must be of type Client', $fieldMapping['type']));
     }
 }
예제 #7
0
 /**
  * Loads the metadata for the specified class into the provided container.
  *
  * @param string        $className
  * @param ClassMetadata $metadata
  *
  * @throws \Exception
  * @return void
  */
 public function loadMetadataForClass($className, ClassMetadata $metadata)
 {
     if (!$metadata instanceof \Doctrine\ORM\Mapping\ClassMetadata) {
         throw new \Exception('Error: class metadata object is the wrong type');
     }
     $refClass = new \ReflectionClass($className);
     $classDocBlock = $refClass->getDocComment();
     if (!$classDocBlock || strpos($classDocBlock, '@Table') === false) {
         $metadata->setPrimaryTable(['name' => $this->_getTableName($className)]);
     }
     $needAutoGenerator = false;
     foreach ($refClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
         $propName = $prop->getName();
         try {
             $mapping = $metadata->getFieldMapping($propName);
         } catch (MappingException $e) {
             $mapping = null;
         }
         if (!$mapping) {
             if ($propName == 'createdAt') {
                 if (!$this->isTransient($className) && !$refClass->isAbstract() && call_user_func($className . '::useAutoTimestamp')) {
                     $metadata->mapField(['fieldName' => 'createdAt', 'columnName' => call_user_func($className . '::getCreatedAtColumn'), 'type' => 'datetime']);
                 }
             } else {
                 if ($propName == 'updatedAt') {
                     if (!$this->isTransient($className) && !$refClass->isAbstract() && call_user_func($className . '::useAutoTimestamp')) {
                         $metadata->mapField(['fieldName' => 'updatedAt', 'columnName' => call_user_func($className . '::getUpdatedAtColumn'), 'type' => 'datetime']);
                     }
                 } else {
                     $columnName = Inflector::tableize($propName);
                     $fieldMap = ['fieldName' => $propName, 'columnName' => $columnName, 'type' => $this->_getDefaultDataType($columnName)];
                     if ($columnName == 'id') {
                         $fieldMap['id'] = true;
                         $fieldMap['autoincrement'] = true;
                         $fieldMap['unsigned'] = true;
                         $needAutoGenerator = true;
                     } else {
                         if (in_array($columnName, ['price', 'tax', 'amount', 'cost', 'total'])) {
                             // DECIMAL(10,2)
                             $fieldMap['precision'] = 10;
                             $fieldMap['scale'] = 2;
                         }
                     }
                     $metadata->mapField($fieldMap);
                 }
             }
         }
     }
     if ($needAutoGenerator && !$metadata->usesIdGenerator()) {
         $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_AUTO);
     }
 }
예제 #8
0
 /**
  * Checks if $field type is valid as Sortable Position field
  *
  * @param ClassMetadata $meta
  * @param string $field
  * @return boolean
  */
 protected function isValidField($meta, $field)
 {
     $mapping = $meta->getFieldMapping($field);
     return $mapping && in_array($mapping['type'], $this->validTypes);
 }
 /**
  * @param Common\Persistence\Mapping\ClassMetadata $class
  *
  * @return array
  *
  * @throws ORM\Mapping\MappingException
  */
 private function buildPhoneFields(Common\Persistence\Mapping\ClassMetadata $class)
 {
     $phoneFields = [];
     foreach ($class->getFieldNames() as $fieldName) {
         $mapping = $class->getFieldMapping($fieldName);
         if ($mapping['type'] !== Types\Phone::PHONE) {
             continue;
         }
         $classReflection = $class->isInheritedField($fieldName) ? new \ReflectionClass($mapping['declared']) : $class->getReflectionClass();
         $phoneFields[$fieldName] = ['phoneFieldClass' => $classReflection->getName()];
     }
     return $phoneFields;
 }