-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathNameTrait.php
102 lines (92 loc) · 3.39 KB
/
NameTrait.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\base;
use Craft;
use craft\nameparsing\CustomLanguage;
use TheIconic\NameParser\Language\English;
use TheIconic\NameParser\Language\German;
use TheIconic\NameParser\Parser as NameParser;
/**
* NameTrait implements the common properties for entities with full/first/last names.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.0.0
*/
trait NameTrait
{
/**
* @var string|null Full name
* @since 4.0.0
*/
public ?string $fullName = null;
/**
* @var string|null First name
*/
public ?string $firstName = null;
/**
* @var string|null Last name
*/
public ?string $lastName = null;
/**
* Normalizes the name properties.
*/
protected function normalizeNames(): void
{
$properties = ['fullName', 'firstName', 'lastName'];
foreach ($properties as $property) {
if (isset($this->$property) && trim($this->$property) === '') {
$this->$property = null;
}
}
}
/**
* Parses `fullName` if set, or sets it based on `firstName` and `lastName`.
*/
protected function prepareNamesForSave(): void
{
if ($this->fullName !== null) {
// if firstName/lastName are also set, just leave them alone
// (https://github.com/craftcms/cms/issues/14665)
if ($this->firstName !== null || $this->lastName !== null) {
return;
}
$generalConfig = Craft::$app->getConfig()->getGeneral();
$languages = [
// Load our custom language file first so config settings can override the defaults
new CustomLanguage(
$generalConfig->extraNameSuffixes,
$generalConfig->extraNameSalutations,
$generalConfig->extraLastNamePrefixes,
),
new English(),
new German(),
];
$name = (new NameParser($languages))->parse($this->fullName);
$this->firstName = $name->getFirstname() ?: null;
$this->lastName = $name->getLastname() ?: null;
// Re-extract the first and last names from the full name to ensure casing doesn't change
// see https://github.com/craftcms/cms/issues/14723
if ($this->firstName !== null) {
$firstNameOffset = mb_stripos($this->fullName, $this->firstName);
if ($firstNameOffset !== false) {
$this->firstName = mb_substr($this->fullName, $firstNameOffset, mb_strlen($this->firstName));
}
}
if ($this->lastName !== null) {
$lastNameOffset = mb_stripos($this->fullName, $this->lastName,
isset($firstNameOffset) && $firstNameOffset !== false
? $firstNameOffset + mb_strlen($this->firstName) : 0,
);
if ($lastNameOffset !== false) {
$this->lastName = mb_substr($this->fullName, $lastNameOffset, mb_strlen($this->lastName));
}
}
} elseif ($this->firstName !== null || $this->lastName !== null) {
$this->fullName = trim("$this->firstName $this->lastName") ?: null;
}
}
}