/vendor_full/symfony/src/Symfony/Component/Form/FieldFactory/ValidatorFieldFactoryGuesser.php

https://github.com/l3l0/BehatExamples · PHP · 310 lines · 237 code · 18 blank · 55 comment · 6 complexity · 92b7ebf2cb4cde9147779f11a82df0ab MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\FieldFactory;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
  13. /**
  14. * Guesses form fields from the metadata of the a Validator class
  15. *
  16. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  17. */
  18. class ValidatorFieldFactoryGuesser implements FieldFactoryGuesserInterface
  19. {
  20. /**
  21. * Constructor
  22. *
  23. * @param ClassMetadataFactoryInterface $metadataFactory
  24. */
  25. public function __construct(ClassMetadataFactoryInterface $metadataFactory)
  26. {
  27. $this->metadataFactory = $metadataFactory;
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. public function guessClass($class, $property)
  33. {
  34. $guesser = $this;
  35. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  36. return $guesser->guessClassForConstraint($constraint);
  37. });
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. public function guessRequired($class, $property)
  43. {
  44. $guesser = $this;
  45. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  46. return $guesser->guessRequiredForConstraint($constraint);
  47. });
  48. }
  49. /**
  50. * @inheritDoc
  51. */
  52. public function guessMaxLength($class, $property)
  53. {
  54. $guesser = $this;
  55. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  56. return $guesser->guessMaxLengthForConstraint($constraint);
  57. });
  58. }
  59. /**
  60. * Iterates over the constraints of a property, executes a constraints on
  61. * them and returns the best guess
  62. *
  63. * @param string $class The class to read the constraints from
  64. * @param string $property The property for which to find constraints
  65. * @param \Closure $guessForConstraint The closure that returns a guess
  66. * for a given constraint
  67. * @return FieldFactoryGuess The guessed value with the highest confidence
  68. */
  69. protected function guess($class, $property, \Closure $guessForConstraint)
  70. {
  71. $guesses = array();
  72. $classMetadata = $this->metadataFactory->getClassMetadata($class);
  73. if ($classMetadata->hasMemberMetadatas($property)) {
  74. $memberMetadatas = $classMetadata->getMemberMetadatas($property);
  75. foreach ($memberMetadatas as $memberMetadata) {
  76. $constraints = $memberMetadata->getConstraints();
  77. foreach ($constraints as $constraint) {
  78. if ($guess = $guessForConstraint($constraint)) {
  79. $guesses[] = $guess;
  80. }
  81. }
  82. }
  83. }
  84. return FieldFactoryGuess::getBestGuess($guesses);
  85. }
  86. /**
  87. * Guesses a field class name for a given constraint
  88. *
  89. * @param Constraint $constraint The constraint to guess for
  90. * @return FieldFactoryClassGuess The guessed field class and options
  91. */
  92. public function guessClassForConstraint(Constraint $constraint)
  93. {
  94. switch (get_class($constraint)) {
  95. case 'Symfony\Component\Validator\Constraints\AssertType':
  96. switch ($constraint->type) {
  97. case 'boolean':
  98. case 'bool':
  99. return new FieldFactoryClassGuess(
  100. 'Symfony\Component\Form\CheckboxField',
  101. array(),
  102. FieldFactoryGuess::MEDIUM_CONFIDENCE
  103. );
  104. case 'double':
  105. case 'float':
  106. case 'numeric':
  107. case 'real':
  108. return new FieldFactoryClassGuess(
  109. 'Symfony\Component\Form\NumberField',
  110. array(),
  111. FieldFactoryGuess::MEDIUM_CONFIDENCE
  112. );
  113. case 'integer':
  114. case 'int':
  115. case 'long':
  116. return new FieldFactoryClassGuess(
  117. 'Symfony\Component\Form\IntegerField',
  118. array(),
  119. FieldFactoryGuess::MEDIUM_CONFIDENCE
  120. );
  121. case 'string':
  122. return new FieldFactoryClassGuess(
  123. 'Symfony\Component\Form\TextField',
  124. array(),
  125. FieldFactoryGuess::LOW_CONFIDENCE
  126. );
  127. case '\DateTime':
  128. return new FieldFactoryClassGuess(
  129. 'Symfony\Component\Form\DateField',
  130. array(),
  131. FieldFactoryGuess::MEDIUM_CONFIDENCE
  132. );
  133. }
  134. break;
  135. case 'Symfony\Component\Validator\Constraints\Choice':
  136. return new FieldFactoryClassGuess(
  137. 'Symfony\Component\Form\ChoiceField',
  138. array('choices' => $constraint->choices),
  139. FieldFactoryGuess::HIGH_CONFIDENCE
  140. );
  141. case 'Symfony\Component\Validator\Constraints\Country':
  142. return new FieldFactoryClassGuess(
  143. 'Symfony\Component\Form\CountryField',
  144. array(),
  145. FieldFactoryGuess::HIGH_CONFIDENCE
  146. );
  147. case 'Symfony\Component\Validator\Constraints\Date':
  148. return new FieldFactoryClassGuess(
  149. 'Symfony\Component\Form\DateField',
  150. array('type' => 'string'),
  151. FieldFactoryGuess::HIGH_CONFIDENCE
  152. );
  153. case 'Symfony\Component\Validator\Constraints\DateTime':
  154. return new FieldFactoryClassGuess(
  155. 'Symfony\Component\Form\DateTimeField',
  156. array('type' => 'string'),
  157. FieldFactoryGuess::HIGH_CONFIDENCE
  158. );
  159. case 'Symfony\Component\Validator\Constraints\Email':
  160. return new FieldFactoryClassGuess(
  161. 'Symfony\Component\Form\TextField',
  162. array(),
  163. FieldFactoryGuess::HIGH_CONFIDENCE
  164. );
  165. case 'Symfony\Component\Validator\Constraints\File':
  166. return new FieldFactoryClassGuess(
  167. 'Symfony\Component\Form\FileField',
  168. array(),
  169. FieldFactoryGuess::HIGH_CONFIDENCE
  170. );
  171. case 'Symfony\Component\Validator\Constraints\Image':
  172. return new FieldFactoryClassGuess(
  173. 'Symfony\Component\Form\FileField',
  174. array(),
  175. FieldFactoryGuess::HIGH_CONFIDENCE
  176. );
  177. case 'Symfony\Component\Validator\Constraints\Ip':
  178. return new FieldFactoryClassGuess(
  179. 'Symfony\Component\Form\TextField',
  180. array(),
  181. FieldFactoryGuess::MEDIUM_CONFIDENCE
  182. );
  183. case 'Symfony\Component\Validator\Constraints\Language':
  184. return new FieldFactoryClassGuess(
  185. 'Symfony\Component\Form\LanguageField',
  186. array(),
  187. FieldFactoryGuess::HIGH_CONFIDENCE
  188. );
  189. case 'Symfony\Component\Validator\Constraints\Locale':
  190. return new FieldFactoryClassGuess(
  191. 'Symfony\Component\Form\LocaleField',
  192. array(),
  193. FieldFactoryGuess::HIGH_CONFIDENCE
  194. );
  195. case 'Symfony\Component\Validator\Constraints\Max':
  196. return new FieldFactoryClassGuess(
  197. 'Symfony\Component\Form\NumberField',
  198. array(),
  199. FieldFactoryGuess::LOW_CONFIDENCE
  200. );
  201. case 'Symfony\Component\Validator\Constraints\MaxLength':
  202. return new FieldFactoryClassGuess(
  203. 'Symfony\Component\Form\TextField',
  204. array(),
  205. FieldFactoryGuess::LOW_CONFIDENCE
  206. );
  207. case 'Symfony\Component\Validator\Constraints\Min':
  208. return new FieldFactoryClassGuess(
  209. 'Symfony\Component\Form\NumberField',
  210. array(),
  211. FieldFactoryGuess::LOW_CONFIDENCE
  212. );
  213. case 'Symfony\Component\Validator\Constraints\MinLength':
  214. return new FieldFactoryClassGuess(
  215. 'Symfony\Component\Form\TextField',
  216. array(),
  217. FieldFactoryGuess::LOW_CONFIDENCE
  218. );
  219. case 'Symfony\Component\Validator\Constraints\Regex':
  220. return new FieldFactoryClassGuess(
  221. 'Symfony\Component\Form\TextField',
  222. array(),
  223. FieldFactoryGuess::LOW_CONFIDENCE
  224. );
  225. case 'Symfony\Component\Validator\Constraints\Time':
  226. return new FieldFactoryClassGuess(
  227. 'Symfony\Component\Form\TimeField',
  228. array('type' => 'string'),
  229. FieldFactoryGuess::HIGH_CONFIDENCE
  230. );
  231. case 'Symfony\Component\Validator\Constraints\Url':
  232. return new FieldFactoryClassGuess(
  233. 'Symfony\Component\Form\UrlField',
  234. array(),
  235. FieldFactoryGuess::HIGH_CONFIDENCE
  236. );
  237. default:
  238. return new FieldFactoryClassGuess(
  239. 'Symfony\Component\Form\TextField',
  240. array(),
  241. FieldFactoryGuess::LOW_CONFIDENCE
  242. );
  243. }
  244. }
  245. /**
  246. * Guesses whether a field is required based on the given constraint
  247. *
  248. * @param Constraint $constraint The constraint to guess for
  249. * @return FieldFactoryGuess The guess whether the field is required
  250. */
  251. public function guessRequiredForConstraint(Constraint $constraint)
  252. {
  253. switch (get_class($constraint)) {
  254. case 'Symfony\Component\Validator\Constraints\NotNull':
  255. return new FieldFactoryGuess(
  256. true,
  257. FieldFactoryGuess::HIGH_CONFIDENCE
  258. );
  259. case 'Symfony\Component\Validator\Constraints\NotBlank':
  260. return new FieldFactoryGuess(
  261. true,
  262. FieldFactoryGuess::HIGH_CONFIDENCE
  263. );
  264. default:
  265. return new FieldFactoryGuess(
  266. false,
  267. FieldFactoryGuess::LOW_CONFIDENCE
  268. );
  269. }
  270. }
  271. /**
  272. * Guesses a field's maximum length based on the given constraint
  273. *
  274. * @param Constraint $constraint The constraint to guess for
  275. * @return FieldFactoryGuess The guess for the maximum length
  276. */
  277. public function guessMaxLengthForConstraint(Constraint $constraint)
  278. {
  279. switch (get_class($constraint)) {
  280. case 'Symfony\Component\Validator\Constraints\MaxLength':
  281. return new FieldFactoryGuess(
  282. $constraint->limit,
  283. FieldFactoryGuess::HIGH_CONFIDENCE
  284. );
  285. case 'Symfony\Component\Validator\Constraints\Max':
  286. return new FieldFactoryGuess(
  287. strlen((string)$constraint->limit),
  288. FieldFactoryGuess::HIGH_CONFIDENCE
  289. );
  290. }
  291. }
  292. }