1 | <?php |
||
2 | /** |
||
3 | * ExportTrait |
||
4 | * |
||
5 | * @package php-deferred-callchain |
||
6 | * @author Jean Claveau |
||
7 | */ |
||
8 | namespace JClaveau\Async; |
||
9 | |||
10 | /** |
||
11 | * Trait gathering support of export functions like toString() or |
||
12 | * jsonSerialize() |
||
13 | */ |
||
14 | trait ExportTrait |
||
15 | { |
||
16 | /** |
||
17 | * For implementing JsonSerializable interface. |
||
18 | * |
||
19 | * @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php |
||
20 | */ |
||
21 | public function jsonSerialize() |
||
22 | { |
||
23 | return $this->stack; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Outputs the PHP code producing the current call chain while it's casted |
||
28 | * as a string. |
||
29 | * |
||
30 | * @return string The PHP code corresponding to this call chain |
||
31 | */ |
||
32 | public function __toString() |
||
33 | { |
||
34 | return $this->toString(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Outputs the PHP code producing the current call chain while it's casted |
||
39 | * as a string. |
||
40 | * |
||
41 | * @param array $options target: mixed | max_parameter_length: int | short_objects: bool |
||
42 | * @return string The PHP code corresponding to this call chain |
||
43 | */ |
||
44 | public function toString(array $options=[]) |
||
45 | { |
||
46 | $target = isset($options['target']) ? $options['target'] : $this->expectedTarget; |
||
47 | $max_param_length = isset($options['max_parameter_length']) ? $options['max_parameter_length'] : 56; |
||
48 | $short_objects = isset($options['short_objects']) ? $options['short_objects'] : true; |
||
49 | |||
50 | $string = '(new ' . get_called_class(); |
||
51 | $target && $string .= '(' . static::varExport($target, [ |
||
52 | 'short_objects' => $short_objects, |
||
53 | 'max_length' => $max_param_length, |
||
54 | ]) . ')'; |
||
55 | $string .= ')'; |
||
56 | |||
57 | foreach ($this->stack as $i => $call) { |
||
58 | if (isset($call['method'])) { |
||
59 | $string .= '->'; |
||
60 | $string .= $call['method'].'('; |
||
61 | $string .= implode(', ', array_map(function($argument) use ($max_param_length, $short_objects) { |
||
62 | return static::varExport($argument, [ |
||
63 | 'short_objects' => $short_objects, |
||
64 | 'max_length' => $max_param_length |
||
65 | ]); |
||
66 | }, $call['arguments'])); |
||
67 | $string .= ')'; |
||
68 | } |
||
69 | else { |
||
70 | $string .= '[' . static::varExport($call['entry'], [ |
||
71 | 'short_objects' => $short_objects, |
||
72 | 'max_length' => $max_param_length, |
||
73 | ]) . ']'; |
||
74 | } |
||
75 | |||
76 | if (! empty($options['limit']) && $options['limit'] == $i) { |
||
77 | break; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return $string; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Enhanced var_export() required for dumps. |
||
86 | * |
||
87 | * @param mixed $variable |
||
88 | * @param array $options max_length: int | short_objects: bool |
||
89 | * @return string The PHP code of the variable |
||
90 | */ |
||
91 | protected static function varExport($variable, array $options=[]) |
||
92 | { |
||
93 | $options['max_length'] = isset($options['max_length']) ? $options['max_length'] : 56; |
||
94 | $options['short_objects'] = (! empty($options['short_objects'])) || in_array('short_objects', $options); |
||
95 | |||
96 | $export = var_export($variable, true); |
||
97 | |||
98 | if ($options['short_objects']) { |
||
99 | if (is_object($variable)) { |
||
100 | $export = ' ' . get_class($variable) . ' #' . spl_object_id($variable) . ' '; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | if (strlen($export) > $options['max_length']) { |
||
105 | |||
106 | if (is_object($variable)) { |
||
107 | // shortening short objects would only slow the workflow |
||
108 | $export = get_class($variable) . ' #' . spl_object_id($variable); |
||
109 | } |
||
110 | elseif (is_string($variable)) { |
||
111 | $keep_length = floor(($options['max_length'] - 5) / 2); |
||
112 | |||
113 | $export = substr($variable, 0, (int) $keep_length) |
||
114 | . ' ... ' |
||
115 | . substr($variable, -$keep_length) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
116 | ; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | return $export; |
||
121 | } |
||
122 | |||
123 | /**/ |
||
124 | } |
||
125 |