Skip to content

Commit 4f26755

Browse files
committed
docs(app): Additional docs fixes
1 parent 5da3cbf commit 4f26755

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

user_guide_src/source/outgoing/api_transformers.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ Quick Example
1919

2020
The following example shows a common usage pattern for transformers in your application.
2121

22-
.. literalinclude:: api_resources/001.php
22+
.. literalinclude:: api_transformers/001.php
2323

2424
In this example, the ``UserTransformer`` defines which fields from a user entity should be included in the
2525
API response. The ``transform()`` method converts a single resource, while ``transformMany()`` handles
2626
collections of resources.
2727

28-
******************
28+
**********************
2929
Creating a Transformer
30-
******************
30+
**********************
3131

3232
To create a transformer, extend the ``BaseTransformer`` class and implement the ``toArray()`` method to
3333
define your API resource structure. The ``toArray()`` method receives the resource being transformed as
@@ -36,7 +36,7 @@ a parameter, allowing you to access and transform its data.
3636
Basic Transformer
3737
=================
3838

39-
.. literalinclude:: api_resources/002.php
39+
.. literalinclude:: api_transformers/002.php
4040

4141
The ``toArray()`` method receives the resource (entity, array, or object) as its parameter and defines
4242
the structure of your API response. You can include any fields you want from the resource, and you can
@@ -98,7 +98,7 @@ Using Transformers in Controllers
9898
Once you've created a transformer, you can use it in your controllers to transform data before returning
9999
it to the client.
100100

101-
.. literalinclude:: api_resources/003.php
101+
.. literalinclude:: api_transformers/003.php
102102

103103
***********************
104104
Field Filtering
@@ -107,7 +107,7 @@ Field Filtering
107107
The transformer automatically supports field filtering through the ``fields`` query parameter of the current URL.
108108
This allows API clients to request only specific fields they need, reducing bandwidth and improving performance.
109109

110-
.. literalinclude:: api_resources/007.php
110+
.. literalinclude:: api_transformers/007.php
111111

112112
A request to ``/users/1?fields=id,name`` would return only:
113113

@@ -124,14 +124,14 @@ Restricting Available Fields
124124
By default, clients can request any field defined in your ``toArray()`` method. You can restrict which
125125
fields are allowed by overriding the ``getAllowedFields()`` method:
126126

127-
.. literalinclude:: api_resources/008.php
127+
.. literalinclude:: api_transformers/008.php
128128

129129
Now, even if a client requests ``/users/1?fields=email``, an ``ApiException`` will be thrown because
130130
``email`` is not in the allowed fields list.
131131

132-
***********************
132+
***************************
133133
Including Related Resources
134-
***********************
134+
***************************
135135

136136
Transformers support loading of related resources through the ``include`` query parameter. This
137137
follows a common API pattern where clients can specify which relationships they want included.
@@ -145,7 +145,7 @@ To support including related resources, create methods prefixed with ``include``
145145
resource name. Inside these methods, you can access the current resource being transformed via
146146
``$this->resource``:
147147

148-
.. literalinclude:: api_resources/009.php
148+
.. literalinclude:: api_transformers/009.php
149149

150150
Note how the include methods use ``$this->resource['id']`` to access the ID of the user being transformed.
151151
The ``$this->resource`` property is automatically set by the transformer when ``transform()`` is called.
@@ -180,11 +180,11 @@ Restricting Available Includes
180180
Similar to field filtering, you can restrict which relationships can be included by overriding the
181181
``getAllowedIncludes()`` method:
182182

183-
.. literalinclude:: api_resources/010.php
183+
.. literalinclude:: api_transformers/010.php
184184

185185
If you want to disable all includes, return an empty array:
186186

187-
.. literalinclude:: api_resources/011.php
187+
.. literalinclude:: api_transformers/011.php
188188

189189
Include Validation
190190
==================
@@ -202,20 +202,20 @@ And your transformer doesn't have an ``includeInvalid()`` method, an exception w
202202

203203
This helps catch typos and prevents unexpected behavior.
204204

205-
***********************
205+
************************
206206
Transforming Collections
207-
***********************
207+
************************
208208

209209
The ``transformMany()`` method makes it easy to transform arrays of resources:
210210

211-
.. literalinclude:: api_resources/012.php
211+
.. literalinclude:: api_transformers/012.php
212212

213213
The ``transformMany()`` method applies the same transformation logic to each item in the collection,
214214
including any field filtering or includes specified in the request.
215215

216-
***********************
216+
*********************************
217217
Working with Different Data Types
218-
***********************
218+
*********************************
219219

220220
Transformers can handle various data types, not just entities.
221221

@@ -225,28 +225,28 @@ Transforming Entities
225225
When you pass an ``Entity`` instance to ``transform()``, it automatically calls the entity's ``toArray()``
226226
method to get the data:
227227

228-
.. literalinclude:: api_resources/013.php
228+
.. literalinclude:: api_transformers/013.php
229229

230230
Transforming Arrays
231231
===================
232232

233233
You can transform plain arrays as well:
234234

235-
.. literalinclude:: api_resources/014.php
235+
.. literalinclude:: api_transformers/014.php
236236

237237
Transforming Objects
238238
====================
239239

240240
Any object can be cast to an array and transformed:
241241

242-
.. literalinclude:: api_resources/015.php
242+
.. literalinclude:: api_transformers/015.php
243243

244244
Using toArray() Only
245245
====================
246246

247247
If you don't pass a resource to ``transform()``, it will use the data from your ``toArray()`` method:
248248

249-
.. literalinclude:: api_resources/016.php
249+
.. literalinclude:: api_transformers/016.php
250250

251251
***************
252252
Class Reference
@@ -272,7 +272,7 @@ Class Reference
272272
The resource parameter contains the data being transformed. Return an array with the fields you want
273273
to include in the API response, accessing data from the ``$resource`` parameter.
274274

275-
.. literalinclude:: api_resources/017.php
275+
.. literalinclude:: api_transformers/017.php
276276

277277
.. php:method:: transform($resource = null)
278278
@@ -288,7 +288,7 @@ Class Reference
288288

289289
The method automatically applies field filtering and includes based on query parameters.
290290

291-
.. literalinclude:: api_resources/018.php
291+
.. literalinclude:: api_transformers/018.php
292292

293293
.. php:method:: transformMany(array $resources)
294294
@@ -299,7 +299,7 @@ Class Reference
299299
Transforms a collection of resources by calling ``transform()`` on each item. Field filtering and
300300
includes are applied consistently to all items.
301301

302-
.. literalinclude:: api_resources/019.php
302+
.. literalinclude:: api_transformers/019.php
303303

304304
.. php:method:: getAllowedFields()
305305
@@ -310,7 +310,7 @@ Class Reference
310310
Return ``null`` (the default) to allow all fields from ``toArray()``. Return an array of field names
311311
to create a whitelist of allowed fields.
312312

313-
.. literalinclude:: api_resources/022.php
313+
.. literalinclude:: api_transformers/022.php
314314

315315
.. php:method:: getAllowedIncludes()
316316
@@ -321,7 +321,7 @@ Class Reference
321321
parameter. Return ``null`` (the default) to allow all includes that have corresponding methods.
322322
Return an array of include names to create a whitelist. Return an empty array to disable all includes.
323323

324-
.. literalinclude:: api_resources/023.php
324+
.. literalinclude:: api_transformers/023.php
325325

326326
*******************
327327
Exception Reference

0 commit comments

Comments
 (0)