Example #1
0
 /**
  * Função genérica que lerá as annotations da classe e verificará
  * coisas como tipo do dado, comprimento, qtd de itens na collection, etc
  *
  * @ORM\PreFlush()
  */
 public function isValid()
 {
     $reflx = new \ReflectionClass($this);
     $reader = new IndexedReader(new AnnotationReader());
     $props = $reflx->getProperties();
     //$annotations = $reader->getClassAnnotation($reflx);
     foreach ($props as $prop) {
         $annons = $reader->getPropertyAnnotations($prop);
         //var_dump($annons);
         $attr = $prop->getName();
         $method = 'get' . ucfirst($attr);
         if (!strstr($attr, '__') && $attr != 'lazyPropertiesDefaults' && $attr != 'id' && (method_exists($this, $method) && is_object($this->{$method}()) && $this->{$method}() instanceof \SanSIS\BizlayBundle\Entity\AbstractEntity && is_object($this->__parent) && $this->{$method}() !== $this->__parent)) {
             if ((isset($annons['Doctrine\\ORM\\Mapping\\ManyToOne']) || isset($annons['Doctrine\\ORM\\Mapping\\OneToOne'])) && is_object($this->{$method}())) {
                 $this->{$method}()->setParent($this);
                 $this->{$method}()->isValid($this);
             } else {
                 if (isset($annons['Doctrine\\ORM\\Mapping\\ManyToMany']) || isset($annons['Doctrine\\ORM\\Mapping\\OneToMany'])) {
                     foreach ($this->{$method}() as $obj) {
                         if (is_object($obj)) {
                             $obj->setParent($this);
                             $obj->isValid($this);
                         }
                     }
                 } else {
                     if (isset($annons['Doctrine\\ORM\\Mapping\\Column'])) {
                         $this->checkType($attr, $annons['Doctrine\\ORM\\Mapping\\Column']->type, $annons['Doctrine\\ORM\\Mapping\\Column']->nullable);
                         $this->checkMaxSize($attr, $annons['Doctrine\\ORM\\Mapping\\Column']->length);
                         $this->checkNullable($attr, $annons['Doctrine\\ORM\\Mapping\\Column']->nullable);
                     }
                 }
             }
         }
     }
     if ($this->__parent != $this && $this->hasErrors()) {
         throw new ValidationException($this->getErrors());
     }
     if ($this->hasErrors()) {
         throw new ValidationException($this->getErrors());
     }
 }
 /**
  * @group DCOM-4
  */
 public function testNamespaceAliasAnnotationWithSeparator()
 {
     $reader = new IndexedReader(new AnnotationReader(new \Doctrine\Common\Cache\ArrayCache()));
     $reader->setAnnotationNamespaceAlias('Doctrine\\Tests\\Common\\', 'alias');
     $reflClass = new ReflectionClass('Doctrine\\Tests\\Common\\Annotations\\Regression\\AliasNamespace');
     $result = $reader->getPropertyAnnotations($reflClass->getProperty('foo'));
     $this->assertEquals(1, count($result));
     $annot = $result['Doctrine\\Tests\\Common\\Annotations\\Name'];
     $this->assertTrue($annot instanceof \Doctrine\Tests\Common\Annotations\Name);
     // no use!
     $this->assertEquals('bar', $annot->foo);
 }
 /**
  * Verifica atributos unique em registros ativos
  *
  * @param  ServiceDto $dto [description]
  * @return null          [description]
  */
 public function checkUnique(ServiceDto $dto, AbstractEntity $entity)
 {
     $reflx = new \ReflectionClass($this->getEntityName());
     $reader = new IndexedReader(new AnnotationReader());
     $props = $reflx->getProperties();
     foreach ($props as $prop) {
         $annons = $reader->getPropertyAnnotations($prop);
         if ($prop->getName() != 'id' && isset($annons['Doctrine\\ORM\\Mapping\\Column']->unique) && $annons['Doctrine\\ORM\\Mapping\\Column']->unique) {
             $pt = $annons['Doctrine\\ORM\\Mapping\\Column']->type;
             $getMethod = 'get' . ucfirst($prop->getName());
             $uniqueParam = $entity->{$getMethod}();
             //verificar e adicionar o erro
             $qb = $this->createQueryBuilder('u');
             $qb->select('u.id')->andWhere($qb->expr()->eq($this->ci('u.' . $prop->getName(), $pt), $this->ci(':param', $pt)))->setParameter('param', $uniqueParam);
             $id = $entity->getId();
             if (is_null($id)) {
                 $qb->andWhere($qb->expr()->isNotNull('u.id'));
             } else {
                 $qb->andWhere($qb->expr()->neq('u.id', ':id'))->setParameter('id', $id);
             }
             $query = $qb->getQuery();
             $boolAttr = $reflx->hasProperty('isActive') ? 'isActive' : ($reflx->hasProperty('flActive') ? 'flActive' : false);
             if ($boolAttr) {
                 $query->setDQL($query->getDQL() . ' and u.' . $boolAttr . ' = :boolFlag');
                 $query->setParameter('boolFlag', true);
             }
             if ($reflx->hasProperty('statusTuple')) {
                 $query->setDQL($query->getDQL() . ' and u.statusTuple <> 0 ');
             }
             if (count($query->getScalarResult())) {
                 $this->addError('verificação', 'Já existe o valor informado para ' . $prop->getName() . '.', 'Repository', get_class($entity), $prop->getName());
             }
         }
     }
 }