From 568eb498bb27ec6527d4f3eec090d8842e9b4fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 24 Feb 2022 15:13:56 +0100 Subject: [PATCH] Customer: Fix algorithm for duplicare firstName and lastName. --- src/Entity/Customer.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 1e79a13..e9b275e 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -151,10 +151,7 @@ public function getFirstName(): string public function setFirstName(string $firstName): void { - if (trim($firstName) === '') { - throw new \InvalidArgumentException('First name can not be empty.'); - } - $this->firstName = $this->formatName($firstName); + $this->setName($firstName, $this->lastName); } @@ -166,10 +163,7 @@ public function getLastName(): string public function setLastName(string $lastName): void { - if (trim($lastName) === '') { - throw new \InvalidArgumentException('Last name can not be empty.'); - } - $this->lastName = $this->formatName($lastName); + $this->setName($this->firstName, $lastName); } @@ -410,8 +404,15 @@ private function setName(string $firstName, string $lastName): void if ($firstName === $lastName && isset($parts[0], $parts[1])) { [$firstName, $lastName] = $parts; } - $this->setFirstName($firstName); - $this->setLastName($lastName); + $validator = static function(string $name): void { + if (trim($name) === '') { + throw new \InvalidArgumentException('Name can not be empty.'); + } + }; + $validator($firstName); + $validator($lastName); + $this->firstName = $this->formatName($firstName); + $this->lastName = $this->formatName($lastName); }