TestUserApiResource.php 1.2 KB

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