/vendor/symfony/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php

https://github.com/israelnoguera/parejas · PHP · 364 lines · 284 code · 22 blank · 58 comment · 7 complexity · 5c499b8e48a982e840a573b1e626345f 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\Validator;
  11. use Symfony\Component\Form\FormTypeGuesserInterface;
  12. use Symfony\Component\Form\Guess\Guess;
  13. use Symfony\Component\Form\Guess\TypeGuess;
  14. use Symfony\Component\Form\Guess\ValueGuess;
  15. use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
  16. use Symfony\Component\Validator\Constraint;
  17. class ValidatorTypeGuesser implements FormTypeGuesserInterface
  18. {
  19. private $metadataFactory;
  20. public function __construct(ClassMetadataFactoryInterface $metadataFactory)
  21. {
  22. $this->metadataFactory = $metadataFactory;
  23. }
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function guessType($class, $property)
  28. {
  29. $guesser = $this;
  30. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  31. return $guesser->guessTypeForConstraint($constraint);
  32. });
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function guessRequired($class, $property)
  38. {
  39. $guesser = $this;
  40. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  41. return $guesser->guessRequiredForConstraint($constraint);
  42. });
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function guessMaxLength($class, $property)
  48. {
  49. $guesser = $this;
  50. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  51. return $guesser->guessMaxLengthForConstraint($constraint);
  52. });
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function guessMinLength($class, $property)
  58. {
  59. $guesser = $this;
  60. return $this->guess($class, $property, function (Constraint $constraint) use ($guesser) {
  61. return $guesser->guessMinLengthForConstraint($constraint);
  62. });
  63. }
  64. /**
  65. * Guesses a field class name for a given constraint
  66. *
  67. * @param Constraint $constraint The constraint to guess for
  68. *
  69. * @return TypeGuess The guessed field class and options
  70. */
  71. public function guessTypeForConstraint(Constraint $constraint)
  72. {
  73. switch (get_class($constraint)) {
  74. case 'Symfony\Component\Validator\Constraints\Type':
  75. switch ($constraint->type) {
  76. case 'boolean':
  77. case 'bool':
  78. return new TypeGuess(
  79. 'checkbox',
  80. array(),
  81. Guess::MEDIUM_CONFIDENCE
  82. );
  83. case 'double':
  84. case 'float':
  85. case 'numeric':
  86. case 'real':
  87. return new TypeGuess(
  88. 'number',
  89. array(),
  90. Guess::MEDIUM_CONFIDENCE
  91. );
  92. case 'integer':
  93. case 'int':
  94. case 'long':
  95. return new TypeGuess(
  96. 'integer',
  97. array(),
  98. Guess::MEDIUM_CONFIDENCE
  99. );
  100. case 'string':
  101. return new TypeGuess(
  102. 'text',
  103. array(),
  104. Guess::LOW_CONFIDENCE
  105. );
  106. case '\DateTime':
  107. return new TypeGuess(
  108. 'date',
  109. array(),
  110. Guess::MEDIUM_CONFIDENCE
  111. );
  112. }
  113. break;
  114. case 'Symfony\Component\Validator\Constraints\Country':
  115. return new TypeGuess(
  116. 'country',
  117. array(),
  118. Guess::HIGH_CONFIDENCE
  119. );
  120. case 'Symfony\Component\Validator\Constraints\Date':
  121. return new TypeGuess(
  122. 'date',
  123. array('type' => 'string'),
  124. Guess::HIGH_CONFIDENCE
  125. );
  126. case 'Symfony\Component\Validator\Constraints\DateTime':
  127. return new TypeGuess(
  128. 'datetime',
  129. array('type' => 'string'),
  130. Guess::HIGH_CONFIDENCE
  131. );
  132. case 'Symfony\Component\Validator\Constraints\Email':
  133. return new TypeGuess(
  134. 'email',
  135. array(),
  136. Guess::HIGH_CONFIDENCE
  137. );
  138. case 'Symfony\Component\Validator\Constraints\File':
  139. return new TypeGuess(
  140. 'file',
  141. array(),
  142. Guess::HIGH_CONFIDENCE
  143. );
  144. case 'Symfony\Component\Validator\Constraints\Image':
  145. return new TypeGuess(
  146. 'file',
  147. array(),
  148. Guess::HIGH_CONFIDENCE
  149. );
  150. case 'Symfony\Component\Validator\Constraints\Ip':
  151. return new TypeGuess(
  152. 'text',
  153. array(),
  154. Guess::MEDIUM_CONFIDENCE
  155. );
  156. case 'Symfony\Component\Validator\Constraints\Language':
  157. return new TypeGuess(
  158. 'language',
  159. array(),
  160. Guess::HIGH_CONFIDENCE
  161. );
  162. case 'Symfony\Component\Validator\Constraints\Locale':
  163. return new TypeGuess(
  164. 'locale',
  165. array(),
  166. Guess::HIGH_CONFIDENCE
  167. );
  168. case 'Symfony\Component\Validator\Constraints\Max':
  169. return new TypeGuess(
  170. 'number',
  171. array(),
  172. Guess::LOW_CONFIDENCE
  173. );
  174. case 'Symfony\Component\Validator\Constraints\MaxLength':
  175. return new TypeGuess(
  176. 'text',
  177. array(),
  178. Guess::LOW_CONFIDENCE
  179. );
  180. case 'Symfony\Component\Validator\Constraints\Min':
  181. return new TypeGuess(
  182. 'number',
  183. array(),
  184. Guess::LOW_CONFIDENCE
  185. );
  186. case 'Symfony\Component\Validator\Constraints\MinLength':
  187. return new TypeGuess(
  188. 'text',
  189. array(),
  190. Guess::LOW_CONFIDENCE
  191. );
  192. case 'Symfony\Component\Validator\Constraints\Regex':
  193. return new TypeGuess(
  194. 'text',
  195. array(),
  196. Guess::LOW_CONFIDENCE
  197. );
  198. case 'Symfony\Component\Validator\Constraints\Time':
  199. return new TypeGuess(
  200. 'time',
  201. array('type' => 'string'),
  202. Guess::HIGH_CONFIDENCE
  203. );
  204. case 'Symfony\Component\Validator\Constraints\Url':
  205. return new TypeGuess(
  206. 'url',
  207. array(),
  208. Guess::HIGH_CONFIDENCE
  209. );
  210. case 'Symfony\Component\Validator\Constraints\Size':
  211. return new TypeGuess(
  212. 'number',
  213. array(),
  214. Guess::LOW_CONFIDENCE
  215. );
  216. case 'Symfony\Component\Validator\Constraints\SizeLength':
  217. return new TypeGuess(
  218. 'text',
  219. array(),
  220. Guess::LOW_CONFIDENCE
  221. );
  222. }
  223. }
  224. /**
  225. * Guesses whether a field is required based on the given constraint
  226. *
  227. * @param Constraint $constraint The constraint to guess for
  228. *
  229. * @return Guess The guess whether the field is required
  230. */
  231. public function guessRequiredForConstraint(Constraint $constraint)
  232. {
  233. switch (get_class($constraint)) {
  234. case 'Symfony\Component\Validator\Constraints\NotNull':
  235. return new ValueGuess(
  236. true,
  237. Guess::HIGH_CONFIDENCE
  238. );
  239. case 'Symfony\Component\Validator\Constraints\NotBlank':
  240. return new ValueGuess(
  241. true,
  242. Guess::HIGH_CONFIDENCE
  243. );
  244. default:
  245. return new ValueGuess(
  246. false,
  247. Guess::LOW_CONFIDENCE
  248. );
  249. }
  250. }
  251. /**
  252. * Guesses a field's maximum length based on the given constraint
  253. *
  254. * @param Constraint $constraint The constraint to guess for
  255. *
  256. * @return Guess The guess for the maximum length
  257. */
  258. public function guessMaxLengthForConstraint(Constraint $constraint)
  259. {
  260. switch (get_class($constraint)) {
  261. case 'Symfony\Component\Validator\Constraints\MaxLength':
  262. return new ValueGuess(
  263. $constraint->limit,
  264. Guess::HIGH_CONFIDENCE
  265. );
  266. case 'Symfony\Component\Validator\Constraints\Max':
  267. return new ValueGuess(
  268. strlen((string) $constraint->limit),
  269. Guess::HIGH_CONFIDENCE
  270. );
  271. case 'Symfony\Component\Validator\Constraints\SizeLength':
  272. return new ValueGuess(
  273. $constraint->max,
  274. Guess::HIGH_CONFIDENCE
  275. );
  276. case 'Symfony\Component\Validator\Constraints\Size':
  277. return new ValueGuess(
  278. strlen((string) $constraint->max),
  279. Guess::HIGH_CONFIDENCE
  280. );
  281. }
  282. }
  283. /**
  284. * Guesses a field's minimum length based on the given constraint
  285. *
  286. * @param Constraint $constraint The constraint to guess for
  287. *
  288. * @return Guess The guess for the minimum length
  289. */
  290. public function guessMinLengthForConstraint(Constraint $constraint)
  291. {
  292. switch (get_class($constraint)) {
  293. case 'Symfony\Component\Validator\Constraints\MinLength':
  294. return new ValueGuess(
  295. $constraint->limit,
  296. Guess::HIGH_CONFIDENCE
  297. );
  298. case 'Symfony\Component\Validator\Constraints\Min':
  299. return new ValueGuess(
  300. strlen((string) $constraint->limit),
  301. Guess::HIGH_CONFIDENCE
  302. );
  303. case 'Symfony\Component\Validator\Constraints\SizeLength':
  304. return new ValueGuess(
  305. $constraint->min,
  306. Guess::HIGH_CONFIDENCE
  307. );
  308. case 'Symfony\Component\Validator\Constraints\Size':
  309. return new ValueGuess(
  310. strlen((string) $constraint->min),
  311. Guess::HIGH_CONFIDENCE
  312. );
  313. }
  314. }
  315. /**
  316. * Iterates over the constraints of a property, executes a constraints on
  317. * them and returns the best guess
  318. *
  319. * @param string $class The class to read the constraints from
  320. * @param string $property The property for which to find constraints
  321. * @param \Closure $guessForConstraint The closure that returns a guess
  322. * for a given constraint
  323. * @return Guess The guessed value with the highest confidence
  324. */
  325. protected function guess($class, $property, \Closure $guessForConstraint)
  326. {
  327. $guesses = array();
  328. $classMetadata = $this->metadataFactory->getClassMetadata($class);
  329. if ($classMetadata->hasMemberMetadatas($property)) {
  330. $memberMetadatas = $classMetadata->getMemberMetadatas($property);
  331. foreach ($memberMetadatas as $memberMetadata) {
  332. $constraints = $memberMetadata->getConstraints();
  333. foreach ($constraints as $constraint) {
  334. if ($guess = $guessForConstraint($constraint)) {
  335. $guesses[] = $guess;
  336. }
  337. }
  338. }
  339. }
  340. return Guess::getBestGuess($guesses);
  341. }
  342. }