1 | <?php |
||
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 |
|
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) |
|
53 | |||
54 | 7 | public function first(): string |
|
58 | |||
59 | 7 | public function last(): ?string |
|
63 | |||
64 | /** |
||
65 | * Returns first + last, such as "Jason Fried". |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | 18 | public function full(): string |
|
73 | |||
74 | /** |
||
75 | * Returns first + last initial, such as "Jason F.". |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 7 | public function familiar(): string |
|
83 | |||
84 | /** |
||
85 | * Returns first initial + last, such as "J. Fried". |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 3 | public function abbreviated(): string |
|
93 | |||
94 | /** |
||
95 | * Returns last + first for sorting. |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | 3 | public function sorted(): string |
|
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 |
|
114 | |||
115 | /** |
||
116 | * Returns just the initials. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 7 | public function initials(): string |
|
126 | |||
127 | /** |
||
128 | * Returns a mentionable version of the familiar name. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | 3 | public function mentionable(): string |
|
136 | |||
137 | /** |
||
138 | * Make the methods accessibles as attributes. |
||
139 | * |
||
140 | * @param string $attribute |
||
141 | * @return mixed |
||
142 | */ |
||
143 | 26 | public function __get($attribute) |
|
147 | |||
148 | 14 | public function __toString(): string |
|
152 | } |
||
153 |