/**
  * @param object $entity Entity
  *
  * @throws \Darvin\Utils\DefaultValue\DefaultValueException
  */
 protected function setDefaultValues($entity)
 {
     $entityClass = ClassUtils::getClass($entity);
     $meta = $this->extendedMetadataFactory->getExtendedMetadata($entityClass);
     if (!isset($meta['defaultValues']) || empty($meta['defaultValues'])) {
         return;
     }
     $defaultValuesMap = $meta['defaultValues'];
     $this->filterDefaultValuesMap($defaultValuesMap, $entity, $entityClass);
     if (empty($defaultValuesMap)) {
         return;
     }
     $sourcePropertyValues = $this->getSourcePropertyValues(array_unique(array_values($defaultValuesMap)), $entity, $entityClass);
     $recomputeChangeSet = false;
     foreach ($defaultValuesMap as $targetProperty => $sourcePropertyPath) {
         if (null === $sourcePropertyValues[$sourcePropertyPath]) {
             continue;
         }
         if (!$this->propertyAccessor->isWritable($entity, $targetProperty)) {
             throw new DefaultValueException(sprintf('Property "%s::$%s" is not writable.', $entityClass, $targetProperty));
         }
         $this->propertyAccessor->setValue($entity, $targetProperty, $sourcePropertyValues[$sourcePropertyPath]);
         $recomputeChangeSet = true;
     }
     if ($recomputeChangeSet) {
         $this->recomputeChangeSet($entity);
     }
 }
Beispiel #2
0
 /**
  * @param object $object Object to clone
  *
  * @return object
  * @throws \Darvin\Utils\Cloner\ClonerException
  */
 private function cloneObject($object)
 {
     $objectHash = spl_object_hash($object);
     if (isset($this->clonedObjects[$objectHash])) {
         return $this->clonedObjects[$objectHash];
     }
     $class = ClassUtils::getClass($object);
     if (false === strpos($class, '\\')) {
         $clone = clone $object;
         $this->clonedObjects[$objectHash] = $clone;
         return $clone;
     }
     $meta = $this->extendedMetadataFactory->getExtendedMetadata($class);
     if (!isset($meta['clonable'])) {
         $message = sprintf('Class "%s" must be annotated with "%s" annotation in order to create clone of it\'s instance.', $class, Clonable::ANNOTATION);
         throw new ClonerException($message);
     }
     $clone = new $class();
     $this->clonedObjects[$objectHash] = $clone;
     foreach ($meta['clonable']['properties'] as $property) {
         if (!$this->propertyAccessor->isReadable($object, $property)) {
             throw new ClonerException(sprintf('Property "%s::$%s" is not readable.', $class, $property));
         }
         if (!$this->propertyAccessor->isWritable($clone, $property)) {
             throw new ClonerException(sprintf('Property "%s::$%s" is not writable.', $class, $property));
         }
         $value = $this->propertyAccessor->getValue($object, $property);
         $valueCopy = $this->copyValue($value);
         $this->propertyAccessor->setValue($clone, $property, $valueCopy);
     }
     $this->eventDispatcher->dispatch(Events::POST_CLONE, new CloneEvent($object, $clone));
     return $clone;
 }
 /**
  * @param string $entityClass Entity class
  *
  * @return array
  */
 private function getCustomObjectMeta($entityClass)
 {
     if (!array_key_exists($entityClass, $this->customObjectMeta)) {
         $meta = $this->extendedMetadataFactory->getExtendedMetadata($entityClass);
         $this->customObjectMeta[$entityClass] = isset($meta['customObjects']) && !empty($meta['customObjects']) ? $meta['customObjects'] : null;
     }
     return $this->customObjectMeta[$entityClass];
 }
 /**
  * @param string $entityClass Entity class
  *
  * @return array
  * @throws \Darvin\Utils\Sluggable\SluggableException
  */
 private function getSlugsMetadata($entityClass)
 {
     if (!isset($this->slugsMetadata[$entityClass])) {
         $meta = $this->extendedMetadataFactory->getExtendedMetadata($entityClass);
         if (!isset($meta['slugs']) || empty($meta['slugs'])) {
             $message = sprintf('At least one property of class "%s" must be annotated with "%s" annotation in order to generate slug.', $entityClass, Slug::ANNOTATION);
             throw new SluggableException($message);
         }
         $this->slugsMetadata[$entityClass] = $meta['slugs'];
     }
     return $this->slugsMetadata[$entityClass];
 }
 /**
  * {@inheritdoc}
  */
 public function isFilterable(QueryBuilder $qb)
 {
     $entities = $qb->getRootEntities();
     if (empty($entities)) {
         return false;
     }
     foreach ($entities as $entity) {
         $extendedMeta = $this->extendedMetadataFactory->getExtendedMetadata($entity);
         if (!empty($extendedMeta['user'])) {
             return true;
         }
     }
     return false;
 }
 /**
  * @param object $entity Entity
  *
  * @throws \Darvin\Utils\Transliteratable\TransliteratableException
  */
 protected function transliterate($entity)
 {
     $entityClass = ClassUtils::getClass($entity);
     $meta = $this->extendedMetadataFactory->getExtendedMetadata($entityClass);
     if (!isset($meta['transliteratable']) || empty($meta['transliteratable'])) {
         return;
     }
     $changeSet = $this->uow->getEntityChangeSet($entity);
     $recomputeChangeSet = false;
     foreach ($meta['transliteratable'] as $property => $params) {
         if (!isset($changeSet[$property])) {
             continue;
         }
         if (!$this->propertyAccessor->isWritable($entity, $property)) {
             throw new TransliteratableException(sprintf('Property "%s::$%s" is not writable.', $entityClass, $property));
         }
         $transliterated = $this->transliterator->transliterate($changeSet[$property][1], $params['sanitize'], $params['allowedSymbols'], $params['separator']);
         $this->propertyAccessor->setValue($entity, $property, $transliterated);
         $recomputeChangeSet = true;
     }
     if ($recomputeChangeSet) {
         $this->recomputeChangeSet($entity);
     }
 }
 /**
  * @param string $entityClass Entity class
  *
  * @return string
  */
 private function getNewObjectFlag($entityClass)
 {
     $meta = $this->extendedMetadataFactory->getExtendedMetadata($entityClass);
     return isset($meta['newObjectFlag']) ? $meta['newObjectFlag'] : null;
 }