You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompanyDTO.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\DTO;
  3. use App\Models\Company;
  4. readonly class CompanyDTO
  5. {
  6. public function __construct(
  7. public string $name,
  8. public string $addressLine1,
  9. public string $addressLine2,
  10. public string $city,
  11. public string $state,
  12. public string $postalCode,
  13. public string $country,
  14. ) {}
  15. public static function fromModel(Company $company): self
  16. {
  17. $profile = $company->profile;
  18. $address = $profile->address ?? null;
  19. return new self(
  20. name: $company->name,
  21. addressLine1: $address?->address_line_1 ?? '',
  22. addressLine2: $address?->address_line_2 ?? '',
  23. city: $address?->city ?? '',
  24. state: $address?->state?->name ?? '',
  25. postalCode: $address?->postal_code ?? '',
  26. country: $address?->country?->name ?? '',
  27. );
  28. }
  29. public function getFormattedAddressHtml(): ?string
  30. {
  31. if (empty($this->addressLine1)) {
  32. return null;
  33. }
  34. $lines = array_filter([
  35. $this->addressLine1,
  36. $this->addressLine2,
  37. implode(', ', array_filter([
  38. $this->city,
  39. $this->state,
  40. $this->postalCode,
  41. ])),
  42. $this->country,
  43. ]);
  44. return collect($lines)
  45. ->map(static fn ($line) => "<p>{$line}</p>")
  46. ->join('');
  47. }
  48. }