]> Raphaël G. Git Repositories - userbundle/commitdiff
Fix extraction of forename and surname from mail when empty
authorRaphaël Gertz <git@rapsys.eu>
Sat, 28 Aug 2021 10:15:04 +0000 (12:15 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Sat, 28 Aug 2021 10:15:04 +0000 (12:15 +0200)
Entity/User.php

index cd32f1ec0e08623bf4b3a3922598bb13cecbb98c..9ccad72e5a17fb405bfa0b5f8d2dc163e951d55a 100644 (file)
@@ -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;
        }