name, $state, $this->country); } public function withCountry(CountryCode $country): self { return new self($this->name, $this->state, $country); } public function withoutState(): self { return new self($this->name, null, $this->country); } public function format(bool $includeState = true, bool $includeCountry = false): string { $formatted = $this->name; if ($includeState && $this->state !== null && trim($this->state) !== '') { $formatted .= ', ' . $this->state; } if ($includeCountry && $this->country !== null) { $formatted .= ', ' . ($this->country->getCountryName() ?? $this->country->value); } return $formatted; } public function getDisplayName(): string { return $this->format(true, false); } public function getFullName(): string { return $this->format(true, true); } public function hasState(): bool { return $this->state !== null && trim($this->state) !== ''; } public function hasCountry(): bool { return $this->country !== null; } public function equals(self $other): bool { return $this->name === $other->name && $this->state === $other->state && ( ($this->country === null && $other->country === null) || ($this->country !== null && $other->country !== null && $this->country->value === $other->country->value) ); } public function toString(): string { return $this->getDisplayName(); } public function __toString(): string { return $this->getDisplayName(); } public function toArray(): array { return [ 'name' => $this->name, 'state' => $this->state, 'country' => $this->country?->value, 'display_name' => $this->getDisplayName(), 'full_name' => $this->getFullName(), ]; } public static function fromArray(array $data): self { $country = isset($data['country']) ? CountryCode::fromString($data['country']) : null; return new self( $data['name'], $data['state'] ?? null, $country ); } }