Skip to content

Commit b86320d

Browse files
committed
(app): Remove when / whenNot from BaseTransformer
1 parent 10796a6 commit b86320d

File tree

8 files changed

+0
-289
lines changed

8 files changed

+0
-289
lines changed

system/API/BaseTransformer.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
* 'email' => $resource['email'],
4444
* 'created_at' => $resource['created_at'],
4545
* 'updated_at' => $resource['updated_at'],
46-
* 'bio' => $this->when(($resource['bio'] ?? null) !== null, $resource['bio'] ?? null),
4746
* ];
4847
* }
4948
*
@@ -126,32 +125,6 @@ public function transformMany(array $resources): array
126125
return array_map(fn ($resource): array => $this->transform($resource), $resources);
127126
}
128127

129-
/**
130-
* Conditionally include a value.
131-
*
132-
* @param mixed $value
133-
* @param mixed $default
134-
*
135-
* @return mixed
136-
*/
137-
protected function when(bool $condition, $value, $default = null)
138-
{
139-
return $condition ? $value : $default;
140-
}
141-
142-
/**
143-
* Conditionally exclude a value.
144-
*
145-
* @param mixed $value
146-
* @param mixed|null $default
147-
*
148-
* @return mixed
149-
*/
150-
protected function whenNot(bool $condition, $value, $default = null)
151-
{
152-
return $condition ? $default : $value;
153-
}
154-
155128
/**
156129
* Define which fields can be requested via the 'fields' query parameter.
157130
* Override in child classes to restrict available fields.

tests/system/API/TransformerTest.php

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -193,120 +193,6 @@ public function toArray(mixed $resource): array
193193
$this->assertSame([], $result);
194194
}
195195

196-
public function testWhenConditionIsTrue(): void
197-
{
198-
$request = $this->createMockRequest();
199-
200-
$transformer = new class ($request) extends BaseTransformer {
201-
public function toArray(mixed $resource): array
202-
{
203-
return [
204-
'id' => 1,
205-
'name' => $this->when(true, 'Visible Name'),
206-
];
207-
}
208-
};
209-
210-
$result = $transformer->transform(null);
211-
212-
$this->assertSame(['id' => 1, 'name' => 'Visible Name'], $result);
213-
}
214-
215-
public function testWhenConditionIsFalse(): void
216-
{
217-
$request = $this->createMockRequest();
218-
219-
$transformer = new class ($request) extends BaseTransformer {
220-
public function toArray(mixed $resource): array
221-
{
222-
return [
223-
'id' => 1,
224-
'name' => $this->when(false, 'Visible Name'),
225-
];
226-
}
227-
};
228-
229-
$result = $transformer->transform(null);
230-
231-
$this->assertSame(['id' => 1, 'name' => null], $result);
232-
}
233-
234-
public function testWhenConditionIsFalseWithDefault(): void
235-
{
236-
$request = $this->createMockRequest();
237-
238-
$transformer = new class ($request) extends BaseTransformer {
239-
public function toArray(mixed $resource): array
240-
{
241-
return [
242-
'id' => 1,
243-
'name' => $this->when(false, 'Visible Name', 'Default Name'),
244-
];
245-
}
246-
};
247-
248-
$result = $transformer->transform(null);
249-
250-
$this->assertSame(['id' => 1, 'name' => 'Default Name'], $result);
251-
}
252-
253-
public function testWhenNotConditionIsTrue(): void
254-
{
255-
$request = $this->createMockRequest();
256-
257-
$transformer = new class ($request) extends BaseTransformer {
258-
public function toArray(mixed $resource): array
259-
{
260-
return [
261-
'id' => 1,
262-
'name' => $this->whenNot(true, 'Visible Name'),
263-
];
264-
}
265-
};
266-
267-
$result = $transformer->transform(null);
268-
269-
$this->assertSame(['id' => 1, 'name' => null], $result);
270-
}
271-
272-
public function testWhenNotConditionIsFalse(): void
273-
{
274-
$request = $this->createMockRequest();
275-
276-
$transformer = new class ($request) extends BaseTransformer {
277-
public function toArray(mixed $resource): array
278-
{
279-
return [
280-
'id' => 1,
281-
'name' => $this->whenNot(false, 'Visible Name'),
282-
];
283-
}
284-
};
285-
286-
$result = $transformer->transform(null);
287-
288-
$this->assertSame(['id' => 1, 'name' => 'Visible Name'], $result);
289-
}
290-
291-
public function testWhenNotConditionIsTrueWithDefault(): void
292-
{
293-
$request = $this->createMockRequest();
294-
295-
$transformer = new class ($request) extends BaseTransformer {
296-
public function toArray(mixed $resource): array
297-
{
298-
return [
299-
'id' => 1,
300-
'name' => $this->whenNot(true, 'Visible Name', 'Default Name'),
301-
];
302-
}
303-
};
304-
305-
$result = $transformer->transform(null);
306-
307-
$this->assertSame(['id' => 1, 'name' => 'Default Name'], $result);
308-
}
309-
310196
public function testLimitFieldsWithNoFieldsParam(): void
311197
{
312198
$request = $this->createMockRequest();

user_guide_src/source/outgoing/api_transformers.rst

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -100,37 +100,6 @@ it to the client.
100100

101101
.. literalinclude:: api_resources/003.php
102102

103-
***********************
104-
Conditional Fields
105-
***********************
106-
107-
Often you'll want to include certain fields only under specific conditions. The ``when()`` and ``whenNot()``
108-
methods make this easy.
109-
110-
Using when()
111-
============
112-
113-
The ``when()`` method includes a value only if the condition is true:
114-
115-
.. literalinclude:: api_resources/004.php
116-
117-
In this example, the ``bio`` field will only be included if it has a non-null value. If the condition is
118-
false, the field will be set to ``null`` by default.
119-
120-
Providing a Default Value
121-
==========================
122-
123-
You can provide a default value to use when the condition is false:
124-
125-
.. literalinclude:: api_resources/005.php
126-
127-
Using whenNot()
128-
===============
129-
130-
The ``whenNot()`` method is the opposite of ``when()`` - it includes a value only if the condition is false:
131-
132-
.. literalinclude:: api_resources/006.php
133-
134103
***********************
135104
Field Filtering
136105
***********************
@@ -332,32 +301,6 @@ Class Reference
332301

333302
.. literalinclude:: api_resources/019.php
334303

335-
.. php:method:: when(bool $condition, $value, $default = null)
336-
337-
:param bool $condition: The condition to evaluate
338-
:param mixed $value: The value to return if the condition is true
339-
:param mixed $default: The value to return if the condition is false (defaults to ``null``)
340-
:returns: The value or default based on the condition
341-
:rtype: mixed
342-
343-
Helper method to conditionally include a value in the transformation. Returns ``$value`` if
344-
``$condition`` is true, otherwise returns ``$default``.
345-
346-
.. literalinclude:: api_resources/020.php
347-
348-
.. php:method:: whenNot(bool $condition, $value, $default = null)
349-
350-
:param bool $condition: The condition to evaluate
351-
:param mixed $value: The value to return if the condition is false
352-
:param mixed $default: The value to return if the condition is true (defaults to ``null``)
353-
:returns: The value or default based on the condition
354-
:rtype: mixed
355-
356-
Helper method to conditionally exclude a value. Returns ``$value`` if ``$condition`` is false,
357-
otherwise returns ``$default``.
358-
359-
.. literalinclude:: api_resources/021.php
360-
361304
.. php:method:: getAllowedFields()
362305
363306
:returns: Array of allowed field names, or ``null`` to allow all fields

user_guide_src/source/outgoing/api_transformers/004.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

user_guide_src/source/outgoing/api_transformers/005.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

user_guide_src/source/outgoing/api_transformers/006.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

user_guide_src/source/outgoing/api_transformers/020.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

user_guide_src/source/outgoing/api_transformers/021.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)