/** * @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); } } } } }
public function guessColumnFormatters(\Faker\Generator $generator) { $formatters = array(); $class = $this->class; $nameGuesser = new \Faker\Guesser\Name($generator); $columnTypeGuesser = new ColumnTypeGuesser($generator); foreach ($this->class->getFieldNames() as $fieldName) { if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) { continue; } if ($formatter = $nameGuesser->guessFormat($fieldName)) { $formatters[$fieldName] = $formatter; continue; } if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) { $formatters[$fieldName] = $formatter; continue; } } foreach ($this->class->getAssociationNames() as $assocName) { if (!$this->class->isIdentifier($assocName) || !$this->class->isCollectionValuedAssociation($assocName)) { continue; } $relatedClass = $this->class->getAssociationTargetClass($assocName); $formatters[$assocName] = function ($inserted) use($relatedClass) { return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null; }; } return $formatters; }
/** * @param QueryBuilder $qb * @param Criteria $criteria */ protected function buildWhereClause(Builder $qb, Criteria $criteria) { foreach ($criteria as $key => $value) { if ($this->metadata->hasField($key) || $this->metadata->hasAssociation($key)) { $qb->field($key)->equals($value); } } }
private function mapStringUser(ClassMetadata $classMetadata) { if (!$classMetadata->hasField('createdBy')) { $classMetadata->mapField(['fieldName' => 'createdBy', 'type' => 'string', 'nullable' => true]); } if (!$classMetadata->hasField('updatedBy')) { $classMetadata->mapField(['fieldName' => 'updatedBy', 'type' => 'string', 'nullable' => true]); } }
/** * @param QueryBuilder $qb * @param Criteria $criteria */ protected function buildWhereClause(QueryBuilder $qb, Criteria $criteria) { $values = array(); foreach ($criteria as $key => $value) { if ($this->metadata->hasField($key) || $this->metadata->hasAssociation($key)) { $qb->andWhere('e.' . $key . ' = :' . $key); $values[$key] = $value; } } $qb->setParameters($values); }
/** * {@inheritdoc} */ public function getTransformerInfo(ColumnInfoInterface $columnInfo, ClassMetadata $metadata) { if (!$metadata->hasField($columnInfo->getPropertyPath())) { return; } return [$this->transformer, []]; }
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); } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } if ($timestampable = $this->reader->getPropertyAnnotation($property, self::TIMESTAMPABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}"); } if (!in_array($timestampable->on, array('update', 'create', 'change'))) { throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}"); } if ($timestampable->on == 'change') { if (!isset($timestampable->field) || !isset($timestampable->value)) { throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}"); } $field = array('field' => $field, 'trackedField' => $timestampable->field, 'value' => $timestampable->value); } // properties are unique and mapper checks that, no risk here $config[$timestampable->on][] = $field; } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // position if ($position = $this->reader->getPropertyAnnotation($property, self::POSITION)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'position' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['position'] = $field; } // group if ($group = $this->reader->getPropertyAnnotation($property, self::GROUP)) { $field = $property->getName(); if (!$meta->hasField($field) && !$meta->hasAssociation($field)) { throw new InvalidMappingException("Unable to find 'group' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!isset($config['groups'])) { $config['groups'] = array(); } $config['groups'][] = $field; } } }
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); } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations $config['fields'] = array(); $config['fields_delete'] = array(); foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } $field = null; if ($file = $this->reader->getPropertyAnnotation($property, self::FILE)) { $field['name'] = $property->getName(); $field['dir'] = CMSCore::init()->getUploadsDir() . '/' . $file->dir; if (!$meta->hasField($field['name'])) { throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}"); } } // if ($fileDelete = $this->reader->getPropertyAnnotation($property, self::FILE_DELETE)) { // // $config['fields_delete'][] = $property->getName(); // // } if ($field) { $config['fields'][] = $field; } } }
/** * {@inheritdoc} */ public function getTransformerInfo(ColumnInfoInterface $columnInfo, ClassMetadata $metadata) { if (!$metadata->hasField($columnInfo->getPropertyPath()) || $this->type != $metadata->getTypeOfField($columnInfo->getPropertyPath())) { return; } return array($this->transformer, array()); }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // slug property if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}"); } // process slug handlers $handlers = array(); if (is_array($slug->handlers) && $slug->handlers) { foreach ($slug->handlers as $handler) { if (!$handler instanceof SlugHandler) { throw new InvalidMappingException("SlugHandler: {$handler} should be instance of SlugHandler annotation in entity - {$meta->name}"); } if (!strlen($handler->class)) { throw new InvalidMappingException("SlugHandler class: {$handler->class} should be a valid class name in entity - {$meta->name}"); } $class = $handler->class; $handlers[$class] = array(); foreach ((array) $handler->options as $option) { if (!$option instanceof SlugHandlerOption) { throw new InvalidMappingException("SlugHandlerOption: {$option} should be instance of SlugHandlerOption annotation in entity - {$meta->name}"); } if (!strlen($option->name)) { throw new InvalidMappingException("SlugHandlerOption name: {$option->name} should be valid name in entity - {$meta->name}"); } $handlers[$class][$option->name] = $option->value; } $class::validate($handlers[$class], $meta); } } // process slug fields if (empty($slug->fields) || !is_array($slug->fields)) { throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}"); } foreach ($slug->fields as $slugField) { if (!$meta->hasField($slugField)) { throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $slugField)) { throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}"); } } // set all options $config['slugs'][$field] = array('fields' => $slug->fields, 'slug' => $field, 'style' => $slug->style, 'updatable' => $slug->updatable, 'unique' => $slug->unique, 'separator' => $slug->separator, 'handlers' => $handlers); } } }
/** * Gets a persistent field value. * * @param string $field * * @return mixed * * @throws \BadMethodCallException When no persistent field exists by that name. */ private function get($field) { $this->initializeDoctrine(); if ($this->cm->hasField($field) || $this->cm->hasAssociation($field)) { return $this->{$field}; } else { throw new \BadMethodCallException("no field with name '" . $field . "' exists on '" . $this->cm->getName() . "'"); } }
/** * {@inheritDoc} */ protected function validateExtendedMetadata(ClassMetadata $baseClassMetadata, ClassMetadataInterface $extendedClassMetadata) { if ($extendedClassMetadata->hasTranslatableProperties()) { if (!isset($extendedClassMetadata->localeProperty)) { throw new Exception\MappingException('Entity \'' . $baseClassMetadata->name . '\' has translatable properties so it must have property marked with @Translatable\\Language annotation'); } $translatableProperties = $extendedClassMetadata->getTranslatableProperties(); foreach ($translatableProperties as $translation => $properties) { if (!$baseClassMetadata->hasAssociation($translation) || !$baseClassMetadata->isCollectionValuedAssociation($translation)) { throw new Exception\MappingException('Field \'' . $translation . '\' in entity \'' . $baseClassMetadata->name . '\' has to be a OneToMany association'); } } } if (isset($extendedClassMetadata->localeProperty)) { if ($extendedClassMetadata->hasTranslatableProperties() && ($baseClassMetadata->hasField($extendedClassMetadata->localeProperty) || $baseClassMetadata->hasAssociation($extendedClassMetadata->localeProperty))) { throw new Exception\MappingException('Entity \'' . $baseClassMetadata->name . '\' seems to be a translatable entity so its \'' . $extendedClassMetadata->localeProperty . '\' field must not be persistent'); } else { if (!$extendedClassMetadata->hasTranslatableProperties() && !$baseClassMetadata->hasField($extendedClassMetadata->localeProperty) && !$baseClassMetadata->hasAssociation($extendedClassMetadata->localeProperty)) { throw new Exception\MappingException('Entity \'' . $baseClassMetadata->name . '\' seems to be a translation entity so its \'' . $extendedClassMetadata->localeProperty . '\' field must be persistent'); } } } }
/** * {@inheritdoc} */ public function readMetadata(ClassMetadata $doctrineMeta, array &$meta) { if (!isset($meta['transliteratable'])) { $meta['transliteratable'] = []; } foreach ($doctrineMeta->getReflectionClass()->getProperties() as $reflectionProperty) { $transliteratableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Transliteratable::ANNOTATION); if ($transliteratableAnnotation instanceof Transliteratable) { if (!$doctrineMeta->hasField($reflectionProperty->getName())) { throw $this->createPropertyAnnotationInvalidException(Transliteratable::ANNOTATION, $doctrineMeta->getName(), $reflectionProperty->getName(), 'property must be mapped field'); } $meta['transliteratable'][$reflectionProperty->getName()] = get_object_vars($transliteratableAnnotation); } } }
/** * Builds the entity * * @param ClassMetadata $metadata * @param array $data * * @return object * @throws \Exception */ private function buildEntity(ClassMetadata $metadata, $data) { $class = $metadata->getName(); $entity = new $class(); foreach ($data as $propertyPath => $value) { // Field if ($metadata->hasField($propertyPath)) { $builtValue = $this->buildFieldValue($metadata, $propertyPath, $value); // Association } elseif ($metadata->hasAssociation($propertyPath)) { $builtValue = $this->buildAssociationValue($metadata, $propertyPath, $value); } else { throw new \Exception("Unexpected property path '{$propertyPath}'."); } $this->accessor->setValue($entity, $propertyPath, $builtValue); } return $entity; }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } if ($demo = $this->reader->getPropertyAnnotation($property, self::DEMO)) { $field = $property->getName(); $value = $demo->text; if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find timestampable [{$field}] as mapped property in entity - {$meta->name}"); } $config['text'][] = array('field' => $field, 'value' => $value); } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // class annotations if ($annot = $this->reader->getClassAnnotation($class, self::ENTITY_CLASS)) { if (!class_exists($annot->class)) { throw new InvalidMappingException("Translation class: {$annot->class} does not exist."); } $config['translationClass'] = $annot->class; } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // translatable property if ($translatable = $this->reader->getPropertyAnnotation($property, self::TRANSLATABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find translatable [{$field}] as mapped property in entity - {$meta->name}"); } // fields cannot be overrided and throws mapping exception $config['fields'][] = $field; } // locale property if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw new InvalidMappingException("Locale field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence"); } $config['locale'] = $field; } elseif ($language = $this->reader->getPropertyAnnotation($property, self::LANGUAGE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw new InvalidMappingException("Language field [{$field}] should not be mapped as column property in entity - {$meta->name}, since it makes no sence"); } $config['locale'] = $field; } } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // sluggable property if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}"); } if (!is_null($sluggable->slugField) and !$meta->hasField($sluggable->slugField)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } $config['fields'][$sluggable->slugField][] = array('field' => $field, 'position' => $sluggable->position, 'slugField' => $sluggable->slugField); } // slug property if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}"); } $config['slugFields'][$field]['slug'] = $field; $config['slugFields'][$field]['style'] = $slug->style; $config['slugFields'][$field]['updatable'] = $slug->updatable; $config['slugFields'][$field]['unique'] = $slug->unique; $config['slugFields'][$field]['separator'] = $slug->separator; } } }
protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, PropertyMetadata $propertyMetadata) { $propertyName = $propertyMetadata->name; if ($doctrineMetadata->hasField($propertyName) && ($fieldType = $this->normalizeFieldType($doctrineMetadata->getTypeOfField($propertyName)))) { $propertyMetadata->setType($fieldType); } elseif ($doctrineMetadata->hasAssociation($propertyName)) { $targetEntity = $doctrineMetadata->getAssociationTargetClass($propertyName); if (null === ($targetMetadata = $this->tryLoadingDoctrineMetadata($targetEntity))) { return; } // For inheritance schemes, we cannot add any type as we would only add the super-type of the hierarchy. // On serialization, this would lead to only the supertype being serialized, and properties of subtypes // being ignored. if ($targetMetadata instanceof DoctrineClassMetadata && !$targetMetadata->isInheritanceTypeNone()) { return; } if (!$doctrineMetadata->isSingleValuedAssociation($propertyName)) { $targetEntity = "ArrayCollection<{$targetEntity}>"; } $propertyMetadata->setType($targetEntity); } }
/** * {@inheritdoc} */ public function readMetadata(ClassMetadata $doctrineMeta, array &$meta) { if (!isset($meta['newObjectFlag'])) { $meta['newObjectFlag'] = null; } foreach ($doctrineMeta->getReflectionClass()->getProperties() as $reflectionProperty) { if (null === $this->reader->getPropertyAnnotation($reflectionProperty, NewObjectFlag::ANNOTATION)) { continue; } $objectClass = $doctrineMeta->getName(); $property = $reflectionProperty->getName(); if (!empty($meta['newObjectFlag'])) { if ($meta['newObjectFlag'] === $property) { continue; } throw $this->createPropertyAnnotationInvalidException(NewObjectFlag::ANNOTATION, $objectClass, $property, sprintf('property "%s" is already marked as new object flag', $meta['newObjectFlag'])); } if (!$doctrineMeta->hasField($property)) { throw $this->createPropertyAnnotationInvalidException(NewObjectFlag::ANNOTATION, $objectClass, $property, 'property must be mapped field'); } $meta['newObjectFlag'] = $property; } }
/** * Generates the list of public properties to be lazy loaded, with their default values. * * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class * * @return mixed[] */ private function getLazyLoadedPublicProperties(ClassMetadata $class) { $defaultProperties = $class->getReflectionClass()->getDefaultProperties(); $properties = array(); foreach ($class->getReflectionClass()->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { $name = $property->getName(); if (($class->hasField($name) || $class->hasAssociation($name)) && !$class->isIdentifier($name)) { $properties[$name] = $defaultProperties[$name]; } } return $properties; }
/** * Add a "metaKeywords" field * * @param ClassMetadata $classMetadata */ protected function mapMetaKeywords(ClassMetadata $classMetadata) { if (!$classMetadata->hasField(self::META_KEYWORDS)) { $classMetadata->mapField(['fieldName' => self::META_KEYWORDS, 'type' => 'string', 'length' => 255, 'nullable' => true]); } }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited']) ) { continue; } // sluggable property if ($sluggable = $this->reader->getPropertyAnnotation($property, self::SLUGGABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find sluggable [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}"); } if (!is_null($sluggable->slugField) and !$meta->hasField($sluggable->slugField)) { throw new InvalidMappingException(sprintf('The "%s" property - which is defined as the "slugField" for the "%s" property - does not exist or is not mapped to Doctrine in "%s"', $sluggable->slugField, $field, $meta->name)); } $config['fields'][$sluggable->slugField][] = array('field' => $field, 'position' => $sluggable->position, 'slugField' => $sluggable->slugField); } // slug property if ($slug = $this->reader->getPropertyAnnotation($property, self::SLUG)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find slug [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}"); } // process slug handlers if (is_array($slug->handlers) && $slug->handlers) { foreach ($slug->handlers as $handler) { if (!$handler instanceof SlugHandler) { throw new InvalidMappingException("SlugHandler: {$handler} should be instance of SlugHandler annotation in entity - {$meta->name}"); } if (!strlen($handler->class)) { throw new InvalidMappingException("SlugHandler class: {$handler->class} should be a valid class name in entity - {$meta->name}"); } $class = $handler->class; $config['handlers'][$class] = array(); foreach ((array)$handler->options as $option) { if (!$option instanceof SlugHandlerOption) { throw new InvalidMappingException("SlugHandlerOption: {$option} should be instance of SlugHandlerOption annotation in entity - {$meta->name}"); } if (!strlen($option->name)) { throw new InvalidMappingException("SlugHandlerOption name: {$option->name} should be valid name in entity - {$meta->name}"); } $config['handlers'][$class][$option->name] = $option->value; } $class::validate($config['handlers'][$class], $meta); } } $config['slugFields'][$field]['slug'] = $field; $config['slugFields'][$field]['style'] = $slug->style; $config['slugFields'][$field]['updatable'] = $slug->updatable; $config['slugFields'][$field]['unique'] = $slug->unique; $config['slugFields'][$field]['separator'] = $slug->separator; } } }
/** * @param ClassMetadata $metadata * @param $name * @return bool */ protected function checkForExcludeElementFromMetadata(ClassMetadata $metadata, $name) { $params = array('metadata' => $metadata, 'name' => $name); $result = false; if ($metadata->hasField($name)) { $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_FIELD, $this, $params); } elseif ($metadata->hasAssociation($name)) { $result = $this->getEventManager()->trigger(static::EVENT_EXCLUDE_ASSOCIATION, $this, $params); } if ($result) { $result = (bool) $result->last(); } return $result; }
/** * Return "false" if can't find config for field, "null" if field type is unknown for given field * or array with config data for given field * * @param ClassMetadata $metadata * @param $field * @return array|bool */ protected function guessAttributeParametersScalarField(ClassMetadata $metadata, $field) { if ($metadata->hasField($field)) { $doctrineType = $metadata->getTypeOfField($field); if (!isset($this->doctrineTypeMapping[$doctrineType])) { return null; } return $this->formatResult($this->getLabel($metadata->getName(), $field), $this->doctrineTypeMapping[$doctrineType]['type'], $this->doctrineTypeMapping[$doctrineType]['options']); } elseif ($this->entityConfigProvider->hasConfig($metadata->getName(), $field)) { $entityConfig = $this->entityConfigProvider->getConfig($metadata->getName(), $field); $fieldType = $entityConfig->getId()->getFieldType(); if (!$metadata->hasAssociation($field)) { return $this->formatResult($entityConfig->get('label'), $this->doctrineTypeMapping[$fieldType]['type'], $this->doctrineTypeMapping[$fieldType]['options']); } } return false; }
/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // class annotations if ($annot = $this->reader->getClassAnnotation($class, self::TREE)) { if (!in_array($annot->type, $this->strategies)) { throw new InvalidMappingException("Tree type: {$annot->type} is not available."); } $config['strategy'] = $annot->type; } if ($annot = $this->reader->getClassAnnotation($class, self::CLOSURE)) { if (!class_exists($annot->class)) { throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist."); } $config['closure'] = $annot->class; } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // left if ($left = $this->reader->getPropertyAnnotation($property, self::LEFT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['left'] = $field; } // right if ($right = $this->reader->getPropertyAnnotation($property, self::RIGHT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['right'] = $field; } // ancestor/parent if ($parent = $this->reader->getPropertyAnnotation($property, self::PARENT)) { $field = $property->getName(); if (!$meta->isSingleValuedAssociation($field)) { throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}"); } $config['parent'] = $field; } // root if ($root = $this->reader->getPropertyAnnotation($property, self::ROOT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['root'] = $field; } // level if ($parent = $this->reader->getPropertyAnnotation($property, self::LEVEL)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}"); } if (!$this->isValidField($meta, $field)) { throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}"); } $config['level'] = $field; } } }
/** * Generates a closure capable of finalizing a cloned proxy * * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata * @param \Doctrine\ODM\MongoDB\Persisters\DocumentPersister $documentPersister * @param \ReflectionProperty $reflectionId * * @return \Closure * * @throws \Doctrine\ODM\MongoDB\DocumentNotFoundException */ private function createCloner(BaseClassMetadata $classMetadata, DocumentPersister $documentPersister, ReflectionProperty $reflectionId) { return function (BaseProxy $proxy) use($documentPersister, $classMetadata, $reflectionId) { if ($proxy->__isInitialized()) { return; } $proxy->__setInitialized(true); $proxy->__setInitializer(null); $id = $reflectionId->getValue($proxy); $original = $documentPersister->load(array('_id' => $id)); if (null === $original) { throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); } foreach ($classMetadata->getReflectionClass()->getProperties() as $reflectionProperty) { $propertyName = $reflectionProperty->getName(); if ($classMetadata->hasField($propertyName) || $classMetadata->hasAssociation($propertyName)) { $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original)); } } }; }
/** * Add a "deletedAt" field to a SoftDeletable entity, if necessary * * @param ClassMetadata $classMetadata */ protected function mapDeletedAt(ClassMetadata $classMetadata) { if (!$classMetadata->hasField(self::DELETED_AT_FIELD)) { $classMetadata->mapField(['fieldName' => self::DELETED_AT_FIELD, 'type' => 'datetime', 'nullable' => true]); } }