PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor / ramsey / collection / src / Tool / ValueToStringTrait.php

ValueToStringTrait.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, 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