EnterpriseAddressPresenter.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\miniapi\service;
  4. use app\miniapi\support\PhoneNumber;
  5. use plugin\saiadmin\app\model\water\WaterEnterprise;
  6. use plugin\saiadmin\app\model\water\WaterEnterpriseAddress;
  7. final class EnterpriseAddressPresenter
  8. {
  9. public static function summary(WaterEnterpriseAddress $address, WaterEnterprise $enterprise): array
  10. {
  11. return [
  12. 'id' => (int) $address->id,
  13. 'enterprise_id' => (int) $enterprise->id,
  14. 'name' => $enterprise->name,
  15. 'employee_count' => $enterprise->employee_count === null ? null : (int) $enterprise->employee_count,
  16. 'label' => $address->label,
  17. 'contact' => $address->contact,
  18. 'phone' => PhoneNumber::mask($address->phone),
  19. 'address' => $address->address,
  20. 'region' => $address->region,
  21. 'longitude' => $address->longitude === null ? null : (float) $address->longitude,
  22. 'latitude' => $address->latitude === null ? null : (float) $address->latitude,
  23. 'is_default' => (int) $address->is_default === 1,
  24. 'status' => (int) $address->status,
  25. 'revision' => self::revision($address),
  26. ];
  27. }
  28. public static function detail(WaterEnterpriseAddress $address, WaterEnterprise $enterprise): array
  29. {
  30. return array_replace(self::summary($address, $enterprise), ['phone' => $address->phone]);
  31. }
  32. public static function revision(WaterEnterpriseAddress $address): string
  33. {
  34. return hash('sha256', json_encode($address->only([
  35. 'id',
  36. 'enterprise_id',
  37. 'label',
  38. 'contact',
  39. 'phone',
  40. 'region',
  41. 'address',
  42. 'longitude',
  43. 'latitude',
  44. 'is_default',
  45. 'status',
  46. 'updated_at',
  47. ]), JSON_THROW_ON_ERROR));
  48. }
  49. public static function legacy(WaterEnterprise $enterprise): array
  50. {
  51. return [
  52. 'id' => 0,
  53. 'enterprise_id' => (int) $enterprise->id,
  54. 'name' => $enterprise->name,
  55. 'employee_count' => $enterprise->employee_count === null ? null : (int) $enterprise->employee_count,
  56. 'label' => '主配送地址',
  57. 'contact' => $enterprise->contact,
  58. 'phone' => PhoneNumber::mask($enterprise->phone),
  59. 'address' => $enterprise->address,
  60. 'region' => $enterprise->region,
  61. 'longitude' => $enterprise->longitude === null ? null : (float) $enterprise->longitude,
  62. 'latitude' => $enterprise->latitude === null ? null : (float) $enterprise->latitude,
  63. 'is_default' => true,
  64. 'status' => (int) $enterprise->status,
  65. 'revision' => WaterPresenter::revision($enterprise),
  66. ];
  67. }
  68. }