Skip to content

Commit a2f48cc

Browse files
committed
docs: Add better Output docs, and include toCollection()
1 parent a6d208a commit a2f48cc

File tree

6 files changed

+82
-5
lines changed

6 files changed

+82
-5
lines changed

docs/.vitepress/config.mts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export default withMermaid({
6767
{ text: 'Transformers', link: '/transformers' },
6868
{ text: 'Validation', link: '/validation' },
6969
{ text: 'Computed Properties', link: '/computed-properties' },
70+
{ text: 'Output', link: '/output' },
7071
{ text: 'Wrapping', link: '/wrapping' },
7172
{ text: 'Factories / Testing', link: '/testing' },
7273
]

docs/casting.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ or simply transform it in some way. For example, you can cast a number to a `\Br
99

1010
Casts can act on both input and output, depending on if they implement `CastsPropertySet` or `CastsPropertyGet`.
1111

12-
Output casts are applied when calling `toArray()`, or when serializing to JSON.
12+
Output casts are applied when calling `toArray()`, `toCollection()`, or when serializing to JSON.
1313

1414
The following annotations are available:
1515

@@ -50,7 +50,7 @@ When you access the `dateOfBirth` property directly, it will be a `\Carbon\Carbo
5050
dump($value->dateOfBirth); // CarbonImmutable
5151
```
5252

53-
However, if you call `toArray()` then it will be formatted using the format you specified:
53+
However, if you call `toArray()` or `toCollection()` then it will be formatted using the format you specified:
5454

5555
```php
5656
dump($value->toArray()); // ['name' => 'Davey Shafik', 'age' => 40, 'dateOfBirth' => '1984-05-31']

docs/hidden.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MyValue extends Bag {
2323
}
2424
```
2525

26-
In the above example, the `hiddenProperty` will not be included when calling `toArray()` or `json_encode()`:
26+
In the above example, the `hiddenProperty` will not be included when calling `toArray()`, `toCollection()` or `json_encode()`:
2727

2828
```php
2929
$value = MyValue::from([

docs/how-bag-works.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The `OutputPipeline` is responsible transforming the Bag data to the desired out
6868
```mermaid
6969
graph TD;
7070
toArray("Bag->toArray()") --> processParameters
71+
toCollection("Bag->toCollection()") --> processParameters
7172
toJson("Bag->toJson()") --> processParameters
7273
jsonEncode("json_encode($bag)") --> processParameters
7374
get("Bag->get()") --> processParameters
@@ -82,11 +83,12 @@ processParameters(Process Parameters)
8283
--> wrap(Wrap Output*)
8384
--> output(array or JSON string)
8485
85-
class toArray,toJson,jsonEncode,get,unwrapped mermaid-start
86+
class toArray,toCollection,toJson,jsonEncode,get,unwrapped mermaid-start
8687
class output mermaid-end
8788
class hide,hideJson,wrap mermaid-conditional
8889
8990
click toArray "https://github.com/dshafik/bag/blob/main/src/Bag/Concerns/WithArrayable.php" _blank
91+
click toCollection "https://github.com/dshafik/bag/blob/main/src/Bag/Concerns/WithCollections.php" _blank
9092
click toJson "https://github.com/dshafik/bag/blob/main/src/Bag/Concerns/WithJson.php" _blank
9193
click get "https://github.com/dshafik/bag/blob/main/src/Bag/Concerns/WithOutput.php" _blank
9294
click unwrapped "https://github.com/dshafik/bag/blob/main/src/Bag/Concerns/WithOutput.php" _blank
@@ -138,6 +140,50 @@ click extra "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Ex
138140
click validate "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Validate.php" _blank
139141
```
140142

143+
## The Without Validate Pipeline
144+
145+
The [`WithoutValidationPipeline`](https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/WithoutValidationPipeline.php) is identical to the `InputPipeline` but does not perform validation. The pipeline
146+
consists of the following steps:
147+
148+
```mermaid
149+
graph TD;
150+
start("Bag::from($data)")
151+
--> transform(Transform Input)
152+
--> process(Process Parameters)
153+
--> variadic(Is Variadic?)
154+
--> mapInput(Map Input)
155+
--> laravelParams(Laravel Route Parameter Binding)
156+
-- Finalized Input Values --> missing(Missing Parameters) --> missingError{Error?}
157+
missingError -- Yes --> errorMissingParameters(MissingPropertiesException)
158+
missingError -- No --> extra(Extra Parameters) --> extraError{Error?}
159+
extraError -- Yes --> errorExtraParameters(ExtraPropertiesException)
160+
extraError -- No
161+
--> construct("new Bag(...)")
162+
--> computed(Verify Computed Values)
163+
--> initialized{Initialized?}
164+
initialized -- No --> errorInitialization(ComputedPropertyUninitializedException)
165+
initialized -- Yes
166+
--> bag(Bag Value Returned)
167+
168+
class start mermaid-start
169+
class missingError,extraError,valid,initialized mermaid-decision
170+
class bag mermaid-end
171+
class errorMissingParameters,errorExtraParameters,errorValidation,errorInitialization mermaid-error
172+
173+
click start "https://github.com/dshafik/bag/blob/main/src/Bag/Bag.php" _blank
174+
click transform "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Transform.php" _blank
175+
click process "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ProcessParameters.php" _blank
176+
click variadic "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/IsVariadic.php" _blank
177+
click mapInput "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/MapInput.php" _blank
178+
click laravelParams "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/LaravelRouteParameters.php" _blank
179+
click missing "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/MissingParameters.php" _blank
180+
click extra "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ExtraParameters.php" _blank
181+
click validate "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/Validate.php" _blank
182+
click cast "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/CastInputValues.php" _blank
183+
click construct "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/FillBag.php" _blank
184+
click computed "https://github.com/dshafik/bag/blob/main/src/Bag/Pipelines/Pipes/ComputedValues.php" _blank
185+
```
186+
141187
## The Output Collection Pipeline
142188

143189
The `OutputCollectionPipeline` is responsible for transforming the Bag collection data to the desired output array or JSON. The pipeline consists of the following steps:

docs/mapping.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Input mapping is applied when calling `Bag::from()` or `Bag::withoutValidation()
130130
> [!TIP]
131131
> [Validation](validation) is applied to the original property name, not the mapped name.
132132
133-
Output mapping is applied when calling `$Bag->toArray()` or `$Bag->toJson()` (or when using `json_encode()`).
133+
Output mapping is applied when calling `$Bag->toArray()`, `$Bag->toCollection()`, or `$Bag->toJson()` (or when using `json_encode()`).
134134

135135
## Mapping Hierarchy
136136

docs/output.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Output
2+
3+
Once you have created a Bag value object, you can access the properties using object notation:
4+
5+
```php
6+
use App\Values\MyValue;
7+
8+
$value = new MyValue(name: 'Davey Shafik', age: 40);
9+
$value->name; // 'Davey Shafik'
10+
```
11+
12+
## Casting to Other Types
13+
14+
Bag supports casting to arrays and [`\Bag\Collections`](/collections##extending-laravel-collections) using the
15+
`Bag->toArray()` and `Bag->toCollection()` methods.
16+
17+
Both methods will apply casting and mapping, as well as respect [hidden properties](/hidden).
18+
19+
## JSON Serialization
20+
21+
In addition, you can serialize a Bag object to JSON using `json_encode()` or `Bag->toJson():
22+
23+
```php
24+
$value = MyValue::from(name: 'Davey Shafik', age: 40);
25+
26+
$value->toJson(); // {"name": "Davey Shafik", "age": 40}
27+
json_encode($value, JSON_THROW_ON_ERROR); // {"name": "Davey Shafik", "age": 40}
28+
```
29+
30+
Both `Bag->toJson()` and `json_encode()` will respect [hidden properties](/hidden).

0 commit comments

Comments
 (0)