-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDonorName.php
More file actions
82 lines (69 loc) · 2.45 KB
/
Copy pathDonorName.php
File metadata and controls
82 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace MatchBot\Domain;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embeddable;
use MatchBot\Application\Assert;
use MatchBot\Application\Assertion;
use MatchBot\Application\LazyAssertionException;
/**
* @psalm-immutable
*/
#[Embeddable]
class DonorName
{
/** Currently blank string for organisations. */
#[Column(type: 'string')]
public string $first;
#[Column(type: 'string')]
public string $last;
/**
* @psalm-suppress ImpureMethodCall - \Assert\Assert::lazy etc could probably be marked as pure but is not.
* @throws LazyAssertionException
*/
private function __construct(string $first, string $last)
{
// long numbers are almost certainly mistakes, could be sensitive e.g. payment card no.
// Even if spaces between digits.
$sixDigitsRegex = '/\d\s?\d\s?\d\s?\d\s?\d\s?\d/';
// first name may be empty to account for organisation donors who only have a last name
Assert::lazy()
->that($first, 'first')->betweenLength(0, 255)
->that($last, 'last')->betweenLength(1, 255)
->that($first)->notRegex($sixDigitsRegex)
->that($last)->notRegex($sixDigitsRegex)
->verifyNow();
$this->first = $first;
$this->last = $last;
}
/**
* @throws LazyAssertionException
*/
public static function of(string $first, string $last): self
{
return new self($first, $last);
}
/**
* @param string|null $firstName
* @param string|null $lastName
* @return DonorName|null
* @throws \Assert\AssertionFailedException
*/
public static function maybeFromFirstAndLast(?string $firstName, ?string $lastName, bool $isOrganisation = false): ?self
{
if ($isOrganisation) {
Assertion::notBlank($lastName, 'Last name is required for organisation donors');
return DonorName::of('', $lastName);
}
$hasFirstName = is_string($firstName) && $firstName !== '';
$hasLastName = is_string($lastName) && $lastName !== '';
Assertion::same($hasFirstName, $hasLastName, "First and last names must be supplied together or not at all.");
return ($hasFirstName && $hasLastName) ? DonorName::of($firstName, $lastName) : null;
}
public function fullName(): string
{
if ($this->first === '') {
return $this->last;
}
return "{$this->first} {$this->last}";
}
}