CommonDetailResponse.php 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * 通用详情返回封装.
  4. *
  5. * @author nj
  6. * @date 2021-01-24 02:20:30
  7. */
  8. namespace Modules\ManageApi\Responses;
  9. use Illuminate\Contracts\Support\Arrayable;
  10. use Illuminate\Contracts\Support\Responsable;
  11. class CommonDetailResponse implements Responsable
  12. {
  13. /**
  14. * 录入的数据.
  15. *
  16. * @var mixed
  17. */
  18. private $data = null;
  19. /**
  20. * 附加条件参数.
  21. *
  22. * @var array
  23. */
  24. private $params = [];
  25. public function __construct($data = null, $params = [])
  26. {
  27. $this->data = $data;
  28. $this->params = $params;
  29. }
  30. public function toResponse(
  31. $request = null
  32. )
  33. {
  34. if (empty($this->data)) {
  35. return json_fail(trans('common.err_resource_not_found'));
  36. }
  37. if (is_array($this->data)) {
  38. $result = $this->data;
  39. } else {
  40. $result = $this->data->toArray();
  41. }
  42. return json_success($result);
  43. }
  44. }