@@ -34,8 +34,7 @@ A cloner is used to create an intermediate representation of any PHP variable.
34
34
Its output is a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
35
35
object that wraps this representation.
36
36
37
- You can create a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
38
- object this way::
37
+ You can create a ``Data `` object this way::
39
38
40
39
use Symfony\Component\VarDumper\Cloner\VarCloner;
41
40
@@ -45,8 +44,11 @@ object this way::
45
44
// see the example at the top of this page
46
45
// $dumper->dump($data);
47
46
48
- A cloner also applies limits when creating the representation, so that the
49
- corresponding Data object could represent only a subset of the cloned variable.
47
+ Whatever the cloned data structure, resulting ``Data `` objects are always
48
+ serializable.
49
+
50
+ A cloner applies limits when creating the representation, so that one
51
+ can represent only a subset of the cloned variable.
50
52
Before calling :method: `Symfony\\ Component\\ VarDumper\\ Cloner\\ VarCloner::cloneVar `,
51
53
you can configure these limits:
52
54
@@ -160,6 +162,17 @@ Another option for doing the same could be::
160
162
161
163
// $output is now populated with the dump representation of $variable
162
164
165
+ .. tip ::
166
+
167
+ You can pass ``true `` to the second argument of the
168
+ :method: `Symfony\\ Component\\ VarDumper\\ Dumper\\ AbstractDumper::dump `
169
+ method to make it return the dump as a string::
170
+
171
+ $output = $dumper->dump($cloner->cloneVar($variable), true);
172
+
173
+ .. versionadded :: 3.2
174
+ The ability to return a string was introduced in Symfony 3.2.
175
+
163
176
Dumpers implement the :class: `Symfony\\ Component\\ VarDumper\\ Dumper\\ DataDumperInterface `
164
177
interface that specifies the
165
178
:method: `dump(Data $data) <Symfony\\ Component\\ VarDumper\\ Dumper\\ DataDumperInterface::dump> `
@@ -173,7 +186,7 @@ Casters
173
186
174
187
Objects and resources nested in a PHP variable are "cast" to arrays in the
175
188
intermediate :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
176
- representation. You can tweak the array representation for each object/resource
189
+ representation. You can customize the array representation for each object/resource
177
190
by hooking a Caster into this process. The component already includes many
178
191
casters for base PHP classes and other common classes.
179
192
@@ -209,17 +222,21 @@ can also be registered for the same resource type/class/interface.
209
222
They are called in registration order.
210
223
211
224
Casters are responsible for returning the properties of the object or resource
212
- being cloned in an array. They are callables that accept four arguments:
225
+ being cloned in an array. They are callables that accept five arguments:
213
226
214
- * the object or resource being casted,
215
- * an array modelled for objects after PHP's native ``(array) `` cast operator,
227
+ * the object or resource being casted;
228
+ * an array modelled for objects after PHP's native ``(array) `` cast operator;
216
229
* a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Stub ` object
217
- representing the main properties of the object (class, type, etc.),
218
- * true/false when the caster is called nested in a structure or not.
230
+ representing the main properties of the object (class, type, etc.);
231
+ * true/false when the caster is called nested in a structure or not;
232
+ * A bit field of :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ Caster`` `::EXCLUDE_*``
233
+ constants.
219
234
220
235
Here is a simple caster not doing anything::
221
236
222
- function myCaster($object, $array, $stub, $isNested)
237
+ use Symfony\Component\VarDumper\Cloner\Stub;
238
+
239
+ function myCaster($object, $array, Stub $stub, $isNested, $filter)
223
240
{
224
241
// ... populate/alter $array to your needs
225
242
@@ -246,3 +263,50 @@ properties not in the class declaration).
246
263
.. tip ::
247
264
248
265
Before writing your own casters, you should check the existing ones.
266
+
267
+ Adding Semantics with Metadata
268
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269
+
270
+ .. versionadded :: 3.2
271
+ As of Symfony 3.2, casters can attach metadata attributes to
272
+ :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Stub ` objects to inform
273
+ dumpers about the precise type of the dumped values.
274
+
275
+ Since casters are hooked on specific classes or interfaces, they know about the
276
+ objects they manipulate. By altering the ``$stub `` object (the third argument of
277
+ any caster), one can transfer this knowledge to the resulting ``Data `` object,
278
+ thus to dumpers. To help you do this (see the source code for how it works),
279
+ the component comes with a set of wrappers for common additional semantics. You
280
+ can use:
281
+
282
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ ConstStub ` to wrap a value that is
283
+ best represented by a PHP constant;
284
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ ClassStub ` to wrap a PHP identifier
285
+ (*i.e. * a class name, a method name, an interface, *etc. *);
286
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ CutStub ` to replace big noisy
287
+ objects/strings/*etc. * by ellipses;
288
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ CutArrayStub ` to keep only some
289
+ useful keys of an array;
290
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ EnumStub ` to wrap a set of virtual
291
+ values (*i.e. * values that do not exist as properties in the original PHP data
292
+ structure, but are worth listing alongside with real ones);
293
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ LinkStub ` to wrap strings that can
294
+ be turned into links by dumpers;
295
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ TraceStub ` and their
296
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ FrameStub ` and
297
+ * :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ ArgsStub ` relatives to wrap PHP
298
+ traces (used by :class: `Symfony\\ Component\\ VarDumper\\ Caster\\ ExceptionCaster `).
299
+
300
+ For example, if you know that your ``Product `` objects have a ``brochure `` property
301
+ that holds a file name or a URL, you can wrap them in a ``LinkStub `` to tell
302
+ ``HtmlDumper `` to make them clickable::
303
+
304
+ use Symfony\Component\VarDumper\Caster\LinkStub;
305
+ use Symfony\Component\VarDumper\Cloner\Stub;
306
+
307
+ function ProductCaster(Product $object, $array, Stub $stub, $isNested, $filter = 0)
308
+ {
309
+ $array['brochure'] = new LinkStub($array['brochure']);
310
+
311
+ return $array;
312
+ }
0 commit comments