PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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