PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.8.0
Defender Security – Malware Scanner, Login Security & Firewall v5.8.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / ramsey / collection / src / Tool / ValueToStringTrait.php

ValueToStringTrait.php in Defender Security – Malware Scanner, Login Security & Firewall 5.8.0, at vendor/ramsey/collection/src/Tool/ValueToStringTrait.php

99 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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