akalongman /
laravel-lodash
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Longman\LaravelLodash\Http\Resources; |
||
| 6 | |||
| 7 | use Exception; |
||
| 8 | use Illuminate\Http\JsonResponse; |
||
| 9 | use Illuminate\Http\Resources\Json\JsonResource as BaseResource; |
||
| 10 | use Longman\LaravelLodash\Eloquent\UuidAsPrimaryContract; |
||
| 11 | use Longman\LaravelLodash\Http\Resources\Response\ResourceResponse; |
||
| 12 | use Longman\LaravelLodash\Support\Arr; |
||
| 13 | use ReflectionClass; |
||
| 14 | |||
| 15 | use function array_keys; |
||
| 16 | use function array_merge; |
||
| 17 | use function array_merge_recursive; |
||
| 18 | use function is_null; |
||
| 19 | use function method_exists; |
||
| 20 | use function ucfirst; |
||
| 21 | |||
| 22 | abstract class JsonResource extends BaseResource |
||
| 23 | { |
||
| 24 | use TransformsData; |
||
| 25 | |||
| 26 | protected array $includedRelations = []; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 27 | protected string $dataWrapper = 'data'; |
||
| 28 | /** |
||
| 29 | * Default resource type, if resource is not Model |
||
| 30 | */ |
||
| 31 | protected string $resourceType = 'object'; |
||
| 32 | |||
| 33 | 3 | public function getTransformed(): array |
|
| 34 | { |
||
| 35 | 3 | if ($this->resource instanceof TransformableContract) { |
|
| 36 | 3 | return $this->transformToApi($this->resource); |
|
| 37 | } |
||
| 38 | |||
| 39 | return []; |
||
| 40 | } |
||
| 41 | |||
| 42 | 6 | public function toArray($request): array |
|
| 43 | { |
||
| 44 | 6 | $data = $this->getResourceData(); |
|
| 45 | |||
| 46 | 6 | $relationsData = $this->getRelationsData(); |
|
| 47 | |||
| 48 | 6 | if (! empty($relationsData)) { |
|
| 49 | $data['relationships'] = $relationsData; |
||
| 50 | } |
||
| 51 | |||
| 52 | 6 | if (! empty($this->getDataWrapper())) { |
|
| 53 | 2 | return [$this->getDataWrapper() => $data]; |
|
| 54 | } |
||
| 55 | |||
| 56 | 4 | return $data; |
|
| 57 | } |
||
| 58 | |||
| 59 | 6 | public function getDataWrapper(): string |
|
| 60 | { |
||
| 61 | 6 | return $this->dataWrapper; |
|
| 62 | } |
||
| 63 | |||
| 64 | 5 | public function setDataWrapper(string $dataWrapper): void |
|
| 65 | { |
||
| 66 | 5 | $this->dataWrapper = $dataWrapper; |
|
| 67 | 5 | } |
|
| 68 | |||
| 69 | public function withRelations(array $relations): self |
||
| 70 | { |
||
| 71 | $this->includedRelations = array_merge($this->includedRelations, $relations); |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | 3 | public function toResponse($request): JsonResponse |
|
| 77 | { |
||
| 78 | 3 | return (new ResourceResponse($this))->toResponse($request); |
|
| 79 | } |
||
| 80 | |||
| 81 | 2 | public function withResourceType(string $resourceType): self |
|
| 82 | { |
||
| 83 | 2 | $this->resourceType = $resourceType; |
|
| 84 | |||
| 85 | 2 | return $this; |
|
| 86 | } |
||
| 87 | |||
| 88 | public function appendAdditional(array $data): self |
||
| 89 | { |
||
| 90 | $this->additional = array_merge_recursive($this->additional, $data); |
||
| 91 | |||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | 6 | protected function getResourceData(): array |
|
| 96 | { |
||
| 97 | 6 | if ($this->resource instanceof TransformableContract) { |
|
| 98 | $data = [ |
||
| 99 | 3 | 'id' => $this->getResourceId(), |
|
| 100 | 3 | 'type' => $this->getResourceType(), |
|
| 101 | 3 | 'attributes' => $this->getTransformed(), |
|
| 102 | ]; |
||
| 103 | 3 | } elseif (is_null($this->resource)) { |
|
| 104 | $data = [ |
||
| 105 | 'id' => null, |
||
| 106 | 'type' => null, |
||
| 107 | 'attributes' => [], |
||
| 108 | ]; |
||
| 109 | } else { |
||
| 110 | $data = [ |
||
| 111 | 3 | 'id' => $this->getResourceId(), |
|
| 112 | 3 | 'type' => $this->getResourceType(), |
|
| 113 | 3 | 'attributes' => $this->getTransformed(), |
|
| 114 | ]; |
||
| 115 | } |
||
| 116 | |||
| 117 | 6 | return $data; |
|
| 118 | } |
||
| 119 | |||
| 120 | 6 | protected function getRelationsData(): array |
|
| 121 | { |
||
| 122 | 6 | if (is_null($this->resource)) { |
|
| 123 | return []; |
||
| 124 | } |
||
| 125 | |||
| 126 | 6 | $relations = []; |
|
| 127 | |||
| 128 | 6 | $relationsChain = Arr::undot($this->includedRelations); |
|
| 129 | 6 | foreach ($relationsChain as $currentRelation => $remainingRelationChain) { |
|
| 130 | $methodName = 'include' . ucfirst($currentRelation); |
||
| 131 | |||
| 132 | if (! method_exists($this, $methodName)) { |
||
| 133 | throw new Exception('Relation method does not exist: ' . $methodName); |
||
| 134 | } |
||
| 135 | |||
| 136 | $remainingRelations = []; |
||
| 137 | if (! empty($remainingRelationChain)) { |
||
| 138 | $remainingRelations = array_keys(Arr::dot($remainingRelationChain)); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** @var \Longman\LaravelLodash\Http\Resources\JsonResource $resource */ |
||
| 142 | $resource = $this->$methodName(); |
||
| 143 | if (is_null($resource)) { |
||
| 144 | continue; |
||
| 145 | } |
||
| 146 | |||
| 147 | $resource->withRelations($remainingRelations); |
||
| 148 | |||
| 149 | $relations[$currentRelation] = $resource; |
||
| 150 | } |
||
| 151 | |||
| 152 | 6 | return $relations; |
|
| 153 | } |
||
| 154 | |||
| 155 | 6 | protected function getResourceType(): string |
|
| 156 | { |
||
| 157 | 6 | if (! $this->resource instanceof TransformableContract) { |
|
| 158 | 3 | return $this->resourceType; |
|
| 159 | } |
||
| 160 | |||
| 161 | 3 | $reflection = new ReflectionClass($this->resource->getModel()); |
|
| 162 | |||
| 163 | 3 | return $reflection->getShortName(); |
|
| 164 | } |
||
| 165 | |||
| 166 | 6 | protected function getResourceId(): ?string |
|
| 167 | { |
||
| 168 | 6 | if ($this->resource instanceof UuidAsPrimaryContract) { |
|
| 169 | return $this->resource->getUidString(); |
||
| 170 | } |
||
| 171 | |||
| 172 | 6 | if ($this->resource instanceof TransformableContract) { |
|
| 173 | 3 | return (string) $this->resource->getKey(); |
|
| 174 | } |
||
| 175 | |||
| 176 | 3 | return null; |
|
| 177 | } |
||
| 178 | } |
||
| 179 |