ConstraintValidator.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\Validator;
  11. use Symfony\Component\Validator\Context\ExecutionContextInterface as ExecutionContextInterface2Dot5;
  12. use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
  13. use Symfony\Component\Validator\Violation\LegacyConstraintViolationBuilder;
  14. /**
  15. * Base class for constraint validators.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. */
  19. abstract class ConstraintValidator implements ConstraintValidatorInterface
  20. {
  21. /**
  22. * Whether to format {@link \DateTime} objects as RFC-3339 dates
  23. * ("Y-m-d H:i:s").
  24. */
  25. const PRETTY_DATE = 1;
  26. /**
  27. * Whether to cast objects with a "__toString()" method to strings.
  28. */
  29. const OBJECT_TO_STRING = 2;
  30. /**
  31. * @var ExecutionContextInterface2Dot5
  32. */
  33. protected $context;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function initialize(ExecutionContextInterface $context)
  38. {
  39. $this->context = $context;
  40. }
  41. /**
  42. * Wrapper for {@link ExecutionContextInterface::buildViolation} that
  43. * supports the 2.4 context API.
  44. *
  45. * @param string $message The violation message
  46. * @param array $parameters The message parameters
  47. *
  48. * @return ConstraintViolationBuilderInterface The violation builder
  49. *
  50. * @deprecated since version 2.5, to be removed in 3.0.
  51. */
  52. protected function buildViolation($message, array $parameters = array())
  53. {
  54. @trigger_error('The '.__METHOD__.' is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  55. if ($this->context instanceof ExecutionContextInterface2Dot5) {
  56. return $this->context->buildViolation($message, $parameters);
  57. }
  58. return new LegacyConstraintViolationBuilder($this->context, $message, $parameters);
  59. }
  60. /**
  61. * Wrapper for {@link ExecutionContextInterface::buildViolation} that
  62. * supports the 2.4 context API.
  63. *
  64. * @param ExecutionContextInterface $context The context to use
  65. * @param string $message The violation message
  66. * @param array $parameters The message parameters
  67. *
  68. * @return ConstraintViolationBuilderInterface The violation builder
  69. *
  70. * @deprecated since version 2.5, to be removed in 3.0.
  71. */
  72. protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array())
  73. {
  74. @trigger_error('The '.__METHOD__.' is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  75. if ($context instanceof ExecutionContextInterface2Dot5) {
  76. return $context->buildViolation($message, $parameters);
  77. }
  78. return new LegacyConstraintViolationBuilder($context, $message, $parameters);
  79. }
  80. /**
  81. * Returns a string representation of the type of the value.
  82. *
  83. * This method should be used if you pass the type of a value as
  84. * message parameter to a constraint violation. Note that such
  85. * parameters should usually not be included in messages aimed at
  86. * non-technical people.
  87. *
  88. * @param mixed $value The value to return the type of
  89. *
  90. * @return string The type of the value
  91. */
  92. protected function formatTypeOf($value)
  93. {
  94. return \is_object($value) ? \get_class($value) : \gettype($value);
  95. }
  96. /**
  97. * Returns a string representation of the value.
  98. *
  99. * This method returns the equivalent PHP tokens for most scalar types
  100. * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
  101. * in double quotes ("). Objects, arrays and resources are formatted as
  102. * "object", "array" and "resource". If the $format bitmask contains
  103. * the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
  104. * as RFC-3339 dates ("Y-m-d H:i:s").
  105. *
  106. * Be careful when passing message parameters to a constraint violation
  107. * that (may) contain objects, arrays or resources. These parameters
  108. * should only be displayed for technical users. Non-technical users
  109. * won't know what an "object", "array" or "resource" is and will be
  110. * confused by the violation message.
  111. *
  112. * @param mixed $value The value to format as string
  113. * @param int $format A bitwise combination of the format
  114. * constants in this class
  115. *
  116. * @return string The string representation of the passed value
  117. */
  118. protected function formatValue($value, $format = 0)
  119. {
  120. $isDateTime = $value instanceof \DateTime || $value instanceof \DateTimeInterface;
  121. if (($format & self::PRETTY_DATE) && $isDateTime) {
  122. if (class_exists('IntlDateFormatter')) {
  123. $locale = \Locale::getDefault();
  124. $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
  125. // neither the native nor the stub IntlDateFormatter support
  126. // DateTimeImmutable as of yet
  127. if (!$value instanceof \DateTime) {
  128. $value = new \DateTime(
  129. $value->format('Y-m-d H:i:s.u e'),
  130. $value->getTimezone()
  131. );
  132. }
  133. return $formatter->format($value);
  134. }
  135. return $value->format('Y-m-d H:i:s');
  136. }
  137. if (\is_object($value)) {
  138. if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
  139. return $value->__toString();
  140. }
  141. return 'object';
  142. }
  143. if (\is_array($value)) {
  144. return 'array';
  145. }
  146. if (\is_string($value)) {
  147. return '"'.$value.'"';
  148. }
  149. if (\is_resource($value)) {
  150. return 'resource';
  151. }
  152. if (null === $value) {
  153. return 'null';
  154. }
  155. if (false === $value) {
  156. return 'false';
  157. }
  158. if (true === $value) {
  159. return 'true';
  160. }
  161. return (string) $value;
  162. }
  163. /**
  164. * Returns a string representation of a list of values.
  165. *
  166. * Each of the values is converted to a string using
  167. * {@link formatValue()}. The values are then concatenated with commas.
  168. *
  169. * @param array $values A list of values
  170. * @param int $format A bitwise combination of the format
  171. * constants in this class
  172. *
  173. * @return string The string representation of the value list
  174. *
  175. * @see formatValue()
  176. */
  177. protected function formatValues(array $values, $format = 0)
  178. {
  179. foreach ($values as $key => $value) {
  180. $values[$key] = $this->formatValue($value, $format);
  181. }
  182. return implode(', ', $values);
  183. }
  184. }