Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2698bb3

Browse filesBrowse files
author
Cas Leentfaar
committed
replaced printable methods with those of #10687
1 parent 0fa677b commit 2698bb3
Copy full SHA for 2698bb3

File tree

1 file changed

+55
-27
lines changed
Filter options

1 file changed

+55
-27
lines changed

‎src/Symfony/Component/OptionsResolver/OptionsResolver.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/OptionsResolver.php
+55-27Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,11 @@ private function validateOptionValues(array $options)
301301
foreach ($this->allowedValues as $option => $allowedValues) {
302302
if (isset($options[$option])) {
303303
if (is_array($allowedValues) && !in_array($options[$option], $allowedValues, true)) {
304-
$allowedValuesPrintable = implode(', ', array_map(array($this, 'createPrintableValue'), $allowedValues));
305-
throw new InvalidOptionsException(sprintf('The option "%s" has the value %s, but is expected to be one of %s', $option, $this->createPrintableValue($options[$option]), $allowedValuesPrintable));
304+
throw new InvalidOptionsException(sprintf('The option "%s" has the value %s, but is expected to be one of %s', $option, $this->formatValue($options[$option]), $this->formatValues($allowedValues)));
306305
}
307306

308307
if (is_callable($allowedValues) && !call_user_func($allowedValues, $options[$option])) {
309-
throw new InvalidOptionsException(sprintf('The option "%s" has the value %s, which is not valid', $option, $this->createPrintableValue($options[$option])));
308+
throw new InvalidOptionsException(sprintf('The option "%s" has the value %s, which is not valid', $option, $this->formatValue($options[$option])));
310309
}
311310
}
312311
}
@@ -341,65 +340,94 @@ private function validateOptionTypes(array $options)
341340
}
342341
}
343342

344-
$printableValue = $this->createPrintableValue($value);
345-
$printableType = $this->createPrintableType($value);
346-
347343
throw new InvalidOptionsException(sprintf(
348344
'The option "%s" with value %s is expected to be of type "%s", got "%s"',
349345
$option,
350-
$printableValue,
346+
$this->formatValue($value),
351347
implode('", "', $allowedTypes),
352-
$printableType
348+
$this->formatTypeOf($value)
353349
));
354350
}
355351
}
356352

357353
/**
358-
* Attempts to convert a given value into a (short) string or integer.
354+
* Returns a string representation of the type of the value.
355+
*
356+
* @param mixed $value
357+
*
358+
* @return string
359+
*/
360+
protected function formatTypeOf($value)
361+
{
362+
return is_object($value) ? get_class($value) : gettype($value);
363+
}
364+
365+
/**
366+
* Returns a string representation of the value.
359367
*
360-
* @param mixed $value The value to convert.
368+
* @param mixed $value
369+
* @param bool $prettyDateTime
361370
*
362-
* @return string The printable value.
371+
* @return string
363372
*/
364-
private function createPrintableValue($value)
373+
protected function formatValue($value, $prettyDateTime = false)
365374
{
375+
if ($prettyDateTime && $value instanceof \DateTime) {
376+
if (class_exists('IntlDateFormatter')) {
377+
$locale = \Locale::getDefault();
378+
$formatter = new \IntlDateFormatter($locale, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT);
379+
380+
return $formatter->format($value);
381+
}
382+
383+
return $value->format('Y-m-d H:i:s');
384+
}
385+
366386
if (is_object($value)) {
367-
return get_class($value);
387+
return 'object';
368388
}
369389

370390
if (is_array($value)) {
371-
return 'Array';
391+
return 'array';
392+
}
393+
394+
if (is_string($value)) {
395+
return '"'.$value.'"';
396+
}
397+
398+
if (is_resource($value)) {
399+
return 'resource';
372400
}
373401

374402
if (null === $value) {
375403
return 'null';
376404
}
377405

378-
if (is_string($value)) {
379-
return '"'.$value.'"';
406+
if (false === $value) {
407+
return 'false';
380408
}
381409

382-
if (is_bool($value)) {
383-
return (string) var_export($value, true);
410+
if (true === $value) {
411+
return 'true';
384412
}
385413

386414
return (string) $value;
387415
}
388416

389417
/**
390-
* Attempts to convert a given value's type into a printable string.
418+
* Returns a string representation of a list of values.
391419
*
392-
* @param mixed $value The value to convert.
420+
* @param array $values
421+
* @param bool $prettyDateTime
393422
*
394-
* @return string Objects get returned as their classname,
395-
* all other values are put through gettype().
423+
* @return string
396424
*/
397-
private function createPrintableType($value)
425+
protected function formatValues(array $values, $prettyDateTime = false)
398426
{
399-
if (is_object($value)) {
400-
return get_class($value);
401-
} else {
402-
return gettype($value);
427+
foreach ($values as $key => $value) {
428+
$values[$key] = $this->formatValue($value, $prettyDateTime);
403429
}
430+
431+
return implode(', ', $values);
404432
}
405433
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.