@@ -22,6 +22,8 @@ class DeferredCallChain implements \JsonSerializable, \ArrayAccess
22
22
{
23
23
use \JClaveau \Traits \Fluent \New_;
24
24
use FunctionCallTrait;
25
+ use ArrayAccessTrait;
26
+ use ExportTrait;
25
27
26
28
/** @var array $stack The stack of deferred calls */
27
29
protected $ stack = [];
@@ -41,24 +43,6 @@ public function __construct($class_type_interface_or_instance=null)
41
43
}
42
44
}
43
45
44
- /**
45
- * ArrayAccess interface
46
- *
47
- * @param string $key The entry to acces
48
- */
49
- public function &offsetGet ($ key )
50
- {
51
- $ caller = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , 1 )[0 ];
52
-
53
- $ this ->stack [] = [
54
- 'entry ' => $ key ,
55
- 'file ' => isset ($ caller ['file ' ]) ? $ caller ['file ' ] : null ,
56
- 'line ' => isset ($ caller ['line ' ]) ? $ caller ['line ' ] : null ,
57
- ];
58
-
59
- return $ this ;
60
- }
61
-
62
46
/**
63
47
* Stores any call in the the stack.
64
48
*
@@ -82,97 +66,21 @@ public final function __call($method, array $arguments)
82
66
}
83
67
84
68
/**
85
- * For implementing JsonSerializable interface.
86
- *
87
- * @see https://secure.php.net/manual/en/jsonserializable.jsonserialize.php
88
- */
89
- public function jsonSerialize ()
90
- {
91
- return $ this ->stack ;
92
- }
93
-
94
- /**
95
- * Outputs the PHP code producing the current call chain while it's casted
96
- * as a string.
97
- *
98
- * @return string The PHP code corresponding to this call chain
99
- */
100
- public function __toString ()
101
- {
102
- return $ this ->toString ();
103
- }
104
-
105
- /**
106
- * Outputs the PHP code producing the current call chain while it's casted
107
- * as a string.
69
+ * ArrayAccess interface
108
70
*
109
- * @return string The PHP code corresponding to this call chain
71
+ * @param string $key The entry to acces
110
72
*/
111
- protected function toString ( array $ options =[] )
73
+ public function & offsetGet ( $ key )
112
74
{
113
- $ target = isset ( $ options [ ' target ' ]) ? $ options [ ' target ' ] : $ this -> expectedTarget ;
75
+ $ caller = debug_backtrace ( DEBUG_BACKTRACE_IGNORE_ARGS , 1 )[ 0 ] ;
114
76
115
- $ string = '(new ' . get_called_class ();
116
- $ target && $ string .= '( ' . static ::varExport ($ target , ['short_objects ' ]) . ') ' ;
117
- $ string .= ') ' ;
118
-
119
- foreach ($ this ->stack as $ i => $ call ) {
120
- if (isset ($ call ['method ' ])) {
121
- $ string .= '-> ' ;
122
- $ string .= $ call ['method ' ].'( ' ;
123
- $ string .= implode (', ' , array_map (function ($ argument ) {
124
- return static ::varExport ($ argument , ['short_objects ' ]);
125
- }, $ call ['arguments ' ]));
126
- $ string .= ') ' ;
127
- }
128
- else {
129
- $ string .= '[ ' . static ::varExport ($ call ['entry ' ], ['short_objects ' ]) . '] ' ;
130
- }
131
-
132
- if (! empty ($ options ['limit ' ]) && $ options ['limit ' ] == $ i ) {
133
- break ;
134
- }
135
- }
77
+ $ this ->stack [] = [
78
+ 'entry ' => $ key ,
79
+ 'file ' => isset ($ caller ['file ' ]) ? $ caller ['file ' ] : null ,
80
+ 'line ' => isset ($ caller ['line ' ]) ? $ caller ['line ' ] : null ,
81
+ ];
136
82
137
- return $ string ;
138
- }
139
-
140
- /**
141
- * Enhanced var_export() required for dumps.
142
- *
143
- * @param mixed $variable
144
- * @param array $options max_length | alias_instances
145
- * @return string The PHP code of the variable
146
- */
147
- protected static function varExport ($ variable , array $ options =[])
148
- {
149
- $ options ['max_length ' ] = isset ($ options ['max_length ' ]) ? $ options ['max_length ' ] : 512 ;
150
- $ options ['short_objects ' ] = ! empty ($ options ['short_objects ' ]) || in_array ('short_objects ' , $ options );
151
-
152
- $ export = var_export ($ variable , true );
153
-
154
- if ($ options ['short_objects ' ]) {
155
- if (is_object ($ variable )) {
156
- $ export = ' ' . get_class ($ variable ) . ' # ' . spl_object_id ($ variable ) . ' ' ;
157
- }
158
- }
159
-
160
- if (strlen ($ export ) > $ options ['max_length ' ]) {
161
-
162
- if (is_object ($ variable )) {
163
- $ export = get_class ($ variable ) . ' # ' . spl_object_id ($ variable );
164
- }
165
- elseif (is_string ($ variable )) {
166
- $ keep_length = floor (($ options ['max_length ' ] - 5 ) / 2 );
167
-
168
- $ export = substr ($ variable , 0 , (int ) $ keep_length )
169
- . ' ... '
170
- . substr ($ variable , -$ keep_length )
171
- ;
172
- }
173
- }
174
-
175
- return $ export ;
83
+ return $ this ;
176
84
}
177
85
178
86
/**
@@ -350,45 +258,5 @@ public function __invoke($target=null)
350
258
return $ out ;
351
259
}
352
260
353
- /**
354
- * Unused part of the ArrayAccess interface
355
- *
356
- * @param $offset
357
- * @param $value
358
- * @throws \BadMethodCallException
359
- */
360
- public function offsetSet ($ offset , $ value )
361
- {
362
- throw new BadMethodCallException (
363
- "not implemented "
364
- );
365
- }
366
-
367
- /**
368
- * Unused part of the ArrayAccess interface
369
- *
370
- * @param $offset
371
- * @throws \BadMethodCallException
372
- */
373
- public function offsetExists ($ offset )
374
- {
375
- throw new BadMethodCallException (
376
- "not implemented "
377
- );
378
- }
379
-
380
- /**
381
- * Unused part of the ArrayAccess interface
382
- *
383
- * @param $offset
384
- * @throws \BadMethodCallException
385
- */
386
- public function offsetUnset ($ offset )
387
- {
388
- throw new BadMethodCallException (
389
- "not implemented "
390
- );
391
- }
392
-
393
261
/**/
394
262
}
0 commit comments