@@ -34,8 +34,7 @@ A cloner is used to create an intermediate representation of any PHP variable.
3434Its output is a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
3535object that wraps this representation.
3636
37- You can create a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
38- object this way::
37+ You can create a ``Data `` object this way::
3938
4039 use Symfony\Component\VarDumper\Cloner\VarCloner;
4140
@@ -45,8 +44,11 @@ object this way::
4544 // see the example at the top of this page
4645 // $dumper->dump($data);
4746
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.
5052Before calling :method: `Symfony\\ Component\\ VarDumper\\ Cloner\\ VarCloner::cloneVar `,
5153you can configure these limits:
5254
@@ -160,6 +162,17 @@ Another option for doing the same could be::
160162
161163 // $output is now populated with the dump representation of $variable
162164
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+
163176Dumpers implement the :class: `Symfony\\ Component\\ VarDumper\\ Dumper\\ DataDumperInterface `
164177interface that specifies the
165178:method: `dump(Data $data) <Symfony\\ Component\\ VarDumper\\ Dumper\\ DataDumperInterface::dump> `
@@ -173,7 +186,7 @@ Casters
173186
174187Objects and resources nested in a PHP variable are "cast" to arrays in the
175188intermediate :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
177190by hooking a Caster into this process. The component already includes many
178191casters for base PHP classes and other common classes.
179192
@@ -209,17 +222,21 @@ can also be registered for the same resource type/class/interface.
209222They are called in registration order.
210223
211224Casters 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:
213226
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;
216229* 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.
219234
220235Here is a simple caster not doing anything::
221236
222- function myCaster($object, $array, $stub, $isNested)
237+ use Symfony\Component\VarDumper\Cloner\Stub;
238+
239+ function myCaster($object, $array, Stub $stub, $isNested, $filter)
223240 {
224241 // ... populate/alter $array to your needs
225242
@@ -246,3 +263,50 @@ properties not in the class declaration).
246263.. tip ::
247264
248265 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