annotate vendor/consolidation/output-formatters/src/Exception/AbstractDataFormatException.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 namespace Consolidation\OutputFormatters\Exception;
Chris@0 3
Chris@0 4 /**
Chris@0 5 * Contains some helper functions used by exceptions in this project.
Chris@0 6 */
Chris@0 7 abstract class AbstractDataFormatException extends \Exception
Chris@0 8 {
Chris@0 9 /**
Chris@0 10 * Return a description of the data type represented by the provided parameter.
Chris@0 11 *
Chris@0 12 * @param \ReflectionClass $data The data type to describe. Note that
Chris@0 13 * \ArrayObject is used as a proxy to mean an array primitive (or an ArrayObject).
Chris@0 14 * @return string
Chris@0 15 */
Chris@0 16 protected static function describeDataType($data)
Chris@0 17 {
Chris@0 18 if (is_array($data) || ($data instanceof \ReflectionClass)) {
Chris@0 19 if (is_array($data) || ($data->getName() == 'ArrayObject')) {
Chris@0 20 return 'an array';
Chris@0 21 }
Chris@0 22 return 'an instance of ' . $data->getName();
Chris@0 23 }
Chris@0 24 if (is_string($data)) {
Chris@0 25 return 'a string';
Chris@0 26 }
Chris@0 27 if (is_object($data)) {
Chris@0 28 return 'an instance of ' . get_class($data);
Chris@0 29 }
Chris@0 30 throw new \Exception("Undescribable data error: " . var_export($data, true));
Chris@0 31 }
Chris@0 32
Chris@0 33 protected static function describeAllowedTypes($allowedTypes)
Chris@0 34 {
Chris@0 35 if (is_array($allowedTypes) && !empty($allowedTypes)) {
Chris@0 36 if (count($allowedTypes) > 1) {
Chris@0 37 return static::describeListOfAllowedTypes($allowedTypes);
Chris@0 38 }
Chris@0 39 $allowedTypes = $allowedTypes[0];
Chris@0 40 }
Chris@0 41 return static::describeDataType($allowedTypes);
Chris@0 42 }
Chris@0 43
Chris@0 44 protected static function describeListOfAllowedTypes($allowedTypes)
Chris@0 45 {
Chris@0 46 $descriptions = [];
Chris@0 47 foreach ($allowedTypes as $oneAllowedType) {
Chris@0 48 $descriptions[] = static::describeDataType($oneAllowedType);
Chris@0 49 }
Chris@0 50 if (count($descriptions) == 2) {
Chris@0 51 return "either {$descriptions[0]} or {$descriptions[1]}";
Chris@0 52 }
Chris@0 53 $lastDescription = array_pop($descriptions);
Chris@0 54 $otherDescriptions = implode(', ', $descriptions);
Chris@0 55 return "one of $otherDescriptions or $lastDescription";
Chris@0 56 }
Chris@0 57 }