-
Notifications
You must be signed in to change notification settings - Fork 0
Move CreateJsonResource from testcase to custom trait #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brdv
wants to merge
2
commits into
main
Choose a base branch
from
feature/create-jsonresourcehelper-trait
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| <?php | ||
|
|
||
| namespace Brainstud\JsonApi\Traits; | ||
|
|
||
| use Illuminate\Database\Eloquent\Collection; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Support\Str; | ||
|
|
||
| /** | ||
| * JsonResourceHelper | ||
| * | ||
| * Add this trait to a (test) class to add JsonResource helper to it. | ||
| */ | ||
| trait JsonResourceHelper | ||
| { | ||
| /** | ||
| * createJsonResource. | ||
| * | ||
| * Makes it easier to create an array structure of an API resource to prevent being repetitive. | ||
| */ | ||
| protected function createJsonResource( | ||
| Collection|Model $modelOrCollection, | ||
| array $relationships = null, | ||
| array $meta = null, | ||
| array $links = null, | ||
| string $type = null, | ||
| array $exceptAttributes = [], | ||
| array $onlyAttributes = [], | ||
| bool $isReferenceObject = false, | ||
| ): array { | ||
| $type ??= ($modelOrCollection instanceof Collection) | ||
| ? Str::snake(Str::plural(class_basename($modelOrCollection[0]))) | ||
| : Str::snake(Str::plural(class_basename($modelOrCollection))); | ||
|
|
||
| if ($modelOrCollection instanceof Collection) { | ||
| return $modelOrCollection->map(fn ($model) => ( | ||
| $this->createJsonResource( | ||
| $model, | ||
| $relationships, | ||
| type: $type, | ||
| exceptAttributes: $exceptAttributes, | ||
| isReferenceObject: $isReferenceObject, | ||
| ) | ||
| ))->toArray(); | ||
| } elseif ($isReferenceObject) { | ||
| $data = [ | ||
| 'id' => $modelOrCollection->identifier, | ||
| 'type' => $type, | ||
| ]; | ||
| } else { | ||
| $fillableAttributes = $modelOrCollection->getFillable(); | ||
| $data = [ | ||
| 'id' => $modelOrCollection->identifier, | ||
| 'type' => $type, | ||
| 'attributes' => ( | ||
| array_filter( | ||
| $this->getAllAttributes($modelOrCollection), | ||
| fn ($key) => ( | ||
| in_array($key, $fillableAttributes) | ||
| && (empty($onlyAttributes) || in_array($key, $onlyAttributes)) | ||
| && !in_array($key, ['identifier', ...$exceptAttributes]) | ||
| ), | ||
| ARRAY_FILTER_USE_KEY | ||
| ) | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| if (!$isReferenceObject && $relationships) { | ||
| $data['relationships'] = collect(array_keys($relationships))->reduce(function ($rels, $relationKey) use ($relationships) { | ||
| $relation = $relationships[$relationKey]; | ||
| if ($relation instanceof Model) { | ||
| $rels[$relationKey] = [ | ||
| 'data' => $this->createJsonResource($relation, isReferenceObject: true), | ||
| ]; | ||
| } else { | ||
| $rels[$relationKey] = [ | ||
| 'data' => collect($relation)->map(fn ($relatedModel) => ( | ||
| $this->createJsonResource($relatedModel, isReferenceObject: true) | ||
| )), | ||
| ]; | ||
| } | ||
|
|
||
| return $rels; | ||
| }, []); | ||
| } | ||
|
|
||
| if ($meta) { | ||
| $data['meta'] = $meta; | ||
| } | ||
|
|
||
| if ($links) { | ||
| $data['links'] = $links; | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * By default, laravel does not add columns where the value is null | ||
| * to the retrieved model. Therefore, we combine the received and | ||
| * fillable fields on the model. If a field is fillable, but not | ||
| * on the given model, it adds it to the attributes array with | ||
| * a value of null. | ||
| * | ||
| */ | ||
| private function getAllAttributes(Model $model): array | ||
| { | ||
| $columns = $model->getFillable(); | ||
|
|
||
| $attributes = $model->getAttributes(); | ||
|
|
||
| foreach ($columns as $column) { | ||
| if (!array_key_exists($column, $attributes)) { | ||
| $attributes[$column] = null; | ||
| } | ||
| } | ||
|
|
||
| return $attributes; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.