<?php
namespace App\Entity\Common;
use App\Enum\Serialization\ContactSerializationGroups;
use App\Enum\Serialization\ReverseSyncSerializationGroups;
use App\Repository\Common\ContactRepository;
use Doctrine\ORM\Mapping as ORM;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Contact\ContactInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Contact\ContactMethodTrait;
use Nellapp\Bundle\SDKBundle\Entity\Common\Address\Address;
use Nellapp\Bundle\SDKBundle\ReverseSync\Entity\ReverseSyncableInterface;
use Nellapp\Bundle\SDKBundle\ReverseSync\EntityKeys;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Validator\Constraints as Assert;
#[UniqueEntity(
fields: ['email'],
message: 'contact.email.UniqueEntity',
errorPath: 'email',
groups: ['check_unique'],
)]
#[
ORM\Entity(repositoryClass: ContactRepository::class),
ORM\UniqueConstraint(fields: ['email']),
ORM\Table(name: 'contacts'),
ORM\Index(fields: ['email'], flags: ['fulltext']),
ORM\Index(fields: ['firstName'], flags: ['fulltext']),
ORM\Index(fields: ['lastName'], flags: ['fulltext']),
]
class Contact implements ContactInterface, ReverseSyncableInterface
{
use ContactMethodTrait;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'NONE')]
#[ORM\Column(type: 'string', length: 36)]
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
private ?string $id = null;
#[ORM\Column(type: 'string', length: 180)]
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
ReverseSyncSerializationGroups::REVERSE_SYNC,
ContactSerializationGroups::CREATE,
])]
#[
Assert\NotBlank(message: 'contact.email.NotBlank'),
Assert\Email(message: 'contact.email.Email', mode: 'strict'),
Assert\Length(
min: 6,
max: 64,
minMessage: 'contact.email.Length.min',
maxMessage: 'contact.email.Length.max',
)
]
private ?string $email = null;
#[ORM\Column(type: 'string', nullable: true)]
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
ReverseSyncSerializationGroups::REVERSE_SYNC,
ContactSerializationGroups::CREATE,
])]
#[
Assert\NotBlank(message: 'contact.firstName.NotBlank'),
Assert\Length(
min: 2,
max: 255,
minMessage: 'contact.firstName.Length.min',
maxMessage: 'contact.firstName.Length.max',
)
]
private ?string $firstName = null;
#[ORM\Column(type: 'string', nullable: true)]
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
ReverseSyncSerializationGroups::REVERSE_SYNC,
ContactSerializationGroups::CREATE,
])]
#[
Assert\NotBlank(message: 'contact.lastName.NotBlank'),
Assert\Length(
min: 2,
max: 255,
minMessage: 'contact.lastName.Length.min',
maxMessage: 'contact.lastName.Length.max',
)
]
private ?string $lastName = null;
#[ORM\Column(type: 'string', length: 20, nullable: true)]
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
ReverseSyncSerializationGroups::REVERSE_SYNC,
ContactSerializationGroups::CREATE,
])]
#[
Assert\Length(
min: 8,
max: 20,
minMessage: 'contact.phoneNumber.Length.min',
maxMessage: 'contact.phoneNumber.Length.max',
)
]
private ?string $phoneNumber = null;
#[ORM\Embedded(class: Address::class)]
#[Assert\Valid()]
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
private ?Address $address = null;
public function __construct()
{
$this->initContactMethod();
}
public static function getReverseSyncKey(): string
{
return EntityKeys::CONTACT;
}
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
])]
public function getFullAddress(): string
{
return $this->address?->toMultiLine() ?? '';
}
public function getPostCodeAndCity(): string
{
return $this->address?->getPostCodeAndCity() ?? '';
}
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
])]
public function getFullName(): string
{
return trim(($this->firstName ?? '') . ' ' . ($this->lastName ?? ''));
}
#[Serial\Groups([
ContactSerializationGroups::COLLECTION,
ContactSerializationGroups::DETAIL,
])]
public function getFullNameWithEmail(): string
{
return trim($this->getFullName() . ' <' . $this->email . '>');
}
}