src/Entity/Common/Contact.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Common;
  3. use App\Enum\Serialization\ContactSerializationGroups;
  4. use App\Enum\Serialization\ReverseSyncSerializationGroups;
  5. use App\Repository\Common\ContactRepository;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Nellapp\Bundle\SDKBundle\Channel\Entity\Contact\ContactInterface;
  8. use Nellapp\Bundle\SDKBundle\Channel\Entity\Contact\ContactMethodTrait;
  9. use Nellapp\Bundle\SDKBundle\Entity\Common\Address\Address;
  10. use Nellapp\Bundle\SDKBundle\ReverseSync\Entity\ReverseSyncableInterface;
  11. use Nellapp\Bundle\SDKBundle\ReverseSync\EntityKeys;
  12. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  13. use Symfony\Component\Serializer\Annotation as Serial;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. #[UniqueEntity(
  16.     fields: ['email'],
  17.     message'contact.email.UniqueEntity',
  18.     errorPath'email',
  19.     groups: ['check_unique'],
  20. )]
  21. #[
  22.     ORM\Entity(repositoryClassContactRepository::class),
  23.     ORM\UniqueConstraint(fields: ['email']),
  24.     ORM\Table(name'contacts'),
  25.     ORM\Index(fields: ['email'], flags: ['fulltext']),
  26.     ORM\Index(fields: ['firstName'], flags: ['fulltext']),
  27.     ORM\Index(fields: ['lastName'], flags: ['fulltext']),
  28. ]
  29. class Contact implements ContactInterfaceReverseSyncableInterface
  30. {
  31.     use ContactMethodTrait;
  32.     #[ORM\Id]
  33.     #[ORM\GeneratedValue(strategy'NONE')]
  34.     #[ORM\Column(type'string'length36)]
  35.     #[Serial\Groups([
  36.         ContactSerializationGroups::COLLECTION,
  37.         ContactSerializationGroups::DETAIL,
  38.         ReverseSyncSerializationGroups::REVERSE_SYNC,
  39.     ])]
  40.     private ?string $id null;
  41.     #[ORM\Column(type'string'length180)]
  42.     #[Serial\Groups([
  43.         ContactSerializationGroups::COLLECTION,
  44.         ContactSerializationGroups::DETAIL,
  45.         ReverseSyncSerializationGroups::REVERSE_SYNC,
  46.         ContactSerializationGroups::CREATE,
  47.     ])]
  48.     #[
  49.         Assert\NotBlank(message'contact.email.NotBlank'),
  50.         Assert\Email(message'contact.email.Email'mode'strict'),
  51.         Assert\Length(
  52.             min6,
  53.             max64,
  54.             minMessage'contact.email.Length.min',
  55.             maxMessage'contact.email.Length.max',
  56.         )
  57.     ]
  58.     private ?string $email null;
  59.     #[ORM\Column(type'string'nullabletrue)]
  60.     #[Serial\Groups([
  61.         ContactSerializationGroups::COLLECTION,
  62.         ContactSerializationGroups::DETAIL,
  63.         ReverseSyncSerializationGroups::REVERSE_SYNC,
  64.         ContactSerializationGroups::CREATE,
  65.     ])]
  66.     #[
  67.         Assert\NotBlank(message'contact.firstName.NotBlank'),
  68.         Assert\Length(
  69.             min2,
  70.             max255,
  71.             minMessage'contact.firstName.Length.min',
  72.             maxMessage'contact.firstName.Length.max',
  73.         )
  74.     ]
  75.     private ?string $firstName null;
  76.     #[ORM\Column(type'string'nullabletrue)]
  77.     #[Serial\Groups([
  78.         ContactSerializationGroups::COLLECTION,
  79.         ContactSerializationGroups::DETAIL,
  80.         ReverseSyncSerializationGroups::REVERSE_SYNC,
  81.         ContactSerializationGroups::CREATE,
  82.     ])]
  83.     #[
  84.         Assert\NotBlank(message'contact.lastName.NotBlank'),
  85.         Assert\Length(
  86.             min2,
  87.             max255,
  88.             minMessage'contact.lastName.Length.min',
  89.             maxMessage'contact.lastName.Length.max',
  90.         )
  91.     ]
  92.     private ?string $lastName null;
  93.     #[ORM\Column(type'string'length20nullabletrue)]
  94.     #[Serial\Groups([
  95.         ContactSerializationGroups::COLLECTION,
  96.         ContactSerializationGroups::DETAIL,
  97.         ReverseSyncSerializationGroups::REVERSE_SYNC,
  98.         ContactSerializationGroups::CREATE,
  99.     ])]
  100.     #[
  101.         Assert\Length(
  102.             min8,
  103.             max20,
  104.             minMessage'contact.phoneNumber.Length.min',
  105.             maxMessage'contact.phoneNumber.Length.max',
  106.         )
  107.     ]
  108.     private ?string $phoneNumber null;
  109.     #[ORM\Embedded(class: Address::class)]
  110.     #[Assert\Valid()]
  111.     #[Serial\Groups([
  112.         ContactSerializationGroups::COLLECTION,
  113.         ContactSerializationGroups::DETAIL,
  114.         ReverseSyncSerializationGroups::REVERSE_SYNC,
  115.     ])]
  116.     private ?Address $address null;
  117.     public function __construct()
  118.     {
  119.         $this->initContactMethod();
  120.     }
  121.     public static function getReverseSyncKey(): string
  122.     {
  123.         return EntityKeys::CONTACT;
  124.     }
  125.     #[Serial\Groups([
  126.         ContactSerializationGroups::COLLECTION,
  127.         ContactSerializationGroups::DETAIL,
  128.     ])]
  129.     public function getFullAddress(): string
  130.     {
  131.         return $this->address?->toMultiLine() ?? '';
  132.     }
  133.     public function getPostCodeAndCity(): string
  134.     {
  135.         return $this->address?->getPostCodeAndCity() ?? '';
  136.     }
  137.     #[Serial\Groups([
  138.         ContactSerializationGroups::COLLECTION,
  139.         ContactSerializationGroups::DETAIL,
  140.     ])]
  141.     public function getFullName(): string
  142.     {
  143.         return trim(($this->firstName ?? '') . ' ' . ($this->lastName ?? ''));
  144.     }
  145.     #[Serial\Groups([
  146.         ContactSerializationGroups::COLLECTION,
  147.         ContactSerializationGroups::DETAIL,
  148.     ])]
  149.     public function getFullNameWithEmail(): string
  150.     {
  151.         return trim($this->getFullName() . ' <' $this->email '>');
  152.     }
  153. }