media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
collection
/
src
/
Tool
/
ValueToStringTrait.php
ValueToStringTrait.php in Media Cloud Sync 1.2.2, at includes/sdk/google/ramsey/collection/src/Tool/ValueToStringTrait.php
| 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 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\Ramsey\Collection\Tool; |
| 14 | |
| 15 | use DateTimeInterface; |
| 16 | use function get_class; |
| 17 | use function get_resource_type; |
| 18 | use function is_array; |
| 19 | use function is_bool; |
| 20 | use function is_callable; |
| 21 | use function is_object; |
| 22 | use function is_resource; |
| 23 | use function is_scalar; |
| 24 | use function var_export; |
| 25 | /** |
| 26 | * Provides functionality to express a value as string |
| 27 | */ |
| 28 | trait ValueToStringTrait |
| 29 | { |
| 30 | /** |
| 31 | * Returns a string representation of the value. |
| 32 | * |
| 33 | * - null value: `'NULL'` |
| 34 | * - boolean: `'TRUE'`, `'FALSE'` |
| 35 | * - array: `'Array'` |
| 36 | * - scalar: converted-value |
| 37 | * - resource: `'(type resource #number)'` |
| 38 | * - object with `__toString()`: result of `__toString()` |
| 39 | * - object DateTime: ISO 8601 date |
| 40 | * - object: `'(className Object)'` |
| 41 | * - anonymous function: same as object |
| 42 | * |
| 43 | * @param mixed $value the value to return as a string. |
| 44 | */ |
| 45 | // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
| 46 | protected function toolValueToString($value): string |
| 47 | { |
| 48 | // null |
| 49 | if ($value === null) { |
| 50 | return 'NULL'; |
| 51 | } |
| 52 | // boolean constants |
| 53 | if (is_bool($value)) { |
| 54 | return $value ? 'TRUE' : 'FALSE'; |
| 55 | } |
| 56 | // array |
| 57 | if (is_array($value)) { |
| 58 | return 'Array'; |
| 59 | } |
| 60 | // scalar types (integer, float, string) |
| 61 | if (is_scalar($value)) { |
| 62 | return (string) $value; |
| 63 | } |
| 64 | // resource |
| 65 | if (is_resource($value)) { |
| 66 | return '(' . get_resource_type($value) . ' resource #' . (int) $value . ')'; |
| 67 | } |
| 68 | // If we don't know what it is, use var_export(). |
| 69 | if (!is_object($value)) { |
| 70 | return '(' . var_export($value, \true) . ')'; |
| 71 | } |
| 72 | // From here, $value should be an object. |
| 73 | // __toString() is implemented |
| 74 | if (is_callable([$value, '__toString'])) { |
| 75 | return (string) $value->__toString(); |
| 76 | } |
| 77 | // object of type \DateTime |
| 78 | if ($value instanceof DateTimeInterface) { |
| 79 | return $value->format('c'); |
| 80 | } |
| 81 | // unknown type |
| 82 | // phpcs:ignore SlevomatCodingStandard.Classes.ModernClassNameReference.ClassNameReferencedViaFunctionCall |
| 83 | return '(' . get_class($value) . ' Object)'; |
| 84 | } |
| 85 | } |
| 86 |