PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/DataCollector/FormDataExtractor.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS
PHP | 204 lines | 124 code | 39 blank | 41 comment | 10 complexity | 0986fcfd4c597f20705f9db36874d44e MD5 | raw file
  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\HttpKernel\DataCollector\Util\ValueExporter;
  14. use Symfony\Component\Validator\ConstraintViolationInterface;
  15. /**
  16. * Default implementation of {@link FormDataExtractorInterface}.
  17. *
  18. * @since 2.4
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class FormDataExtractor implements FormDataExtractorInterface
  22. {
  23. /**
  24. * @var ValueExporter
  25. */
  26. private $valueExporter;
  27. /**
  28. * Constructs a new data extractor.
  29. */
  30. public function __construct(ValueExporter $valueExporter = null)
  31. {
  32. $this->valueExporter = $valueExporter ?: new ValueExporter();
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function extractConfiguration(FormInterface $form)
  38. {
  39. $data = array(
  40. 'id' => $this->buildId($form),
  41. 'name' => $form->getName(),
  42. 'type' => $form->getConfig()->getType()->getName(),
  43. 'type_class' => get_class($form->getConfig()->getType()->getInnerType()),
  44. 'synchronized' => $this->valueExporter->exportValue($form->isSynchronized()),
  45. 'passed_options' => array(),
  46. 'resolved_options' => array(),
  47. );
  48. foreach ($form->getConfig()->getAttribute('data_collector/passed_options', array()) as $option => $value) {
  49. $data['passed_options'][$option] = $this->valueExporter->exportValue($value);
  50. }
  51. foreach ($form->getConfig()->getOptions() as $option => $value) {
  52. $data['resolved_options'][$option] = $this->valueExporter->exportValue($value);
  53. }
  54. ksort($data['passed_options']);
  55. ksort($data['resolved_options']);
  56. return $data;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function extractDefaultData(FormInterface $form)
  62. {
  63. $data = array(
  64. 'default_data' => array(
  65. 'norm' => $this->valueExporter->exportValue($form->getNormData()),
  66. ),
  67. 'submitted_data' => array(),
  68. );
  69. if ($form->getData() !== $form->getNormData()) {
  70. $data['default_data']['model'] = $this->valueExporter->exportValue($form->getData());
  71. }
  72. if ($form->getViewData() !== $form->getNormData()) {
  73. $data['default_data']['view'] = $this->valueExporter->exportValue($form->getViewData());
  74. }
  75. return $data;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function extractSubmittedData(FormInterface $form)
  81. {
  82. $data = array(
  83. 'submitted_data' => array(
  84. 'norm' => $this->valueExporter->exportValue($form->getNormData()),
  85. ),
  86. 'errors' => array(),
  87. );
  88. if ($form->getViewData() !== $form->getNormData()) {
  89. $data['submitted_data']['view'] = $this->valueExporter->exportValue($form->getViewData());
  90. }
  91. if ($form->getData() !== $form->getNormData()) {
  92. $data['submitted_data']['model'] = $this->valueExporter->exportValue($form->getData());
  93. }
  94. foreach ($form->getErrors() as $error) {
  95. $errorData = array(
  96. 'message' => $error->getMessage(),
  97. 'origin' => is_object($error->getOrigin())
  98. ? spl_object_hash($error->getOrigin())
  99. : null,
  100. 'trace' => array(),
  101. );
  102. $cause = $error->getCause();
  103. while (null !== $cause) {
  104. if ($cause instanceof ConstraintViolationInterface) {
  105. $errorData['trace'][] = array(
  106. 'class' => $this->valueExporter->exportValue(get_class($cause)),
  107. 'root' => $this->valueExporter->exportValue($cause->getRoot()),
  108. 'path' => $this->valueExporter->exportValue($cause->getPropertyPath()),
  109. 'value' => $this->valueExporter->exportValue($cause->getInvalidValue()),
  110. );
  111. $cause = method_exists($cause, 'getCause') ? $cause->getCause() : null;
  112. continue;
  113. }
  114. if ($cause instanceof \Exception) {
  115. $errorData['trace'][] = array(
  116. 'class' => $this->valueExporter->exportValue(get_class($cause)),
  117. 'message' => $this->valueExporter->exportValue($cause->getMessage()),
  118. );
  119. $cause = $cause->getPrevious();
  120. continue;
  121. }
  122. $errorData['trace'][] = $cause;
  123. break;
  124. }
  125. $data['errors'][] = $errorData;
  126. }
  127. $data['synchronized'] = $this->valueExporter->exportValue($form->isSynchronized());
  128. return $data;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function extractViewVariables(FormView $view)
  134. {
  135. $data = array();
  136. // Set the ID in case no FormInterface object was collected for this
  137. // view
  138. if (!isset($data['id'])) {
  139. $data['id'] = isset($view->vars['id']) ? $view->vars['id'] : null;
  140. }
  141. if (!isset($data['name'])) {
  142. $data['name'] = isset($view->vars['name']) ? $view->vars['name'] : null;
  143. }
  144. foreach ($view->vars as $varName => $value) {
  145. $data['view_vars'][$varName] = $this->valueExporter->exportValue($value);
  146. }
  147. ksort($data['view_vars']);
  148. return $data;
  149. }
  150. /**
  151. * Recursively builds an HTML ID for a form.
  152. *
  153. * @param FormInterface $form The form
  154. *
  155. * @return string The HTML ID
  156. */
  157. private function buildId(FormInterface $form)
  158. {
  159. $id = $form->getName();
  160. if (null !== $form->getParent()) {
  161. $id = $this->buildId($form->getParent()).'_'.$id;
  162. }
  163. return $id;
  164. }
  165. }