src/Util/EntityUtil.php line 138

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Util;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\Validator\Validator\ValidatorInterface;
  5. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  6. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  7. use Symfony\Component\Serializer\Serializer;
  8. use EADPlataforma\Services\GeneralService;
  9. use \ReflectionClass;
  10. use \ReflectionProperty;
  11. use \Exception;
  12. class EntityUtil{
  13.     /**
  14.      *
  15.      * @var Entity
  16.      *
  17.      */
  18.     private $entity;
  19.     /**
  20.      * @var SchoolEntityManager
  21.      */
  22.     protected $em;
  23.     /**
  24.      *
  25.      * @var ValidatorInterface $validator
  26.      *
  27.      */
  28.     private $validator;
  29.     /**
  30.      * Constructor
  31.      *
  32.      * @param GeneralService $generalService
  33.      * @param ValidatorInterface $validator
  34.      */
  35.     public function __construct(GeneralService $generalServiceValidatorInterface $validator)
  36.     {
  37.         $this->em $generalService->getService('SchoolEntityManager');
  38.         $this->validator $validator;
  39.     }
  40.     public function setEntity($entity)
  41.     {
  42.         $this->entity $entity;
  43.         return $this;
  44.     }
  45.     public function json() {
  46.         if($this->entity){
  47.             $encoders = [new JsonEncoder()];
  48.             $normalizers = [new ObjectNormalizer()];
  49.             $serializer = new Serializer($normalizers$encoders);
  50.             $jsonContent $serializer->serialize($this->entity'json', [
  51.                 'circular_reference_handler' => function ($object) {
  52.                     return $object->getId();
  53.                 }
  54.             ]);
  55.             return $jsonContent;
  56.         }else{
  57.             throw new Exception("Entity not found"1);
  58.         }
  59.     }
  60.     public function getClassName($onlyName false)
  61.     {
  62.         if($this->entity){
  63.             $className get_class($this->entity);
  64.             
  65.             if($onlyName){
  66.                 $className explode("\\"$className);
  67.                 $className end($className);
  68.             }
  69.             return $className;
  70.         }else{
  71.             throw new Exception("Entity not found"1);
  72.         }
  73.     }
  74.     public function getTableName()
  75.     {
  76.         if($this->entity){
  77.             return $this->em->getClassMetadata($this->getClassName())->getTableName();
  78.         }else{
  79.             throw new Exception("Entity not found"1);
  80.         }
  81.     }
  82.     public function getFieldsName()
  83.     {
  84.         if($this->entity){
  85.             $reflection = new ReflectionClass($this->entity);
  86.             $entityProperties $reflection->getProperties(ReflectionProperty::IS_PRIVATE);
  87.             $fields = [];
  88.             foreach ($entityProperties as $key => $property) {
  89.                 $propName $property->getName();
  90.                 $fields[] = $propName;
  91.             }
  92.             return $fields;
  93.         }else{
  94.             throw new Exception("Entity not found"1);
  95.         }
  96.     }
  97.     public function setAllFields(array $data)
  98.     {
  99.         if($this->entity){
  100.             foreach ($data as $property => $value) {
  101.                 if (property_exists($this->entity$property)) {
  102.                     $setFunc "set" ucfirst($property);
  103.                     if(method_exists($this->entity$setFunc)){
  104.                         $this->entity->{$setFunc}($value);
  105.                     }
  106.                 }
  107.             }
  108.             return $this->entity;
  109.         }else{
  110.             throw new Exception("Entity not found"1);
  111.         }
  112.     }
  113.     public function validateEntity($groups = [])
  114.     {
  115.         $groups[] = "Default";
  116.         $errors $this->validator->validate($this->entitynull$groups);
  117.         if (count($errors) > 0) {
  118.             $fields = [];
  119.             
  120.             $className $this->getClassName(true);
  121.             foreach ($errors as $violation) {
  122.                 $fieldName $violation->getPropertyPath();
  123.                 $fieldArray explode('.'$fieldName);
  124.                 if(count($fieldArray) == 1){
  125.                     $fieldName array_shift($fieldArray);
  126.                     $fields[] = $fieldName;
  127.                 }
  128.             }
  129.             $fields array_unique($fields);
  130.             sort($fields);
  131.             return $fields;
  132.         }
  133.         return false;
  134.     }
  135. }