Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator; Chris@0: Chris@0: use Symfony\Component\Validator\Context\ExecutionContextInterface; Chris@0: Chris@0: /** Chris@0: * Base class for constraint validators. Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: abstract class ConstraintValidator implements ConstraintValidatorInterface Chris@0: { Chris@0: /** Chris@0: * Whether to format {@link \DateTime} objects as RFC-3339 dates Chris@0: * ("Y-m-d H:i:s"). Chris@0: */ Chris@0: const PRETTY_DATE = 1; Chris@0: Chris@0: /** Chris@0: * Whether to cast objects with a "__toString()" method to strings. Chris@0: */ Chris@0: const OBJECT_TO_STRING = 2; Chris@0: Chris@0: /** Chris@0: * @var ExecutionContextInterface Chris@0: */ Chris@0: protected $context; Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function initialize(ExecutionContextInterface $context) Chris@0: { Chris@0: $this->context = $context; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a string representation of the type of the value. Chris@0: * Chris@0: * This method should be used if you pass the type of a value as Chris@0: * message parameter to a constraint violation. Note that such Chris@0: * parameters should usually not be included in messages aimed at Chris@0: * non-technical people. Chris@0: * Chris@0: * @param mixed $value The value to return the type of Chris@0: * Chris@0: * @return string The type of the value Chris@0: */ Chris@0: protected function formatTypeOf($value) Chris@0: { Chris@17: return \is_object($value) ? \get_class($value) : \gettype($value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a string representation of the value. Chris@0: * Chris@0: * This method returns the equivalent PHP tokens for most scalar types Chris@0: * (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped Chris@0: * in double quotes ("). Objects, arrays and resources are formatted as Chris@0: * "object", "array" and "resource". If the $format bitmask contains Chris@0: * the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted Chris@0: * as RFC-3339 dates ("Y-m-d H:i:s"). Chris@0: * Chris@0: * Be careful when passing message parameters to a constraint violation Chris@0: * that (may) contain objects, arrays or resources. These parameters Chris@0: * should only be displayed for technical users. Non-technical users Chris@0: * won't know what an "object", "array" or "resource" is and will be Chris@0: * confused by the violation message. Chris@0: * Chris@0: * @param mixed $value The value to format as string Chris@0: * @param int $format A bitwise combination of the format Chris@0: * constants in this class Chris@0: * Chris@0: * @return string The string representation of the passed value Chris@0: */ Chris@0: protected function formatValue($value, $format = 0) Chris@0: { Chris@0: $isDateTime = $value instanceof \DateTimeInterface; Chris@0: Chris@0: if (($format & self::PRETTY_DATE) && $isDateTime) { Chris@0: if (class_exists('IntlDateFormatter')) { Chris@0: $locale = \Locale::getDefault(); Chris@0: $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); Chris@0: Chris@0: // neither the native nor the stub IntlDateFormatter support Chris@0: // DateTimeImmutable as of yet Chris@0: if (!$value instanceof \DateTime) { Chris@0: $value = new \DateTime( Chris@0: $value->format('Y-m-d H:i:s.u e'), Chris@0: $value->getTimezone() Chris@0: ); Chris@0: } Chris@0: Chris@0: return $formatter->format($value); Chris@0: } Chris@0: Chris@0: return $value->format('Y-m-d H:i:s'); Chris@0: } Chris@0: Chris@17: if (\is_object($value)) { Chris@0: if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) { Chris@0: return $value->__toString(); Chris@0: } Chris@0: Chris@0: return 'object'; Chris@0: } Chris@0: Chris@17: if (\is_array($value)) { Chris@0: return 'array'; Chris@0: } Chris@0: Chris@17: if (\is_string($value)) { Chris@0: return '"'.$value.'"'; Chris@0: } Chris@0: Chris@17: if (\is_resource($value)) { Chris@0: return 'resource'; Chris@0: } Chris@0: Chris@0: if (null === $value) { Chris@0: return 'null'; Chris@0: } Chris@0: Chris@0: if (false === $value) { Chris@0: return 'false'; Chris@0: } Chris@0: Chris@0: if (true === $value) { Chris@0: return 'true'; Chris@0: } Chris@0: Chris@0: return (string) $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a string representation of a list of values. Chris@0: * Chris@0: * Each of the values is converted to a string using Chris@0: * {@link formatValue()}. The values are then concatenated with commas. Chris@0: * Chris@0: * @param array $values A list of values Chris@0: * @param int $format A bitwise combination of the format Chris@0: * constants in this class Chris@0: * Chris@0: * @return string The string representation of the value list Chris@0: * Chris@0: * @see formatValue() Chris@0: */ Chris@0: protected function formatValues(array $values, $format = 0) Chris@0: { Chris@0: foreach ($values as $key => $value) { Chris@0: $values[$key] = $this->formatValue($value, $format); Chris@0: } Chris@0: Chris@0: return implode(', ', $values); Chris@0: } Chris@0: }