Skip to content

Commit 420e569

Browse files
committed
doc: update readme.md and changelog
1 parent 6d02e20 commit 420e569

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

CHANGELOG-1.2.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Release note
2+
============
3+
4+
# v1.2.0
5+
### Added
6+
- Implement described notation
7+
- Implement `applyWhen`, Unlike `mergeWhen`, `applyWhen` keeps the keys even when the condition is not met.
8+
9+
### Breaking change
10+
- `Resources\Concerns\Identifier::toType`: by default return type computed from resource class insteadof model class

readme.md

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,34 @@ protected function toAttributes(Request $request): array
189189
// Conditional attribute
190190
'secret' => $this->when($request->user()->isAdmin(), 'secret-value'),
191191
// Merging Conditional Attributes
192-
$this->mergeWhen($request->user()->isAdmin(), [
193-
'first-secret' => 'value',
194-
'second-secret' => 'value',
192+
// use applyWhen insteadof mergeWhen for keep fields
193+
// useful for fields request rules validation
194+
$this->applyWhen($request->user()->isAdmin(), [
195+
'first-secret' => 123,
196+
'second-secret' => 456.789,
197+
]),
198+
];
199+
}
200+
```
201+
202+
#### Described attributes
203+
_**@see** [described notation](##described-notation)_
204+
205+
```php
206+
protected function toAttributes(Request $request): array
207+
{
208+
return [
209+
'name' => $this->string(),
210+
// pass key to describer
211+
$this->string('email'),
212+
// with lazy evaluation
213+
'hash64' => $this->string(fn() => base64_encode("{$this->id}-{$this->email}")),
214+
// Conditional attribute
215+
$this->string('secret')->when($request->user()->isAdmin(), 'secret-value'),
216+
// Merging Conditional Attributes
217+
$this->applyWhen($request->user()->isAdmin(), [
218+
'first-secret' => $this->integer(fn() => 123),
219+
'second-secret' => $this->float(fn() => 456.789),
195220
]),
196221
];
197222
}
@@ -242,7 +267,34 @@ protected function toRelationships(Request $request): array
242267
// as collection, with condition
243268
'comments' => CommentResource::relationship(fn() => $this->whenLoaded('comments'))->asCollection(),
244269
// with relationship (allow to include links and meta on relation)
245-
'posts' => PostResource::relationship(fn() => $this->posts)->asCollection(),
270+
'posts' => PostResource::relationship(fn() => $this->posts)
271+
->asCollection(),
272+
];
273+
}
274+
```
275+
276+
#### Described attributes
277+
_**@see** [described notation](##described-notation)_
278+
279+
```php
280+
protected function toRelationships(Request $request): array
281+
{
282+
return [
283+
'avatar' => $this->one(AvatarResource::class),
284+
// custom relation name
285+
'my-avatar' => $this->one(AvatarResource::class, 'avatar'),
286+
// as collection, with condition
287+
'comments' => $this->many(CommentResource::class)
288+
->whenLoaded(),
289+
// with relationship (allow to include links and meta on relation)
290+
'posts' => $this->many(PostResource::class)
291+
->links(fn() => [
292+
'self' => "https://api.example.com/posts/{$this->resource->id}/relationships/posts",
293+
'related' => "https://api.example.com/posts/{$this->resource->id}/posts",
294+
])
295+
->meta(fn() => [
296+
'total' => $this->integer(fn() => $this->resource->posts()->count()),
297+
]),
246298
];
247299
}
248300
```
@@ -324,3 +376,21 @@ Usage is the same as laravel collections.
324376
```php
325377
UserResource::collection(User::all()); // => JsonApiCollection
326378
```
379+
380+
381+
## Described notation
382+
383+
### Value methods
384+
| Method | Description |
385+
|-----------|--------------------------|
386+
| `bool` | Cast to boolean |
387+
| `integer` | Cast to integer |
388+
| `float` | Cast to float |
389+
| `array` | Cast to array |
390+
| `mixed` | Don't cast, return as is |
391+
392+
### Relation methods
393+
| Method | Description |
394+
|---------|-------------------------------------------------------------------|
395+
| `one` | For relationship with a single value: `HasOne`, `BelongsTo`, ... |
396+
| `many` | For relationship with many value: `HasMany`, `BelongsToMany`, ... |

0 commit comments

Comments
 (0)