Files
myep/src/BusProNet/Model/Communication.php
T

78 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\BusProNet\Model;
use App\Validator\Constraints as AppAssert;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Represents communication information including email, phone, and newsletter preferences.
*
* This class handles contact information for users and participants in the booking system.
* It provides validation constraints for required fields and methods to convert
* communication data to API payload format.
*/
class Communication
{
#[Assert\When(
expression: 'this.isPhoneNewlyEntered()',
constraints: [new AppAssert\PhoneNumber()],
groups: ['personal_data'],
)]
public ?string $phone = null;
#[Assert\NotBlank(message: 'Bitte angeben', groups: ['personal_data'])]
#[Assert\When(
expression: 'this.isMobileNewlyEntered()',
constraints: [new AppAssert\PhoneNumber()],
groups: ['personal_data'],
)]
public ?string $mobile = null;
// The numbers as loaded from BusPro, which go back unchanged and are exempt from PhoneNumber.
public ?string $loadedPhone = null;
public ?string $loadedMobile = null;
#[Assert\NotBlank(message: 'Bitte angeben', groups: ['personal_identity'])]
#[Assert\Email(message: 'Bitte eine gültige Adresse angeben', mode: 'strict', groups: ['personal_identity'])]
public ?string $email = null;
public bool $newsletter = false;
public function rememberLoadedNumbers(): void
{
$this->loadedPhone = $this->phone;
$this->loadedMobile = $this->mobile;
}
public function isPhoneNewlyEntered(): bool
{
return AppAssert\PhoneNumber::isNewlyEntered($this->phone, $this->loadedPhone);
}
public function isMobileNewlyEntered(): bool
{
return AppAssert\PhoneNumber::isNewlyEntered($this->mobile, $this->loadedMobile);
}
/**
* Converts the communication data to API payload format.
*
* Transforms the communication 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 [
'email' => $this->email,
'telefonmobil' => $this->mobile,
'telefonprivat' => $this->phone,
'newsletter' => $this->newsletter ? 'True' : 'False',
];
}
}