From fac6439a8d449e9f34f26270bd423af526ae7e21 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Sat, 28 Aug 2021 12:15:04 +0200 Subject: [PATCH] Fix extraction of forename and surname from mail when empty --- Entity/User.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Entity/User.php b/Entity/User.php index cd32f1e..9ccad72 100644 --- a/Entity/User.php +++ b/Entity/User.php @@ -83,13 +83,16 @@ class User implements UserInterface, \Serializable { * @param string $mail The user mail */ public function __construct(string $mail) { - //Extract names from mail - $names = explode(' ', ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $mail)))))); - //Set defaults - $this->mail = $mail; - $this->forename = $names[0]; - $this->surname = $names[1]??$names[0]; + if (!empty($this->mail = $mail)) { + //Extract names from mail + $names = explode(' ', ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $mail)))))); + $this->forename = $names[0]; + $this->surname = $names[1]??$names[0]; + } else { + $this->forename = ''; + $this->surname = ''; + } $this->password = $mail; $this->active = false; $this->disabled = false; @@ -117,7 +120,18 @@ class User implements UserInterface, \Serializable { * @return User */ public function setMail(string $mail): User { - $this->mail = $mail; + //With mail + if (!empty($this->mail = $mail)) { + //Without forename and surname + if (empty($this->forename) && empty($this->surname)) { + //Extract names from mail + $names = explode(' ', ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $mail)))))); + //Set forename + $this->forename = $names[0]; + //Set surname + $this->surname = $names[1]??$names[0]; + } + } return $this; } -- 2.41.0