ConstraintValidator.php 7.0 KB

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