Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
56 / 56 |
|
100.00% |
23 / 23 |
CRAP | |
100.00% |
1 / 1 |
| NameResult | |
100.00% |
56 / 56 |
|
100.00% |
23 / 23 |
27 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| getSalutation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFirstname | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMiddlename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNickname | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| getInitials | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLastnamePrefix | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLastname | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSuffix | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCredentials | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getGivenName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getFullName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| hasSalutation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasFirstname | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasMiddlename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasNickname | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasInitials | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasLastnamePrefix | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasLastname | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasSuffix | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasCredentials | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Parser\Name; |
| 16 | |
| 17 | use Pop\Parser\AbstractResult; |
| 18 | |
| 19 | /** |
| 20 | * Name result class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Parser |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 1.0.0 |
| 28 | */ |
| 29 | class NameResult extends AbstractResult |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Salutation |
| 34 | * @var ?string |
| 35 | * */ |
| 36 | protected ?string $salutation = null; |
| 37 | |
| 38 | /** |
| 39 | * First name |
| 40 | * @var ?string |
| 41 | * */ |
| 42 | protected ?string $firstname = null; |
| 43 | |
| 44 | /** |
| 45 | * Middle name |
| 46 | * @var ?string |
| 47 | * */ |
| 48 | protected ?string $middlename = null; |
| 49 | |
| 50 | /** |
| 51 | * Nickname |
| 52 | * @var ?string |
| 53 | * */ |
| 54 | protected ?string $nickname = null; |
| 55 | |
| 56 | /** |
| 57 | * Initials |
| 58 | * @var ?string |
| 59 | * */ |
| 60 | protected ?string $initials = null; |
| 61 | |
| 62 | /** |
| 63 | * Lastname prefix |
| 64 | * @var ?string |
| 65 | * */ |
| 66 | protected ?string $lastnamePrefix = null; |
| 67 | |
| 68 | /** |
| 69 | * Last name |
| 70 | * @var ?string |
| 71 | * */ |
| 72 | protected ?string $lastname = null; |
| 73 | |
| 74 | /** |
| 75 | * Suffix |
| 76 | * @var ?string |
| 77 | * */ |
| 78 | protected ?string $suffix = null; |
| 79 | |
| 80 | /** |
| 81 | * Credentials |
| 82 | * @var ?string |
| 83 | * */ |
| 84 | protected ?string $credentials = null; |
| 85 | |
| 86 | /** |
| 87 | * Constructor |
| 88 | * |
| 89 | * @param array $name |
| 90 | */ |
| 91 | public function __construct(array $name) |
| 92 | { |
| 93 | $this->salutation = $name['salutation'] ?? null; |
| 94 | $this->firstname = $name['firstname'] ?? null; |
| 95 | $this->middlename = $name['middlename'] ?? null; |
| 96 | $this->nickname = $name['nickname'] ?? null; |
| 97 | $this->initials = $name['initials'] ?? null; |
| 98 | $this->lastnamePrefix = $name['lastnamePrefix'] ?? null; |
| 99 | $this->lastname = $name['lastname'] ?? null; |
| 100 | $this->suffix = $name['suffix'] ?? null; |
| 101 | $this->credentials = $name['credentials'] ?? null; |
| 102 | $this->confidence = $name['confidence'] ?? 1.0; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Method to get salutation |
| 107 | * |
| 108 | * @return ?string |
| 109 | */ |
| 110 | public function getSalutation(): ?string |
| 111 | { |
| 112 | return $this->salutation; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Method to get first name |
| 117 | * |
| 118 | * @return ?string |
| 119 | */ |
| 120 | public function getFirstname(): ?string |
| 121 | { |
| 122 | return $this->firstname; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Method to get middle name |
| 127 | * |
| 128 | * @return ?string |
| 129 | */ |
| 130 | public function getMiddlename(): ?string |
| 131 | { |
| 132 | return $this->middlename; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Method to get nickname |
| 137 | * |
| 138 | * @param bool $wrap |
| 139 | * @return ?string |
| 140 | */ |
| 141 | public function getNickname(bool $wrap = false): ?string |
| 142 | { |
| 143 | if (($this->nickname !== null) && $wrap) { |
| 144 | return '(' . $this->nickname . ')'; |
| 145 | } |
| 146 | |
| 147 | return $this->nickname; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Method to get initials |
| 152 | * |
| 153 | * @return ?string |
| 154 | */ |
| 155 | public function getInitials(): ?string |
| 156 | { |
| 157 | return $this->initials; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Method to get lastname prefix |
| 162 | * |
| 163 | * @return ?string |
| 164 | */ |
| 165 | public function getLastnamePrefix(): ?string |
| 166 | { |
| 167 | return $this->lastnamePrefix; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Method to get last name |
| 172 | * |
| 173 | * @return ?string |
| 174 | */ |
| 175 | public function getLastname(): ?string |
| 176 | { |
| 177 | return $this->lastname; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Method to get suffix |
| 182 | * |
| 183 | * @return ?string |
| 184 | */ |
| 185 | public function getSuffix(): ?string |
| 186 | { |
| 187 | return $this->suffix; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Method to get credentials |
| 192 | * |
| 193 | * @return ?string |
| 194 | */ |
| 195 | public function getCredentials(): ?string |
| 196 | { |
| 197 | return $this->credentials; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Method to get the given name (first name, initials and middle name, in that order) |
| 202 | * |
| 203 | * @return ?string |
| 204 | */ |
| 205 | public function getGivenName(): ?string |
| 206 | { |
| 207 | $parts = array_filter([$this->firstname, $this->initials, $this->middlename]); |
| 208 | return !empty($parts) ? implode(' ', $parts) : null; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Method to get the full name (given name plus lastname prefix and lastname) |
| 213 | * |
| 214 | * @return ?string |
| 215 | */ |
| 216 | public function getFullName(): ?string |
| 217 | { |
| 218 | $parts = array_filter([$this->getGivenName(), $this->lastnamePrefix, $this->lastname]); |
| 219 | return !empty($parts) ? implode(' ', $parts) : null; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Has salutation |
| 224 | * |
| 225 | * @return bool |
| 226 | */ |
| 227 | public function hasSalutation(): bool |
| 228 | { |
| 229 | return !empty($this->salutation); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Has first name |
| 234 | * |
| 235 | * @return bool |
| 236 | */ |
| 237 | public function hasFirstname(): bool |
| 238 | { |
| 239 | return !empty($this->firstname); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Has middle name |
| 244 | * |
| 245 | * @return bool |
| 246 | */ |
| 247 | public function hasMiddlename(): bool |
| 248 | { |
| 249 | return !empty($this->middlename); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Has nickname |
| 254 | * |
| 255 | * @return bool |
| 256 | */ |
| 257 | public function hasNickname(): bool |
| 258 | { |
| 259 | return !empty($this->nickname); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Has initials |
| 264 | * |
| 265 | * @return bool |
| 266 | */ |
| 267 | public function hasInitials(): bool |
| 268 | { |
| 269 | return !empty($this->initials); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Has lastname prefix |
| 274 | * |
| 275 | * @return bool |
| 276 | */ |
| 277 | public function hasLastnamePrefix(): bool |
| 278 | { |
| 279 | return !empty($this->lastnamePrefix); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Has last name |
| 284 | * |
| 285 | * @return bool |
| 286 | */ |
| 287 | public function hasLastname(): bool |
| 288 | { |
| 289 | return !empty($this->lastname); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Has suffix |
| 294 | * |
| 295 | * @return bool |
| 296 | */ |
| 297 | public function hasSuffix(): bool |
| 298 | { |
| 299 | return !empty($this->suffix); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Has credentials |
| 304 | * |
| 305 | * @return bool |
| 306 | */ |
| 307 | public function hasCredentials(): bool |
| 308 | { |
| 309 | return !empty($this->credentials); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * To array method |
| 314 | * |
| 315 | * @return array |
| 316 | */ |
| 317 | public function toArray(): array |
| 318 | { |
| 319 | return [ |
| 320 | 'salutation' => $this->salutation, |
| 321 | 'firstname' => $this->firstname, |
| 322 | 'initials' => $this->initials, |
| 323 | 'middlename' => $this->middlename, |
| 324 | 'nickname' => $this->nickname, |
| 325 | 'lastnamePrefix' => $this->lastnamePrefix, |
| 326 | 'lastname' => $this->lastname, |
| 327 | 'suffix' => $this->suffix, |
| 328 | 'credentials' => $this->credentials, |
| 329 | 'confidence' => $this->confidence, |
| 330 | ]; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * To string method |
| 335 | * |
| 336 | * @return string |
| 337 | */ |
| 338 | public function __toString(): string |
| 339 | { |
| 340 | $parts = array_filter([ |
| 341 | $this->salutation, |
| 342 | $this->getGivenName(), |
| 343 | $this->getNickname(true), |
| 344 | $this->lastnamePrefix, |
| 345 | $this->lastname, |
| 346 | $this->suffix, |
| 347 | $this->credentials, |
| 348 | ]); |
| 349 | |
| 350 | return implode(' ', $parts); |
| 351 | } |
| 352 | |
| 353 | } |