You are here

protected function ConstraintValidator::formatValue in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/ConstraintValidator.php \Symfony\Component\Validator\ConstraintValidator::formatValue()

Returns a string representation of the value.

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 "object", "array" and "resource". If the parameter $prettyDateTime is set to true, {@link \DateTime} objects will be formatted as RFC-3339 dates ("Y-m-d H:i:s").

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.

Parameters

mixed $value The value to format as string:

int $format A bitwise combination of the format: constants in this class

Return value

string The string representation of the passed value

1 call to ConstraintValidator::formatValue()
ConstraintValidator::formatValues in vendor/symfony/validator/ConstraintValidator.php
Returns a string representation of a list of values.

File

vendor/symfony/validator/ConstraintValidator.php, line 137

Class

ConstraintValidator
Base class for constraint validators.

Namespace

Symfony\Component\Validator

Code

protected function formatValue($value, $format = 0) {
  $isDateTime = $value instanceof \DateTime || $value instanceof \DateTimeInterface;
  if ($format & self::PRETTY_DATE && $isDateTime) {
    if (class_exists('IntlDateFormatter')) {
      $locale = \Locale::getDefault();
      $formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);

      // neither the native nor the stub IntlDateFormatter support
      // DateTimeImmutable as of yet
      if (!$value instanceof \DateTime) {
        $value = new \DateTime($value
          ->format('Y-m-d H:i:s.u e'), $value
          ->getTimezone());
      }
      return $formatter
        ->format($value);
    }
    return $value
      ->format('Y-m-d H:i:s');
  }
  if (is_object($value)) {
    if ($format & self::OBJECT_TO_STRING && method_exists($value, '__toString')) {
      return $value
        ->__toString();
    }
    return 'object';
  }
  if (is_array($value)) {
    return 'array';
  }
  if (is_string($value)) {
    return '"' . $value . '"';
  }
  if (is_resource($value)) {
    return 'resource';
  }
  if (null === $value) {
    return 'null';
  }
  if (false === $value) {
    return 'false';
  }
  if (true === $value) {
    return 'true';
  }
  return (string) $value;
}