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