Files
myep/src/BusProNet/Model/Address.php
T
2026-09-11 19:37:00 +02:00

79 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\BusProNet\Model;
use Symfony\Component\Validator\Constraints as Assert;
use function Symfony\Component\String\u;
/**
* Represents a physical address with street, postal code, city, and country information.
*
* This class handles address data for users and participants in the booking system.
* It provides validation constraints for required fields and methods to convert
* address data to API payload format.
*/
class Address
{
#[Assert\NotBlank(message: 'Bitte angeben', groups: ['personal_data'])]
public ?string $street = null;
#[Assert\NotBlank(message: 'Bitte angeben', groups: ['personal_data'])]
public ?string $postCode = null;
#[Assert\NotBlank(message: 'Bitte angeben', groups: ['personal_data'])]
public ?string $city = null;
public ?string $district = null;
#[Assert\NotBlank(message: 'Bitte angeben', groups: ['personal_data'])]
public ?string $country = null;
/**
* Normalizes legacy string data loaded from API/session sources.
*
* Trims whitespace and converts empty strings to null so load-time audits
* and form hydration see canonical values.
*/
public function normalize(): void
{
$this->street = $this->normalizeNullableString($this->street);
$this->postCode = $this->normalizeNullableString($this->postCode);
$this->city = $this->normalizeNullableString($this->city);
$this->district = $this->normalizeNullableString($this->district);
$this->country = $this->normalizeNullableString($this->country);
}
/**
* Converts the address to API payload format.
*
* Transforms the address object into an array structure suitable
* for API communication with the BusProNet system.
*
* @return array<string, string> The payload array for API transmission
*/
public function toPayload(): array
{
return [
'strasse' => $this->street,
'plz' => $this->postCode,
'ort' => $this->city,
'ortsteil' => $this->district,
'land' => $this->country,
];
}
private function normalizeNullableString(?string $value): ?string
{
if (null === $value) {
return null;
}
$trimmed = u($value)->trim()->toString();
return '' === $trimmed ? null : $trimmed;
}
}