* Configuration > Program Settings (parameter mt3s_marketing_program_stakeholder_phone_region, FR by default). A number that * doesn't parse (free input, extensions…) is kept exactly as typed: the save is never blocked. */ class StakeholderModel extends FormModel { /** * local.php parameter: default region for phone numbers typed without a country prefix. */ public const PHONE_REGION_PARAMETER = 'mt3s_marketing_program_stakeholder_phone_region'; public const DEFAULT_PHONE_REGION = 'FR'; public function getRepository(): StakeholderRepository { $repository = $this->em->getRepository(Stakeholder::class); \assert($repository instanceof StakeholderRepository); return $repository; } public function getPermissionBase(): string { return 'mt3smarketingprogram:stakeholders'; } public function getEntity($id = null): ?Stakeholder { if (null === $id) { return new Stakeholder(); } $stakeholder = parent::getEntity($id); return $stakeholder instanceof Stakeholder ? $stakeholder : null; } /** * Normalizes the phone number to E.164 before saving; keeps the raw input when it doesn't parse. */ public function saveEntity($entity, $unlock = true): void { if ($entity instanceof Stakeholder && null !== $entity->getPhone() && '' !== trim($entity->getPhone())) { $entity->setPhone($this->normalizePhone($entity->getPhone())); } parent::saveEntity($entity, $unlock); } /** * @param array $options */ public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface { if (!$entity instanceof Stakeholder) { throw new MethodNotAllowedHttpException(['Stakeholder']); } if (!empty($action)) { $options['action'] = $action; } return $formFactory->create(StakeholderType::class, $entity, $options); } /** * The number as E.164 when it parses with the default region, or the raw input as typed when it * doesn't ("+33 6 … ext. 12" stays as typed). */ public function normalizePhone(string $phone): string { try { $number = PhoneNumberUtil::getInstance()->parse(trim($phone), $this->getDefaultPhoneRegion()); if (PhoneNumberUtil::getInstance()->isValidNumber($number)) { return PhoneNumberUtil::getInstance()->format($number, PhoneNumberFormat::E164); } } catch (NumberParseException) { // Not a parseable number: keep the raw input. } return $phone; } public function getDefaultPhoneRegion(): string { $region = $this->coreParametersHelper->get(self::PHONE_REGION_PARAMETER); return is_string($region) && '' !== trim($region) ? strtoupper(trim($region)) : self::DEFAULT_PHONE_REGION; } }