googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
collection
/
src
/
Tool
/
ValueToStringTrait.php
googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
collection
/
src
/
Tool
Last commit date
TypeTrait.php
3 years ago
ValueExtractorTrait.php
3 years ago
ValueToStringTrait.php
3 years ago
ValueToStringTrait.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the ramsey/collection library |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | * |
| 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Ramsey\Collection\Tool; |
| 16 | |
| 17 | use DateTimeInterface; |
| 18 | |
| 19 | use function get_class; |
| 20 | use function get_resource_type; |
| 21 | use function is_array; |
| 22 | use function is_bool; |
| 23 | use function is_callable; |
| 24 | use function is_resource; |
| 25 | use function is_scalar; |
| 26 | |
| 27 | /** |
| 28 | * Provides functionality to express a value as string |
| 29 | */ |
| 30 | trait ValueToStringTrait |
| 31 | { |
| 32 | /** |
| 33 | * Returns a string representation of the value. |
| 34 | * |
| 35 | * - null value: `'NULL'` |
| 36 | * - boolean: `'TRUE'`, `'FALSE'` |
| 37 | * - array: `'Array'` |
| 38 | * - scalar: converted-value |
| 39 | * - resource: `'(type resource #number)'` |
| 40 | * - object with `__toString()`: result of `__toString()` |
| 41 | * - object DateTime: ISO 8601 date |
| 42 | * - object: `'(className Object)'` |
| 43 | * - anonymous function: same as object |
| 44 | * |
| 45 | * @param mixed $value the value to return as a string. |
| 46 | */ |
| 47 | protected function toolValueToString($value): string |
| 48 | { |
| 49 | // null |
| 50 | if ($value === null) { |
| 51 | return 'NULL'; |
| 52 | } |
| 53 | |
| 54 | // boolean constants |
| 55 | if (is_bool($value)) { |
| 56 | return $value ? 'TRUE' : 'FALSE'; |
| 57 | } |
| 58 | |
| 59 | // array |
| 60 | if (is_array($value)) { |
| 61 | return 'Array'; |
| 62 | } |
| 63 | |
| 64 | // scalar types (integer, float, string) |
| 65 | if (is_scalar($value)) { |
| 66 | return (string) $value; |
| 67 | } |
| 68 | |
| 69 | // resource |
| 70 | if (is_resource($value)) { |
| 71 | return '(' . get_resource_type($value) . ' resource #' . (int) $value . ')'; |
| 72 | } |
| 73 | |
| 74 | // If we don't know what it is, use var_export(). |
| 75 | if (!is_object($value)) { |
| 76 | return '(' . var_export($value, true) . ')'; |
| 77 | } |
| 78 | |
| 79 | // From here, $value should be an object. |
| 80 | |
| 81 | // __toString() is implemented |
| 82 | if (is_callable([$value, '__toString'])) { |
| 83 | return (string) $value->__toString(); |
| 84 | } |
| 85 | |
| 86 | // object of type \DateTime |
| 87 | if ($value instanceof DateTimeInterface) { |
| 88 | return $value->format('c'); |
| 89 | } |
| 90 | |
| 91 | // unknown type |
| 92 | return '(' . get_class($value) . ' Object)'; |
| 93 | } |
| 94 | } |
| 95 |