|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Enzyme\Name; |
|
4
|
|
|
|
|
5
|
|
|
use Stringy\Stringy as S; |
|
6
|
|
|
|
|
7
|
|
|
class Format |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The simple name instance to format |
|
11
|
|
|
* |
|
12
|
|
|
* @var Simple |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $name; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Create a new formatter. |
|
18
|
|
|
* |
|
19
|
|
|
* @param Simple $name The name to format. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(Simple $name) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->name = $name; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* A quick way to format a name. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Simple $name The name to format. |
|
30
|
|
|
* @param string $format_string The format string. |
|
31
|
|
|
* |
|
32
|
|
|
* @return S |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function nameLike(Simple $name, $format_string) |
|
35
|
|
|
{ |
|
36
|
|
|
$fmt = new static($name); |
|
37
|
|
|
return $fmt->like($format_string); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Formats the name like the given format string describes. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $format_string The format string. |
|
44
|
|
|
* |
|
45
|
|
|
* @return S |
|
46
|
|
|
*/ |
|
47
|
|
|
public function like($format_string) |
|
48
|
|
|
{ |
|
49
|
|
|
$string = S::create($format_string)->collapseWhitespace(); |
|
50
|
|
|
$parts = $string->split(' '); |
|
51
|
|
|
|
|
52
|
|
|
$final_parts = []; |
|
53
|
|
|
foreach ($parts as $part) { |
|
54
|
|
|
$final_parts[] = $this->format($part); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return S::create(implode(' ', $final_parts))->collapseWhitespace(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Format the given name segment/part. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $part The part. |
|
64
|
|
|
* |
|
65
|
|
|
* @return S |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function format($part) |
|
68
|
|
|
{ |
|
69
|
|
|
$part = S::create($part); |
|
70
|
|
|
|
|
71
|
|
|
switch ($part->substr(0, 2)) { |
|
72
|
|
|
case 'Pr': |
|
73
|
|
|
return $part->replace('Prefix', $this->tryGetLongVersion($this->name->getPrefix())); |
|
74
|
|
|
|
|
75
|
|
|
case 'Fi': |
|
76
|
|
|
return $part->replace('First', $this->tryGetLongVersion($this->name->getFirst())); |
|
77
|
|
|
|
|
78
|
|
|
case 'Mi': |
|
79
|
|
|
return $part->replace('Middle', $this->tryGetLongVersion($this->name->getMiddle())); |
|
80
|
|
|
|
|
81
|
|
|
case 'La': |
|
82
|
|
|
return $part->replace('Last', $this->tryGetLongVersion($this->name->getLast())); |
|
83
|
|
|
|
|
84
|
|
|
case 'P.': |
|
85
|
|
|
return $part->replace('P.', $this->tryGetShortVersion($this->name->getPrefix())); |
|
86
|
|
|
|
|
87
|
|
|
case 'F.': |
|
88
|
|
|
return $part->replace('F.', $this->tryGetShortVersion($this->name->getFirst())); |
|
89
|
|
|
|
|
90
|
|
|
case 'M.': |
|
91
|
|
|
return $part->replace('M.', $this->tryGetShortVersion($this->name->getMiddle())); |
|
92
|
|
|
|
|
93
|
|
|
case 'L.': |
|
94
|
|
|
return $part->replace('L.', $this->tryGetShortVersion($this->name->getLast())); |
|
95
|
|
|
|
|
96
|
|
|
default: |
|
97
|
|
|
return $part; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Tries to extract the long version of the given name from this part. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Part $part The part to process. |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function tryGetLongVersion($part) |
|
109
|
|
|
{ |
|
110
|
|
|
return $part !== null |
|
111
|
|
|
? $part->long() |
|
112
|
|
|
: ''; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Tries to extract the short version of the given name from this part. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Part $part The part to process. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function tryGetShortVersion($part) |
|
123
|
|
|
{ |
|
124
|
|
|
return $part !== null |
|
125
|
|
|
? $part->short() |
|
126
|
|
|
: ''; |
|
127
|
|
|
} |
|
128
|
|
|
} |