AddressTransformable.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Shop\Addresses\Transformations;
  3. use App\Shop\Addresses\Address;
  4. use App\Shop\Cities\Repositories\CityRepository;
  5. use App\Shop\Countries\Repositories\CountryRepository;
  6. use App\Shop\Customers\Customer;
  7. use App\Shop\Customers\Repositories\CustomerRepository;
  8. use App\Shop\Provinces\Province;
  9. use App\Shop\Provinces\Repositories\ProvinceRepository;
  10. use App\Shop\Cities\City;
  11. use App\Shop\Countries\Country;
  12. trait AddressTransformable
  13. {
  14. /**
  15. * Transform the address
  16. *
  17. * @param Address $address
  18. *
  19. * @return Address
  20. * @throws \App\Shop\Cities\Exceptions\CityNotFoundException
  21. * @throws \App\Shop\Countries\Exceptions\CountryNotFoundException
  22. * @throws \App\Shop\Customers\Exceptions\CustomerNotFoundException
  23. */
  24. public function transformAddress(Address $address)
  25. {
  26. $obj = new Address;
  27. $obj->id = $address->id;
  28. $obj->alias = $address->alias;
  29. $obj->address_1 = $address->address_1;
  30. $obj->address_2 = $address->address_2;
  31. $obj->zip = $address->zip;
  32. $obj->city = $address->city;
  33. if (isset($address->province_id)) {
  34. $provinceRepo = new ProvinceRepository(new Province);
  35. $province = $provinceRepo->findProvinceById($address->province_id);
  36. $obj->province = $province->name;
  37. }
  38. $countryRepo = new CountryRepository(new Country);
  39. $country = $countryRepo->findCountryById($address->country_id);
  40. $obj->country = $country->name;
  41. $customerRepo = new CustomerRepository(new Customer);
  42. $customer = $customerRepo->findCustomerById($address->customer_id);
  43. $obj->customer = $customer->name;
  44. $obj->status = $address->status;
  45. return $obj;
  46. }
  47. }