TestUserApiResource.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Fixtures;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. class TestUserApiResource extends JsonResource
  5. {
  6. /**
  7. * Transform the resource into an array.
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. *
  11. * @return array
  12. */
  13. public function toArray($request)
  14. {
  15. $result = [
  16. 'id' => $this->id,
  17. 'name' => $this->first_name . ' ' . $this->last_name,
  18. 'email' => $this->email,
  19. 'children' => $this->whenLoaded('children', function () {
  20. return TestUserApiResource::collection($this->children);
  21. }),
  22. 'pets' => $this->whenLoaded('pets', function () {
  23. return TestPetApiResource::collection($this->pets);
  24. }),
  25. ];
  26. if($request->route()->named('someone')) {
  27. return ['someone' => true];
  28. }
  29. if ($this['state1'] && $this['random-state']) {
  30. $result['state1'] = $this['state1'];
  31. $result['random-state'] = $this['random-state'];
  32. }
  33. return $result;
  34. }
  35. }