1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DBUnt1tled\VO\VObjects\Person; |
6
|
|
|
|
7
|
|
|
use DBUnt1tled\VO\Exception\InvalidVOArgumentException; |
8
|
|
|
use DBUnt1tled\VO\VObjects\Scalar\Strings; |
9
|
|
|
use DBUnt1tled\VO\VObjects\ValueObjectInterface; |
10
|
|
|
|
11
|
|
|
class NameExtended extends Name |
12
|
|
|
{ |
13
|
|
|
/** @var Strings */ |
14
|
|
|
private $middleName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* NameExtended constructor. |
18
|
|
|
* @param Strings $firstName |
19
|
|
|
* @param Strings $middleName |
20
|
|
|
* @param Strings $lastName |
21
|
|
|
* @throws \ReflectionException |
22
|
|
|
*/ |
23
|
2 |
|
public function __construct(Strings $firstName, Strings $middleName, Strings $lastName) |
24
|
|
|
{ |
25
|
2 |
|
parent::__construct($firstName, $lastName); |
26
|
2 |
|
$this->guardExtended($middleName); |
27
|
1 |
|
$this->middleName = $middleName; |
|
|
|
|
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $value |
32
|
|
|
* @param mixed ...$other |
33
|
|
|
*/ |
34
|
2 |
|
public function guardExtended($value, ...$other): void |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
/** @var ValueObjectInterface $value*/ |
37
|
2 |
|
if (mb_strlen($value->getValue()) < self::NAME_MIN_LENGTH) { |
38
|
1 |
|
throw new InvalidVOArgumentException(sprintf('Middle name must contain a minimum of %s symbols.', self::NAME_MIN_LENGTH), $value); |
39
|
|
|
} |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $firstName |
44
|
|
|
* @param string $middleName |
45
|
|
|
* @param string $lastName |
46
|
|
|
* @return NameExtended |
47
|
|
|
* @throws \ReflectionException |
48
|
|
|
*/ |
49
|
2 |
|
public static function createFromString(string $firstName, string $middleName, string $lastName = ''): self |
50
|
|
|
{ |
51
|
2 |
|
return new static(new Strings($firstName), new Strings($middleName), new Strings($lastName)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
1 |
|
public function getMiddleName(): string |
58
|
|
|
{ |
59
|
1 |
|
return $this->middleName->getValue(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
1 |
|
public function getFullName(): string |
66
|
|
|
{ |
67
|
1 |
|
return $this->getFirstName().' '.$this->getMiddleName().' '.$this->getLastName(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.