vendor/symfony/form/Extension/DataCollector/FormDataExtractor.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\DataCollector;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\Validator\ConstraintViolationInterface;
  14. /**
  15.  * Default implementation of {@link FormDataExtractorInterface}.
  16.  *
  17.  * @author Bernhard Schussek <bschussek@gmail.com>
  18.  */
  19. class FormDataExtractor implements FormDataExtractorInterface
  20. {
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public function extractConfiguration(FormInterface $form)
  25.     {
  26.         $data = [
  27.             'id' => $this->buildId($form),
  28.             'name' => $form->getName(),
  29.             'type_class' => \get_class($form->getConfig()->getType()->getInnerType()),
  30.             'synchronized' => $form->isSynchronized(),
  31.             'passed_options' => [],
  32.             'resolved_options' => [],
  33.         ];
  34.         foreach ($form->getConfig()->getAttribute('data_collector/passed_options', []) as $option => $value) {
  35.             $data['passed_options'][$option] = $value;
  36.         }
  37.         foreach ($form->getConfig()->getOptions() as $option => $value) {
  38.             $data['resolved_options'][$option] = $value;
  39.         }
  40.         ksort($data['passed_options']);
  41.         ksort($data['resolved_options']);
  42.         return $data;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function extractDefaultData(FormInterface $form)
  48.     {
  49.         $data = [
  50.             'default_data' => [
  51.                 'norm' => $form->getNormData(),
  52.             ],
  53.             'submitted_data' => [],
  54.         ];
  55.         if ($form->getData() !== $form->getNormData()) {
  56.             $data['default_data']['model'] = $form->getData();
  57.         }
  58.         if ($form->getViewData() !== $form->getNormData()) {
  59.             $data['default_data']['view'] = $form->getViewData();
  60.         }
  61.         return $data;
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function extractSubmittedData(FormInterface $form)
  67.     {
  68.         $data = [
  69.             'submitted_data' => [
  70.                 'norm' => $form->getNormData(),
  71.             ],
  72.             'errors' => [],
  73.         ];
  74.         if ($form->getViewData() !== $form->getNormData()) {
  75.             $data['submitted_data']['view'] = $form->getViewData();
  76.         }
  77.         if ($form->getData() !== $form->getNormData()) {
  78.             $data['submitted_data']['model'] = $form->getData();
  79.         }
  80.         foreach ($form->getErrors() as $error) {
  81.             $errorData = [
  82.                 'message' => $error->getMessage(),
  83.                 'origin' => \is_object($error->getOrigin())
  84.                     ? spl_object_hash($error->getOrigin())
  85.                     : null,
  86.                 'trace' => [],
  87.             ];
  88.             $cause $error->getCause();
  89.             while (null !== $cause) {
  90.                 if ($cause instanceof ConstraintViolationInterface) {
  91.                     $errorData['trace'][] = $cause;
  92.                     $cause method_exists($cause'getCause') ? $cause->getCause() : null;
  93.                     continue;
  94.                 }
  95.                 if ($cause instanceof \Exception) {
  96.                     $errorData['trace'][] = $cause;
  97.                     $cause $cause->getPrevious();
  98.                     continue;
  99.                 }
  100.                 $errorData['trace'][] = $cause;
  101.                 break;
  102.             }
  103.             $data['errors'][] = $errorData;
  104.         }
  105.         $data['synchronized'] = $form->isSynchronized();
  106.         return $data;
  107.     }
  108.     /**
  109.      * {@inheritdoc}
  110.      */
  111.     public function extractViewVariables(FormView $view)
  112.     {
  113.         $data = [
  114.             'id' => $view->vars['id'] ?? null,
  115.             'name' => $view->vars['name'] ?? null,
  116.             'view_vars' => [],
  117.         ];
  118.         foreach ($view->vars as $varName => $value) {
  119.             $data['view_vars'][$varName] = $value;
  120.         }
  121.         ksort($data['view_vars']);
  122.         return $data;
  123.     }
  124.     /**
  125.      * Recursively builds an HTML ID for a form.
  126.      */
  127.     private function buildId(FormInterface $form): string
  128.     {
  129.         $id $form->getName();
  130.         if (null !== $form->getParent()) {
  131.             $id $this->buildId($form->getParent()).'_'.$id;
  132.         }
  133.         return $id;
  134.     }
  135. }