Completed
Pull Request — master (#27)
by Andre
01:12
created

Name::getFullName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TheIconic\NameParser;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
use TheIconic\NameParser\Part\GivenNamePart;
7
8
class Name
9
{
10
    private const PARTS_NAMESPACE = 'TheIconic\NameParser\Part';
11
12
    /**
13
     * @var array the parts that make up this name
14
     */
15
    protected $parts = [];
16
17
    /**
18
     * constructor takes the array of parts this name consists of
19
     *
20
     * @param array|null $parts
21
     */
22
    public function __construct(array $parts = null)
23
    {
24
        if (null !== $parts) {
25
            $this->setParts($parts);
26
        }
27
    }
28
29
    /**
30
     * @return string
31
     */
32
    public function __toString(): string
33
    {
34
        return implode(' ', $this->getAll(true));
35
    }
36
37
    /**
38
     * set the parts this name consists of
39
     *
40
     * @param array $parts
41
     * @return $this
42
     */
43
    public function setParts(array $parts): Name
44
    {
45
        $this->parts = $parts;
46
47
        return $this;
48
    }
49
50
    /**
51
     * get the parts this name consists of
52
     *
53
     * @return array
54
     */
55
    public function getParts(): array
56
    {
57
        return $this->parts;
58
    }
59
60
    /**
61
     * @param bool $format
62
     * @return array
63
     */
64
    public function getAll(bool $format = false): array
65
    {
66
        $results = [];
67
        $keys = [
68
            'salutation' => [],
69
            'firstname' => [],
70
            'nickname' => [$format],
71
            'middlename' => [],
72
            'initials' => [],
73
            'lastname' => [],
74
            'suffix' => [],
75
        ];
76
77
        foreach ($keys as $key => $args) {
78
            $method = sprintf('get%s', ucfirst($key));
79
            if ($value = call_user_func_array(array($this, $method), $args)) {
80
                $results[$key] = $value;
81
            };
82
        }
83
84
        return $results;
85
    }
86
87
    /**
88
     * get the given name (first name, middle names and initials)
89
     * in the order they were entered while still applying normalisation
90
     *
91
     * @return string
92
     */
93
    public function getGivenName(): string
94
    {
95
        $fullNameParts = [];
96
97
        foreach ($this->parts as $part) {
98
            if ($part instanceof GivenNamePart) {
99
                $fullNameParts[] = $part->normalize();
100
            }
101
        }
102
103
        return implode(' ', $fullNameParts);
104
    }
105
106
    /**
107
     * get the given name followed by the last name (including any prefixes)
108
     *
109
     * @return string
110
     */
111
    public function getFullName(): string
112
    {
113
        return sprintf('%s %s', $this->getGivenName(), $this->getLastname());
114
    }
115
116
    /**
117
     * get the first name
118
     *
119
     * @return string
120
     */
121
    public function getFirstname(): string
122
    {
123
        return $this->export('Firstname');
124
    }
125
126
    /**
127
     * get the last name
128
     *
129
     * @param bool $pure
130
     * @return string
131
     */
132
    public function getLastname(bool $pure = false): string
133
    {
134
        return $this->export('Lastname', $pure);
135
    }
136
137
    /**
138
     * get the last name prefix
139
     *
140
     * @return string
141
     */
142
    public function getLastnamePrefix(): string
143
    {
144
        return $this->export('LastnamePrefix');
145
    }
146
147
    /**
148
     * get the initials
149
     *
150
     * @return string
151
     */
152
    public function getInitials(): string
153
    {
154
        return $this->export('Initial');
155
    }
156
157
    /**
158
     * get the suffix(es)
159
     *
160
     * @return string
161
     */
162
    public function getSuffix(): string
163
    {
164
        return $this->export('Suffix');
165
    }
166
167
    /**
168
     * get the salutation(s)
169
     *
170
     * @return string
171
     */
172
    public function getSalutation(): string
173
    {
174
        return $this->export('Salutation');
175
    }
176
177
    /**
178
     * get the nick name(s)
179
     *
180
     * @param bool $wrap
181
     * @return string
182
     */
183
    public function getNickname(bool $wrap = false): string
184
    {
185
        if ($wrap) {
186
            return sprintf('(%s)', $this->export('Nickname'));
187
        }
188
189
        return $this->export('Nickname');
190
    }
191
192
    /**
193
     * get the middle name(s)
194
     *
195
     * @return string
196
     */
197
    public function getMiddlename(): string
198
    {
199
        return $this->export('Middlename');
200
    }
201
202
    /**
203
     * helper method used by getters to extract and format relevant name parts
204
     *
205
     * @param string $type
206
     * @param bool $strict
207
     * @return string
208
     */
209
    protected function export(string $type, bool $strict = false): string
210
    {
211
        $matched = [];
212
213
        foreach ($this->parts as $part) {
214
            if ($part instanceof AbstractPart && $this->isType($part, $type, $strict)) {
215
                $matched[] = $part->normalize();
216
            }
217
        }
218
219
        return implode(' ',  $matched);
220
    }
221
222
    /**
223
     * helper method to check if a part is of the given type
224
     *
225
     * @param AbstractPart $part
226
     * @param string $type
227
     * @param bool $strict
228
     * @return bool
229
     */
230
    protected function isType(AbstractPart $part, string $type, bool $strict = false): bool
231
    {
232
        $className = sprintf('%s\\%s', self::PARTS_NAMESPACE, $type);
233
234
        if ($strict) {
235
            return get_class($part) === $className;
236
        }
237
238
        return is_a($part, $className);
239
    }
240
}
241