PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php

https://gitlab.com/Marwamimo/Crowdrise_Web
PHP | 260 lines | 122 code | 36 blank | 102 comment | 18 complexity | 2a7e63be11bd2bbd96455db26353e5ab 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\Core\ChoiceList;
  11. use Symfony\Component\Form\Exception\StringCastException;
  12. use Symfony\Component\Form\Exception\InvalidArgumentException;
  13. use Symfony\Component\PropertyAccess\PropertyPath;
  14. use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
  15. use Symfony\Component\PropertyAccess\PropertyAccess;
  16. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  17. /**
  18. * A choice list for object choices.
  19. *
  20. * Supports generation of choice labels, choice groups and choice values
  21. * by calling getters of the object (or associated objects).
  22. *
  23. * <code>
  24. * $choices = array($user1, $user2);
  25. *
  26. * // call getName() to determine the choice labels
  27. * $choiceList = new ObjectChoiceList($choices, 'name');
  28. * </code>
  29. *
  30. * @author Bernhard Schussek <bschussek@gmail.com>
  31. */
  32. class ObjectChoiceList extends ChoiceList
  33. {
  34. /**
  35. * @var PropertyAccessorInterface
  36. */
  37. private $propertyAccessor;
  38. /**
  39. * The property path used to obtain the choice label.
  40. *
  41. * @var PropertyPath
  42. */
  43. private $labelPath;
  44. /**
  45. * The property path used for object grouping.
  46. *
  47. * @var PropertyPath
  48. */
  49. private $groupPath;
  50. /**
  51. * The property path used to obtain the choice value.
  52. *
  53. * @var PropertyPath
  54. */
  55. private $valuePath;
  56. /**
  57. * Creates a new object choice list.
  58. *
  59. * @param array|\Traversable $choices The array of choices. Choices may also be given
  60. * as hierarchy of unlimited depth by creating nested
  61. * arrays. The title of the sub-hierarchy can be
  62. * stored in the array key pointing to the nested
  63. * array. The topmost level of the hierarchy may also
  64. * be a \Traversable.
  65. * @param string $labelPath A property path pointing to the property used
  66. * for the choice labels. The value is obtained
  67. * by calling the getter on the object. If the
  68. * path is NULL, the object's __toString() method
  69. * is used instead.
  70. * @param array $preferredChoices A flat array of choices that should be
  71. * presented to the user with priority.
  72. * @param string $groupPath A property path pointing to the property used
  73. * to group the choices. Only allowed if
  74. * the choices are given as flat array.
  75. * @param string $valuePath A property path pointing to the property used
  76. * for the choice values. If not given, integers
  77. * are generated instead.
  78. * @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths.
  79. */
  80. public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null)
  81. {
  82. $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
  83. $this->labelPath = null !== $labelPath ? new PropertyPath($labelPath) : null;
  84. $this->groupPath = null !== $groupPath ? new PropertyPath($groupPath) : null;
  85. $this->valuePath = null !== $valuePath ? new PropertyPath($valuePath) : null;
  86. parent::__construct($choices, array(), $preferredChoices);
  87. }
  88. /**
  89. * Initializes the list with choices.
  90. *
  91. * Safe to be called multiple times. The list is cleared on every call.
  92. *
  93. * @param array|\Traversable $choices The choices to write into the list.
  94. * @param array $labels Ignored.
  95. * @param array $preferredChoices The choices to display with priority.
  96. *
  97. * @throws InvalidArgumentException When passing a hierarchy of choices and using
  98. * the "groupPath" option at the same time.
  99. */
  100. protected function initialize($choices, array $labels, array $preferredChoices)
  101. {
  102. if (null !== $this->groupPath) {
  103. $groupedChoices = array();
  104. foreach ($choices as $i => $choice) {
  105. if (is_array($choice)) {
  106. throw new InvalidArgumentException('You should pass a plain object array (without groups) when using the "groupPath" option.');
  107. }
  108. try {
  109. $group = $this->propertyAccessor->getValue($choice, $this->groupPath);
  110. } catch (NoSuchPropertyException $e) {
  111. // Don't group items whose group property does not exist
  112. // see https://github.com/symfony/symfony/commit/d9b7abb7c7a0f28e0ce970afc5e305dce5dccddf
  113. $group = null;
  114. }
  115. if (null === $group) {
  116. $groupedChoices[$i] = $choice;
  117. } else {
  118. $groupName = (string) $group;
  119. if (!isset($groupedChoices[$groupName])) {
  120. $groupedChoices[$groupName] = array();
  121. }
  122. $groupedChoices[$groupName][$i] = $choice;
  123. }
  124. }
  125. $choices = $groupedChoices;
  126. }
  127. $labels = array();
  128. $this->extractLabels($choices, $labels);
  129. parent::initialize($choices, $labels, $preferredChoices);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getValuesForChoices(array $choices)
  135. {
  136. if (!$this->valuePath) {
  137. return parent::getValuesForChoices($choices);
  138. }
  139. // Use the value path to compare the choices
  140. $choices = $this->fixChoices($choices);
  141. $values = array();
  142. foreach ($choices as $i => $givenChoice) {
  143. // Ignore non-readable choices
  144. if (!is_object($givenChoice) && !is_array($givenChoice)) {
  145. continue;
  146. }
  147. $givenValue = (string) $this->propertyAccessor->getValue($givenChoice, $this->valuePath);
  148. foreach ($this->values as $value) {
  149. if ($value === $givenValue) {
  150. $values[$i] = $value;
  151. unset($choices[$i]);
  152. if (0 === count($choices)) {
  153. break 2;
  154. }
  155. }
  156. }
  157. }
  158. return $values;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. *
  163. * @deprecated Deprecated since version 2.4, to be removed in 3.0.
  164. */
  165. public function getIndicesForChoices(array $choices)
  166. {
  167. if (!$this->valuePath) {
  168. return parent::getIndicesForChoices($choices);
  169. }
  170. // Use the value path to compare the choices
  171. $choices = $this->fixChoices($choices);
  172. $indices = array();
  173. foreach ($choices as $i => $givenChoice) {
  174. // Ignore non-readable choices
  175. if (!is_object($givenChoice) && !is_array($givenChoice)) {
  176. continue;
  177. }
  178. $givenValue = (string) $this->propertyAccessor->getValue($givenChoice, $this->valuePath);
  179. foreach ($this->values as $j => $value) {
  180. if ($value === $givenValue) {
  181. $indices[$i] = $j;
  182. unset($choices[$i]);
  183. if (0 === count($choices)) {
  184. break 2;
  185. }
  186. }
  187. }
  188. }
  189. return $indices;
  190. }
  191. /**
  192. * Creates a new unique value for this choice.
  193. *
  194. * If a property path for the value was given at object creation,
  195. * the getter behind that path is now called to obtain a new value.
  196. * Otherwise a new integer is generated.
  197. *
  198. * @param mixed $choice The choice to create a value for
  199. *
  200. * @return int|string A unique value without character limitations.
  201. */
  202. protected function createValue($choice)
  203. {
  204. if ($this->valuePath) {
  205. return (string) $this->propertyAccessor->getValue($choice, $this->valuePath);
  206. }
  207. return parent::createValue($choice);
  208. }
  209. private function extractLabels($choices, array &$labels)
  210. {
  211. foreach ($choices as $i => $choice) {
  212. if (is_array($choice)) {
  213. $labels[$i] = array();
  214. $this->extractLabels($choice, $labels[$i]);
  215. } elseif ($this->labelPath) {
  216. $labels[$i] = $this->propertyAccessor->getValue($choice, $this->labelPath);
  217. } elseif (method_exists($choice, '__toString')) {
  218. $labels[$i] = (string) $choice;
  219. } else {
  220. throw new StringCastException(sprintf('A "__toString()" method was not found on the objects of type "%s" passed to the choice field. To read a custom getter instead, set the argument $labelPath to the desired property path.', get_class($choice)));
  221. }
  222. }
  223. }
  224. }