ConstraintValidator.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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;
  12. /**
  13. * Base class for constraint validators.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. abstract class ConstraintValidator implements ConstraintValidatorInterface
  18. {
  19. /**
  20. * Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
  21. * (if it is available) or as RFC-3339 dates ("Y-m-d H:i:s").
  22. */
  23. public const PRETTY_DATE = 1;
  24. /**
  25. * Whether to cast objects with a "__toString()" method to strings.
  26. */
  27. public const OBJECT_TO_STRING = 2;
  28. /**
  29. * @var ExecutionContextInterface
  30. */
  31. protected $context;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function initialize(ExecutionContextInterface $context)
  36. {
  37. $this->context = $context;
  38. }
  39. /**
  40. * Returns a string representation of the type of the value.
  41. *
  42. * This method should be used if you pass the type of a value as
  43. * message parameter to a constraint violation. Note that such
  44. * parameters should usually not be included in messages aimed at
  45. * non-technical people.
  46. *
  47. * @param mixed $value The value to return the type of
  48. *
  49. * @return string The type of the value
  50. */
  51. protected function formatTypeOf($value)
  52. {
  53. return get_debug_type($value);
  54. }
  55. /**
  56. * Returns a string representation of the value.
  57. *
  58. * This method returns the equivalent PHP tokens for most scalar types
  59. * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
  60. * in double quotes ("). Objects, arrays and resources are formatted as
  61. * "object", "array" and "resource". If the $format bitmask contains
  62. * the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
  63. * with the {@link \IntlDateFormatter}. If it is not available, they will be
  64. * formatted as RFC-3339 dates ("Y-m-d H:i:s").
  65. *
  66. * Be careful when passing message parameters to a constraint violation
  67. * that (may) contain objects, arrays or resources. These parameters
  68. * should only be displayed for technical users. Non-technical users
  69. * won't know what an "object", "array" or "resource" is and will be
  70. * confused by the violation message.
  71. *
  72. * @param mixed $value The value to format as string
  73. * @param int $format A bitwise combination of the format
  74. * constants in this class
  75. *
  76. * @return string The string representation of the passed value
  77. */
  78. protected function formatValue($value, int $format = 0)
  79. {
  80. if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
  81. if (class_exists(\IntlDateFormatter::class)) {
  82. $formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC');
  83. return $formatter->format(new \DateTime(
  84. $value->format('Y-m-d H:i:s.u'),
  85. new \DateTimeZone('UTC')
  86. ));
  87. }
  88. return $value->format('Y-m-d H:i:s');
  89. }
  90. if (\is_object($value)) {
  91. if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
  92. return $value->__toString();
  93. }
  94. return 'object';
  95. }
  96. if (\is_array($value)) {
  97. return 'array';
  98. }
  99. if (\is_string($value)) {
  100. return '"'.$value.'"';
  101. }
  102. if (\is_resource($value)) {
  103. return 'resource';
  104. }
  105. if (null === $value) {
  106. return 'null';
  107. }
  108. if (false === $value) {
  109. return 'false';
  110. }
  111. if (true === $value) {
  112. return 'true';
  113. }
  114. return (string) $value;
  115. }
  116. /**
  117. * Returns a string representation of a list of values.
  118. *
  119. * Each of the values is converted to a string using
  120. * {@link formatValue()}. The values are then concatenated with commas.
  121. *
  122. * @param array $values A list of values
  123. * @param int $format A bitwise combination of the format
  124. * constants in this class
  125. *
  126. * @return string The string representation of the value list
  127. *
  128. * @see formatValue()
  129. */
  130. protected function formatValues(array $values, int $format = 0)
  131. {
  132. foreach ($values as $key => $value) {
  133. $values[$key] = $this->formatValue($value, $format);
  134. }
  135. return implode(', ', $values);
  136. }
  137. }