src/Validator/ColumnValidator.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  4.  * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
  5.  */
  6. namespace App\Validator;
  7. use App\Repository\DocumentUploadValueRepository;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormError;
  11. use App\Entity\DocumentUpload;
  12. /**
  13.  * Description of ColumnValidator
  14.  *
  15.  * @author programmer
  16.  */
  17. class ColumnValidator extends AbstractExcelValidator 
  18. {   
  19.     private DocumentUploadValueRepository $documentUploadValueRepository;
  20.     
  21.     public function __construct(DocumentUploadValueRepository $documentUploadValueRepository
  22.     {
  23.         $this->documentUploadValueRepository $documentUploadValueRepository;
  24.     }
  25.     
  26.     public function validate(FormEvent $evt): void 
  27.     {
  28.         $entity $evt->getData();
  29.         if (!$entity instanceof DocumentUpload) {
  30.             return;
  31.         }
  32.         
  33.         $headers $entity->getDocument()->getDocumentHeaders();
  34.         $excel IOFactory::load($entity->getAttachment());
  35.         $sheet $excel->getActiveSheet();
  36.         $typeValidate = function (string $type$valueint $row):bool {
  37.             $validate "is_" $type;
  38.             if (false === call_user_func($validate$value)) {
  39.                 throw new \Exception(
  40.                     sprintf("invalid type: %s, %s expected at row %s"$value$type$row)
  41.                 );
  42.             }
  43.             
  44.             return true;
  45.         };
  46.         
  47.         for ($i $entity->getDocument()->getDataStartAt(); $i $sheet->getHighestDataRow(); $i++) {
  48.             foreach ($headers as $header) {
  49.                 $cell $header->getColumnName() . $i;
  50.                 $value $sheet->getCell($cell)->getValue();
  51.                 
  52.                 if (null === $value) {
  53.                     continue;
  54.                 }
  55.                 try {
  56.                     $typeValidate($header->getTypeData(), $value$i);
  57.                     $entity->addDocumentUploadValue(
  58.                         $this->documentUploadValueRepository->findOrCreate(
  59.                             $value
  60.                             $i,
  61.                             $header
  62.                             $entity
  63.                         )
  64.                     );
  65.                 } catch (\Exception $ex) {
  66.                     $evt->getForm()->get("attachment")
  67.                             ->addError(
  68.                                 new FormError($ex->getMessage())
  69.                             );
  70.                     return;
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }