2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator;
|
|
|
|
|
2015-09-30 14:39:58 +01:00
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
2014-09-24 10:48:52 +01:00
|
|
|
|
2012-01-30 19:56:10 +00:00
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Base class for constraint validators.
|
2012-01-30 19:56:10 +00:00
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*/
|
2010-06-24 09:40:05 +01:00
|
|
|
abstract class ConstraintValidator implements ConstraintValidatorInterface
|
|
|
|
{
|
2014-08-04 11:36:09 +01:00
|
|
|
/**
|
|
|
|
* Whether to format {@link \DateTime} objects as RFC-3339 dates
|
|
|
|
* ("Y-m-d H:i:s").
|
|
|
|
*/
|
|
|
|
const PRETTY_DATE = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to cast objects with a "__toString()" method to strings.
|
|
|
|
*/
|
|
|
|
const OBJECT_TO_STRING = 2;
|
|
|
|
|
2011-05-17 22:04:13 +01:00
|
|
|
/**
|
2016-12-05 16:26:24 +00:00
|
|
|
* @var ExecutionContextInterface
|
2011-05-17 22:04:13 +01:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $context;
|
2012-02-01 12:53:45 +00:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2012-11-22 14:58:46 +00:00
|
|
|
public function initialize(ExecutionContextInterface $context)
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
$this->context = $context;
|
2012-04-17 15:54:40 +01:00
|
|
|
}
|
2014-04-10 16:39:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string representation of the type of the value.
|
|
|
|
*
|
2014-07-30 09:05:53 +01:00
|
|
|
* This method should be used if you pass the type of a value as
|
|
|
|
* message parameter to a constraint violation. Note that such
|
|
|
|
* parameters should usually not be included in messages aimed at
|
|
|
|
* non-technical people.
|
2014-04-10 16:39:39 +01:00
|
|
|
*
|
2014-07-30 09:05:53 +01:00
|
|
|
* @param mixed $value The value to return the type of
|
|
|
|
*
|
|
|
|
* @return string The type of the value
|
2014-04-10 16:39:39 +01:00
|
|
|
*/
|
2014-07-24 13:14:19 +01:00
|
|
|
protected function formatTypeOf($value)
|
2014-04-10 16:39:39 +01:00
|
|
|
{
|
2018-07-05 12:24:53 +01:00
|
|
|
return \is_object($value) ? \get_class($value) : \gettype($value);
|
2014-04-10 16:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string representation of the value.
|
|
|
|
*
|
2014-07-30 09:05:53 +01:00
|
|
|
* This method returns the equivalent PHP tokens for most scalar types
|
|
|
|
* (i.e. "false" for false, "1" for 1 etc.). Strings are always wrapped
|
|
|
|
* in double quotes ("). Objects, arrays and resources are formatted as
|
2016-03-12 00:42:00 +00:00
|
|
|
* "object", "array" and "resource". If the $format bitmask contains
|
2016-05-11 14:21:50 +01:00
|
|
|
* the PRETTY_DATE bit, then {@link \DateTime} objects will be formatted
|
2016-03-12 00:42:00 +00:00
|
|
|
* as RFC-3339 dates ("Y-m-d H:i:s").
|
2014-07-30 09:05:53 +01:00
|
|
|
*
|
|
|
|
* Be careful when passing message parameters to a constraint violation
|
|
|
|
* that (may) contain objects, arrays or resources. These parameters
|
|
|
|
* should only be displayed for technical users. Non-technical users
|
|
|
|
* won't know what an "object", "array" or "resource" is and will be
|
|
|
|
* confused by the violation message.
|
|
|
|
*
|
2014-12-04 20:26:11 +00:00
|
|
|
* @param mixed $value The value to format as string
|
|
|
|
* @param int $format A bitwise combination of the format
|
|
|
|
* constants in this class
|
2014-04-10 16:39:39 +01:00
|
|
|
*
|
2014-07-30 09:05:53 +01:00
|
|
|
* @return string The string representation of the passed value
|
2014-04-10 16:39:39 +01:00
|
|
|
*/
|
2014-08-04 11:36:09 +01:00
|
|
|
protected function formatValue($value, $format = 0)
|
2014-04-10 16:39:39 +01:00
|
|
|
{
|
2019-09-03 12:59:35 +01:00
|
|
|
if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
|
2014-04-10 16:39:39 +01:00
|
|
|
if (class_exists('IntlDateFormatter')) {
|
|
|
|
$locale = \Locale::getDefault();
|
2019-09-03 12:59:35 +01:00
|
|
|
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $value->getTimezone());
|
2014-04-10 16:39:39 +01:00
|
|
|
|
2014-08-15 11:35:26 +01:00
|
|
|
// neither the native nor the stub IntlDateFormatter support
|
|
|
|
// DateTimeImmutable as of yet
|
|
|
|
if (!$value instanceof \DateTime) {
|
2019-12-09 13:42:15 +00:00
|
|
|
$value = new \DateTime($value->format('Y-m-d H:i:s.u e'));
|
2014-08-15 11:35:26 +01:00
|
|
|
}
|
|
|
|
|
2014-04-10 16:39:39 +01:00
|
|
|
return $formatter->format($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
2018-07-05 12:24:53 +01:00
|
|
|
if (\is_object($value)) {
|
2016-03-12 00:42:00 +00:00
|
|
|
if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
|
2014-08-04 11:36:09 +01:00
|
|
|
return $value->__toString();
|
|
|
|
}
|
|
|
|
|
2014-07-28 14:51:04 +01:00
|
|
|
return 'object';
|
2014-04-10 16:39:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 12:24:53 +01:00
|
|
|
if (\is_array($value)) {
|
2014-07-28 14:51:04 +01:00
|
|
|
return 'array';
|
2014-04-10 16:39:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 12:24:53 +01:00
|
|
|
if (\is_string($value)) {
|
2014-04-10 16:39:39 +01:00
|
|
|
return '"'.$value.'"';
|
|
|
|
}
|
|
|
|
|
2018-07-05 12:24:53 +01:00
|
|
|
if (\is_resource($value)) {
|
2014-07-28 14:51:04 +01:00
|
|
|
return 'resource';
|
2014-04-10 16:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $value) {
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false === $value) {
|
|
|
|
return 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (true === $value) {
|
|
|
|
return 'true';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string) $value;
|
|
|
|
}
|
2014-05-22 09:30:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string representation of a list of values.
|
|
|
|
*
|
2014-07-30 09:05:53 +01:00
|
|
|
* Each of the values is converted to a string using
|
|
|
|
* {@link formatValue()}. The values are then concatenated with commas.
|
|
|
|
*
|
2014-12-04 20:26:11 +00:00
|
|
|
* @param array $values A list of values
|
|
|
|
* @param int $format A bitwise combination of the format
|
|
|
|
* constants in this class
|
2014-07-30 09:05:53 +01:00
|
|
|
*
|
|
|
|
* @return string The string representation of the value list
|
2014-05-22 09:30:24 +01:00
|
|
|
*
|
2014-07-30 09:05:53 +01:00
|
|
|
* @see formatValue()
|
2014-05-22 09:30:24 +01:00
|
|
|
*/
|
2014-08-04 11:36:09 +01:00
|
|
|
protected function formatValues(array $values, $format = 0)
|
2014-05-22 09:30:24 +01:00
|
|
|
{
|
|
|
|
foreach ($values as $key => $value) {
|
2014-08-04 11:36:09 +01:00
|
|
|
$values[$key] = $this->formatValue($value, $format);
|
2014-05-22 09:30:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $values);
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|