PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
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 3.0.1, at vendor/ramsey/collection/src/Tool/ValueToStringTrait.php

95 lines 2.3 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_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