2 namespace Consolidation\OutputFormatters\Exception;
5 * Contains some helper functions used by exceptions in this project.
7 abstract class AbstractDataFormatException extends \Exception
10 * Return a description of the data type represented by the provided parameter.
12 * @param \ReflectionClass $data The data type to describe. Note that
13 * \ArrayObject is used as a proxy to mean an array primitive (or an ArrayObject).
16 protected static function describeDataType($data)
18 if (is_array($data) || ($data instanceof \ReflectionClass)) {
19 if (is_array($data) || ($data->getName() == 'ArrayObject')) {
22 return 'an instance of ' . $data->getName();
24 if (is_string($data)) {
27 if (is_object($data)) {
28 return 'an instance of ' . get_class($data);
30 throw new \Exception("Undescribable data error: " . var_export($data, true));
33 protected static function describeAllowedTypes($allowedTypes)
35 if (is_array($allowedTypes) && !empty($allowedTypes)) {
36 if (count($allowedTypes) > 1) {
37 return static::describeListOfAllowedTypes($allowedTypes);
39 $allowedTypes = $allowedTypes[0];
41 return static::describeDataType($allowedTypes);
44 protected static function describeListOfAllowedTypes($allowedTypes)
47 foreach ($allowedTypes as $oneAllowedType) {
48 $descriptions[] = static::describeDataType($oneAllowedType);
50 if (count($descriptions) == 2) {
51 return "either {$descriptions[0]} or {$descriptions[1]}";
53 $lastDescription = array_pop($descriptions);
54 $otherDescriptions = implode(', ', $descriptions);
55 return "one of $otherDescriptions or $lastDescription";