1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Druc\PersonName; |
4
|
|
|
|
5
|
|
|
class PersonName |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
/** @var string */ |
8
|
|
|
public $fullName; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PersonName constructor. |
12
|
|
|
* @param string $fullName |
13
|
|
|
*/ |
14
|
30 |
|
private function __construct(string $fullName = '') |
15
|
|
|
{ |
16
|
30 |
|
$this->fullName = $fullName; |
17
|
30 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $fullName |
21
|
|
|
* @return PersonName |
22
|
|
|
*/ |
23
|
30 |
|
public static function make(string $fullName) |
24
|
|
|
{ |
25
|
30 |
|
return new static($fullName); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $property |
30
|
|
|
* @return null |
31
|
|
|
*/ |
32
|
30 |
|
public function __get($property) |
33
|
|
|
{ |
34
|
30 |
|
if (in_array($property, get_class_methods($this))) { |
35
|
30 |
|
return $this->{$property}(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
3 |
|
public function full() |
45
|
|
|
{ |
46
|
3 |
|
return $this->fullName; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
15 |
|
public function first() |
53
|
|
|
{ |
54
|
15 |
|
return preg_split('/\s+/', $this->fullName, 2)[0]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
21 |
|
public function last() |
61
|
|
|
{ |
62
|
21 |
|
return preg_split('/\s+/', $this->fullName, 2)[1]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public function initials() |
69
|
|
|
{ |
70
|
3 |
|
preg_match_all('/\b(\S)\S*/', $this->fullName, $matches); |
71
|
3 |
|
return implode('', $matches[1]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* First name + Last name initial |
76
|
|
|
* Ex: Constantin Druc Pinsmile -> Constantin D. |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
6 |
|
public function familiar() |
80
|
|
|
{ |
81
|
6 |
|
return $this->first() . ' ' . substr($this->last(), 0, 1) . '.'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Ex: Constantin Druc Pinsmile -> CDP |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
3 |
|
public function abbreviated() |
89
|
|
|
{ |
90
|
3 |
|
return substr($this->first, 0, 1) . '. ' . $this->last(); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Last name, First name |
95
|
|
|
* Ex: Constantin Druc Pinsmile -> Druc Pinsmile, Constantin |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
3 |
|
public function sorted() |
99
|
|
|
{ |
100
|
3 |
|
return $this->last() . ', ' . $this->first(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Lowercase and whitespace striped version of the familiar name |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
3 |
|
public function mentionable() |
108
|
|
|
{ |
109
|
3 |
|
return rtrim(strtolower(preg_replace('/\s+/', '', $this->familiar())), ". "); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
6 |
|
public function possessive() |
116
|
|
|
{ |
117
|
6 |
|
if (substr($this->last(), -1, 1) !== 's') { |
118
|
3 |
|
return $this->fullName . "'s"; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return $this->fullName . "'"; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|