/** * {@inheritDoc} */ public function readExtendedMetadata(ClassMetadata $meta, array &$config) { $class = $meta->getReflectionClass(); // class annotations if ($annot = $this->reader->getClassAnnotation($class, self::LOGGABLE)) { $config['loggable'] = true; if ($annot->logEntryClass) { if (!class_exists($annot->logEntryClass)) { throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist."); } $config['logEntryClass'] = $annot->logEntryClass; } } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // versioned property if ($versioned = $this->reader->getPropertyAnnotation($property, self::VERSIONED)) { $field = $property->getName(); if ($meta->isCollectionValuedAssociation($field)) { throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}"); } // fields cannot be overrided and throws mapping exception $config['versioned'][] = $field; } } }
/** * {@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 ($this->class !== $metadata->getName() || !$columnInfo->getAttribute() || $this->backendType !== $columnInfo->getAttribute()->getBackendType()) { return; } return array($this->transformer, array()); }
public function unserialize($value, ClassMetadata $metadata, $field) { $array = array(); $serializerMetadata = $metadata->getSerializer(); //reset value to array if (!is_array($value)) { if ($value instanceof ArrayCollection) { $value = $value->toArray(); } elseif ($value instanceof ArrayObject) { $value = $value->getArrayCopy(); } } if (isset($serializerMetadata['fields'][$field]['collectionType'])) { switch ($serializerMetadata['fields'][$field]['collectionType']) { case 'ArrayObject': $array = new ArrayObject($value); break; case 'ArrayCollection': $array = new ArrayCollection($value); break; case 'Array': case 'array': default: $array = $value; break; } } else { $array = (array) $value; } return $array; }
/** * Loads the metadata for the specified class into the provided container. * * @param string $className * @param ClassMetadata $metadata */ public function loadMetadataForClass($className, ClassMetadata $metadata) { $class = $metadata->getReflectionClass(); if (!$class) { // this happens when running annotation driver in combination with // static reflection services. This is not the nicest fix $class = new \ReflectionClass($metadata->name); } $entityAnnot = $this->reader->getClassAnnotation($class, 'Doctrine\\KeyValueStore\\Mapping\\Annotations\\Entity'); if (!$entityAnnot) { throw new \InvalidArgumentException($metadata->name . ' is not a valid key-value-store entity.'); } $metadata->storageName = $entityAnnot->storageName; // Evaluate annotations on properties/fields foreach ($class->getProperties() as $property) { $idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\KeyValueStore\\Mapping\\Annotations\\Id'); $transientAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\KeyValueStore\\Mapping\\Annotations\\Transient'); if ($idAnnot) { $metadata->mapIdentifier($property->getName()); } elseif ($transientAnnot) { $metadata->skipTransientField($property->getName()); } else { $metadata->mapField(['fieldName' => $property->getName()]); } } }
/** * {@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()); }
/** * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param \Doctrine\Common\Persistence\ManagerRegistry $doctrine * @param \Doctrine\Common\Persistence\ObjectManager $manager * @param \Behat\Behat\Hook\Scope\ScenarioScope $event * @param \Behat\Gherkin\Node\FeatureNode $feature * @param \Behat\Gherkin\Node\ScenarioNode $scenario * @param \Knp\FriendlyContexts\Alice\Loader\Yaml $loader * @param \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory $metadataFactory * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $userMetadata * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $placeMetadata * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $productMetadata */ function let($container, $doctrine, $manager, $event, $loader, $feature, $scenario, $metadataFactory, $userMetadata, $placeMetadata, $productMetadata) { $doctrine->getManager()->willReturn($manager); $feature->getTags()->willReturn(['alice(Place)', 'admin']); $scenario->getTags()->willReturn(['alice(User)']); $event->getFeature()->willReturn($feature); $event->getScenario()->willReturn($scenario); $loader->load('user.yml')->willReturn([]); $loader->load('product.yml')->willReturn([]); $loader->load('place.yml')->willReturn([]); $loader->getCache()->willReturn([]); $loader->clearCache()->willReturn(null); $fixtures = ['User' => 'user.yml', 'Product' => 'product.yml', 'Place' => 'place.yml']; $config = ['alice' => ['fixtures' => $fixtures, 'dependencies' => []]]; $container->has(Argument::any())->willReturn(true); $container->hasParameter(Argument::any())->willReturn(true); $container->get('friendly.alice.loader.yaml')->willReturn($loader); $container->get('doctrine')->willReturn($doctrine); $container->getParameter('friendly.alice.fixtures')->willReturn($fixtures); $container->getParameter('friendly.alice.dependencies')->willReturn([]); $manager->getMetadataFactory()->willReturn($metadataFactory); $metadataFactory->getAllMetadata()->willReturn([$userMetadata, $placeMetadata, $productMetadata]); $userMetadata->getName()->willReturn('User'); $placeMetadata->getName()->willReturn('Place'); $productMetadata->getName()->willReturn('Product'); $this->initialize($config, $container); }
/** * Loads the metadata for the specified class into the provided container. * * @param string $className * @param CommonClassMetadata $metadata * * @return void */ public function loadMetadataForClass($className, CommonClassMetadata $metadata) { /** @var \Doctrine\KeyValueStore\Mapping\ClassMetadata $metadata */ try { $element = $this->getElement($className); } catch (MappingException $exception) { throw new \InvalidArgumentException($metadata->name . ' is not a valid key-value-store entity.'); } $class = new \ReflectionClass($className); if (isset($element['storageName'])) { $metadata->storageName = $element['storageName']; } $ids = []; if (isset($element['id'])) { $ids = $element['id']; } $transients = []; if (isset($element['transient'])) { $transients = $element['transient']; } foreach ($class->getProperties() as $property) { if (in_array($property->getName(), $ids)) { $metadata->mapIdentifier($property->getName()); continue; } if (in_array($property->getName(), $transients)) { $metadata->skipTransientField($property->getName()); continue; } $metadata->mapField(array('fieldName' => $property->getName())); } }
protected function getEntityFields(ClassMetadata $metaData, array $data) { // change data keys to camelcase $result = array(); foreach ($data as $key => $value) { // convert to camelcase if underscore is in name if (strpos($key, '_') !== false) { $key = implode('', array_map('ucfirst', explode('_', $key))); } $result[$key] = $value; } $data = $result; // get all fields $fieldNames = $metaData->getFieldNames(); $fields = array(); foreach ($fieldNames as $fieldName) { if (!isset($data[$fieldName])) { continue; } $type = $metaData->getTypeOfField($fieldName); $value = $this->getColumnTypeValue($type, $data[$fieldName]); $fields[$fieldName] = $value; } return $fields; }
/** * {@inheritdoc} */ public function getTransformerInfo(ColumnInfoInterface $columnInfo, ClassMetadata $metadata) { if (!$metadata->hasField($columnInfo->getPropertyPath())) { return; } return [$this->transformer, []]; }
/** * {@inheritdoc} */ public function getTransformerInfo(ColumnInfoInterface $columnInfo, ClassMetadata $metadata) { if (!$columnInfo->getLocale() && !count($columnInfo->getSuffixes()) || !$metadata->hasAssociation('translations')) { return; } return array($this->transformer, array()); }
/** * @param ClassMetadata $metadata * * @return bool */ protected function isResource(ClassMetadata $metadata) { if (!($reflClass = $metadata->getReflectionClass())) { return false; } return $reflClass->implementsInterface(PersistableInterface::class); }
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) { $mapping = $this->_getMapping($meta->name); if (isset($mapping['gedmo'])) { $classMapping = $mapping['gedmo']; if (isset($classMapping['loggable'])) { $config['loggable'] = true; if (isset($classMapping['loggable']['logEntryClass'])) { if (!class_exists($classMapping['loggable']['logEntryClass'])) { throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist."); } $config['logEntryClass'] = $classMapping['loggable']['logEntryClass']; } } } if (isset($mapping['fields'])) { foreach ($mapping['fields'] as $field => $fieldMapping) { if (isset($fieldMapping['gedmo'])) { if (in_array('versioned', $fieldMapping['gedmo'])) { if ($meta->isCollectionValuedAssociation($field)) { throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}"); } // fields cannot be overrided and throws mapping exception $config['versioned'][] = $field; } } } } }
/** * Checks if the given relation should be ignored in workflows * * @param ClassMetadata $metadata * @param string $associationName * * @return bool */ protected function isIgnoredInWorkflow(ClassMetadata $metadata, $associationName) { if ($this->isWorkflowField($associationName)) { return true; } return !$metadata->isSingleValuedAssociation($associationName); }
protected function appendConfigurationFromMetadata(ClassMetadata $metadata, &$configuration) { $serializationFilePath = $this->resolvePath($metadata->getReflectionClass()); if ($this->filesystem->exists($serializationFilePath)) { $content = file_get_contents($serializationFilePath); $configuration = array_replace_recursive($configuration, $this->parseContent($content)); } }
/** * Initializes Reflection after ClassMetadata was constructed. * * @param ClassMetadata $class * @param ReflectionService $reflService * * @return void */ protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService) { if (!$class instanceof EntityMetadata) { throw new \LogicException('Metadata is not supported'); } /** @var EntityMetadata $class */ $class->initializeReflection($reflService); }
/** * {@inheritDoc} */ public function getDateValue(ClassMetadata $meta, $field) { $mapping = $meta->getFieldMapping($field); if (isset($mapping['type']) && $mapping['type'] === 'timestamp') { return time(); } return new \DateTime(); }
/** * @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); } } }
/** * {@inheritdoc} */ public function getTransformerInfo(ColumnInfoInterface $columnInfo, ClassMetadata $metadata) { $mapping = $this->getMapping(); if (!$metadata->hasAssociation('translations') || !isset($mapping[$columnInfo->getName()])) { return null; } return array($this->transformer, array('propertyPath' => $mapping[$columnInfo->getName()])); }
/** * @return TranslatableRepositoryInterface * @throws Exception\AnnotationException */ public function getTranslatableRepository() { $repository = $this->objectManager->getRepository($this->classMetadata->getName()); if (!$repository instanceof TranslatableRepositoryInterface) { throw new Exception\AnnotationException(sprintf('Entity "%s" has "%s" as its "repositoryClass" which does not implement \\FSi\\DoctrineExtensions\\Translatable\\Model\\TranslatableRepositoryInterface', $this->classMetadata->getName(), get_class($repository))); } return $repository; }
protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService) { $class->reflClass = $reflService->getClass($class->name); if ($class->reflClass) { foreach ($class->reflClass->getProperties() as $property) { $class->mapField(['fieldName' => $property->getName()]); } } }
/** * {@inheritDoc} */ public function mapTranslation(ClassMetadata $meta, $translatableClassName) { $rc = $meta->getReflectionClass(); if (!$rc->hasProperty('object') || $meta->hasAssociation('object') || !$rc->isSubclassOf($this->personalTranslation)) { return; } $namingStrategy = $this->getObjectManager()->getConfiguration()->getNamingStrategy(); $meta->mapManyToOne(['targetEntity' => $translatableClassName, 'fieldName' => 'object', 'inversedBy' => 'translations', 'isOwningSide' => true, 'joinColumns' => [['name' => $namingStrategy->joinColumnName('object'), 'referencedColumnName' => $namingStrategy->referenceColumnName(), 'onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']]]); }
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]); } }
function it_schedules_owning_document_for_update_when_setting_element_by_key_in_the_collection(MongoDBODMUnitOfWork $uow, DocumentStub $document, ObjectRepository $repository, ClassMetadata $classMetadata, EntityStub $entity4, EntityStub $entity8, EntityStub $entity15, EntityStub $newEntity) { $classMetadata->getIdentifier()->willReturn(['id']); $repository->findBy(['id' => [4, 8, 15]])->willReturn([$entity4, $entity8, $entity15]); $uow->getDocumentState($document)->willReturn(MongoDBODMUnitOfWork::STATE_MANAGED); $uow->isScheduledForUpdate($document)->willReturn(false); $uow->scheduleForUpdate($document)->shouldBeCalled(); $this->setOwner($document); $this->set(2, $newEntity); }
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'])); } }
/** * @param ClassMetadata $metadata */ private function mapManyToMany(ClassMetadata $metadata) { foreach ($this->variables as $variable => $class) { if ($class['variant']['model'] !== $metadata->getName()) { continue; } $metadata->mapManyToOne(array('fieldName' => 'object', 'targetEntity' => $class['variable'], 'inversedBy' => 'variants', 'joinColumns' => array(array('name' => $variable . '_id', 'referencedColumnName' => 'id', 'nullable' => false, 'onDelete' => 'CASCADE')))); $metadata->mapManyToMany(array('fieldName' => 'options', 'type' => ClassMetadataInfo::MANY_TO_MANY, 'targetEntity' => $class['option_value']['model'], 'joinTable' => array('name' => sprintf('sylius_%s_variant_option_value', $variable), 'joinColumns' => array(array('name' => 'variant_id', 'referencedColumnName' => 'id', 'unique' => false, 'nullable' => false, 'onDelete' => 'CASCADE')), 'inverseJoinColumns' => array(array('name' => 'option_value_id', 'referencedColumnName' => 'id', 'unique' => false, 'nullable' => false, 'onDelete' => 'CASCADE'))))); } }
/** * It is a WORKAROUND because Doctrine ClassMetadata has different implementation * for ORM and ODM. * * @param ClassMetadata $metadata * @return bool */ private function isIdentifierNatural(ClassMetadata $metadata) { if ($metadata instanceof ORMClassMetadataInfo) { return $metadata->isIdentifierNatural(); } if ($metadata instanceof ODMClassMetadataInfo) { return $metadata->generatorType === ODMClassMetadataInfo::GENERATOR_TYPE_NONE; } return true; }
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))); } }
public function setUp() { $this->class = 'Abc\\Bundle\\SequenceBundle\\Entity\\Sequence'; $this->classMetaData = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $this->objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $this->repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $this->objectManager->expects($this->any())->method('getClassMetadata')->will($this->returnValue($this->classMetaData)); $this->classMetaData->expects($this->any())->method('getName')->will($this->returnValue($this->class)); $this->objectManager->expects($this->any())->method('getRepository')->will($this->returnValue($this->repository)); $this->subject = new SequenceManager($this->objectManager, $this->class); }