| 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 <[email protected]> |
| 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_object; |
| 25 |
use function is_resource; |
| 26 |
use function is_scalar; |
| 27 |
use function var_export; |
| 28 |
|
| 29 |
/** |
| 30 |
* Provides functionality to express a value as string |
| 31 |
*/ |
| 32 |
trait ValueToStringTrait |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Returns a string representation of the value. |
| 36 |
* |
| 37 |
* - null value: `'NULL'` |
| 38 |
* - boolean: `'TRUE'`, `'FALSE'` |
| 39 |
* - array: `'Array'` |
| 40 |
* - scalar: converted-value |
| 41 |
* - resource: `'(type resource #number)'` |
| 42 |
* - object with `__toString()`: result of `__toString()` |
| 43 |
* - object DateTime: ISO 8601 date |
| 44 |
* - object: `'(className Object)'` |
| 45 |
* - anonymous function: same as object |
| 46 |
* |
| 47 |
* @param mixed $value the value to return as a string. |
| 48 |
*/ |
| 49 |
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
| 50 |
protected function toolValueToString($value): string |
| 51 |
{ |
| 52 |
// null |
| 53 |
if ($value === null) { |
| 54 |
return 'NULL'; |
| 55 |
} |
| 56 |
|
| 57 |
// boolean constants |
| 58 |
if (is_bool($value)) { |
| 59 |
return $value ? 'TRUE' : 'FALSE'; |
| 60 |
} |
| 61 |
|
| 62 |
// array |
| 63 |
if (is_array($value)) { |
| 64 |
return 'Array'; |
| 65 |
} |
| 66 |
|
| 67 |
// scalar types (integer, float, string) |
| 68 |
if (is_scalar($value)) { |
| 69 |
return (string) $value; |
| 70 |
} |
| 71 |
|
| 72 |
// resource |
| 73 |
if (is_resource($value)) { |
| 74 |
return '(' . get_resource_type($value) . ' resource #' . (int) $value . ')'; |
| 75 |
} |
| 76 |
|
| 77 |
// If we don't know what it is, use var_export(). |
| 78 |
if (!is_object($value)) { |
| 79 |
return '(' . var_export($value, true) . ')'; |
| 80 |
} |
| 81 |
|
| 82 |
// From here, $value should be an object. |
| 83 |
|
| 84 |
// __toString() is implemented |
| 85 |
if (is_callable([$value, '__toString'])) { |
| 86 |
return (string) $value->__toString(); |
| 87 |
} |
| 88 |
|
| 89 |
// object of type \DateTime |
| 90 |
if ($value instanceof DateTimeInterface) { |
| 91 |
return $value->format('c'); |
| 92 |
} |
| 93 |
|
| 94 |
// unknown type |
| 95 |
// phpcs:ignore SlevomatCodingStandard.Classes.ModernClassNameReference.ClassNameReferencedViaFunctionCall |
| 96 |
return '(' . get_class($value) . ' Object)'; |
| 97 |
} |
| 98 |
} |
| 99 |
|