1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webstronauts\PersonName; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @property string $first |
7
|
|
|
* @property string $last |
8
|
|
|
* @property string $full |
9
|
|
|
* @property string $familiar |
10
|
|
|
* @property string $abbreviated |
11
|
|
|
* @property string $sorted |
12
|
|
|
* @property string $possessive |
13
|
|
|
* @property string $initials |
14
|
|
|
* @property string $mentionable |
15
|
|
|
*/ |
16
|
|
|
class PersonName |
17
|
|
|
{ |
18
|
|
|
const POSSESSIVE_FIRST = 'first'; |
19
|
|
|
const POSSESSIVE_LAST = 'last'; |
20
|
|
|
const POSSESSIVE_FULL = 'full'; |
21
|
|
|
const POSSESSIVE_INITIALS = 'initials'; |
22
|
|
|
const POSSESSIVE_SORTED = 'sorted'; |
23
|
|
|
const POSSESSIVE_ABBREVIATED = 'abbreviated'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $first; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $last; |
34
|
|
|
|
35
|
11 |
|
public static function make(?string $fullName = null): ?self |
36
|
|
|
{ |
37
|
11 |
|
$segments = preg_split('/\s/', trim(preg_replace('/\s+/', ' ', $fullName)), 2); |
38
|
|
|
|
39
|
11 |
|
return $segments[0] ? new static($segments[0], $segments[1] ?? null) : null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a new PersonName instance. |
44
|
|
|
* |
45
|
|
|
* @param string $first |
46
|
|
|
* @param string $last |
47
|
|
|
*/ |
48
|
26 |
|
public function __construct(string $first, ?string $last = null) |
49
|
|
|
{ |
50
|
26 |
|
$this->first = $first; |
51
|
26 |
|
$this->last = $last; |
52
|
26 |
|
} |
53
|
|
|
|
54
|
7 |
|
public function first(): string |
55
|
|
|
{ |
56
|
7 |
|
return $this->first; |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
public function last(): ?string |
60
|
|
|
{ |
61
|
7 |
|
return $this->last; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns first + last, such as "Jason Fried". |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
18 |
|
public function full(): string |
70
|
|
|
{ |
71
|
18 |
|
return $this->last ? "{$this->first} {$this->last}" : $this->first; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns first + last initial, such as "Jason F.". |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
7 |
|
public function familiar(): string |
80
|
|
|
{ |
81
|
7 |
|
return $this->last ? "{$this->first} {$this->last[0]}." : $this->first; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns first initial + last, such as "J. Fried". |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
3 |
|
public function abbreviated(): string |
90
|
|
|
{ |
91
|
3 |
|
return $this->last ? "{$this->first[0]}. {$this->last}" : $this->first; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns last + first for sorting. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
3 |
|
public function sorted(): string |
100
|
|
|
{ |
101
|
3 |
|
return $this->last ? "{$this->last}, {$this->first}" : $this->first; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns full name with with trailing 's or ' if name ends in s. |
106
|
|
|
* |
107
|
|
|
* @param string $method |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
7 |
|
public function possessive(string $method = self::POSSESSIVE_FULL): string |
111
|
|
|
{ |
112
|
7 |
|
return sprintf('%s\'%s', call_user_func([$this, $method]), substr($this, -1) !== 's' ? 's' : ''); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns just the initials. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
7 |
|
public function initials(): string |
121
|
|
|
{ |
122
|
7 |
|
preg_match_all('/([[:word:]])[[:word:]]*/i', preg_replace('/(\(|\[).*(\)|\])/', '', $this), $matches); |
123
|
|
|
|
124
|
7 |
|
return implode('', end($matches)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns a mentionable version of the familiar name. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
3 |
|
public function mentionable(): string |
133
|
|
|
{ |
134
|
3 |
|
return strtolower(preg_replace('/\s+/', '', substr($this->familiar, 0, -1))); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Make the methods accessibles as attributes. |
139
|
|
|
* |
140
|
|
|
* @param string $attribute |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
26 |
|
public function __get($attribute) |
144
|
|
|
{ |
145
|
26 |
|
return call_user_func([$this, $attribute]); |
146
|
|
|
} |
147
|
|
|
|
148
|
14 |
|
public function __toString(): string |
149
|
|
|
{ |
150
|
14 |
|
return $this->full; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|