TestUserApiResource.php 1.3 KB

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