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

Name::getGivenName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
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
    public function getGivenName(): string
88
    {
89
        $fullNameParts = [];
90
91
        foreach ($this->parts as $part) {
92
            if ($part instanceof GivenNamePart) {
93
                $fullNameParts[] = $part->normalize();
94
            }
95
        }
96
97
        return implode(' ', $fullNameParts);
98
    }
99
100
    /**
101
     * get the first name
102
     *
103
     * @return string
104
     */
105
    public function getFirstname(): string
106
    {
107
        return $this->export('Firstname');
108
    }
109
110
    /**
111
     * get the last name
112
     *
113
     * @param bool $pure
114
     * @return string
115
     */
116
    public function getLastname(bool $pure = false): string
117
    {
118
        return $this->export('Lastname', $pure);
119
    }
120
121
    /**
122
     * get the last name prefix
123
     *
124
     * @return string
125
     */
126
    public function getLastnamePrefix(): string
127
    {
128
        return $this->export('LastnamePrefix');
129
    }
130
131
    /**
132
     * get the initials
133
     *
134
     * @return string
135
     */
136
    public function getInitials(): string
137
    {
138
        return $this->export('Initial');
139
    }
140
141
    /**
142
     * get the suffix(es)
143
     *
144
     * @return string
145
     */
146
    public function getSuffix(): string
147
    {
148
        return $this->export('Suffix');
149
    }
150
151
    /**
152
     * get the salutation(s)
153
     *
154
     * @return string
155
     */
156
    public function getSalutation(): string
157
    {
158
        return $this->export('Salutation');
159
    }
160
161
    /**
162
     * get the nick name(s)
163
     *
164
     * @param bool $wrap
165
     * @return string
166
     */
167
    public function getNickname(bool $wrap = false): string
168
    {
169
        if ($wrap) {
170
            return sprintf('(%s)', $this->export('Nickname'));
171
        }
172
173
        return $this->export('Nickname');
174
    }
175
176
    /**
177
     * get the middle name(s)
178
     *
179
     * @return string
180
     */
181
    public function getMiddlename(): string
182
    {
183
        return $this->export('Middlename');
184
    }
185
186
    /**
187
     * helper method used by getters to extract and format relevant name parts
188
     *
189
     * @param string $type
190
     * @param bool $strict
191
     * @return string
192
     */
193
    protected function export(string $type, bool $strict = false): string
194
    {
195
        $matched = [];
196
197
        foreach ($this->parts as $part) {
198
            if ($part instanceof AbstractPart && $this->isType($part, $type, $strict)) {
199
                $matched[] = $part->normalize();
200
            }
201
        }
202
203
        return implode(' ',  $matched);
204
    }
205
206
    /**
207
     * helper method to check if a part is of the given type
208
     *
209
     * @param AbstractPart $part
210
     * @param string $type
211
     * @param bool $strict
212
     * @return bool
213
     */
214
    protected function isType(AbstractPart $part, string $type, bool $strict = false): bool
215
    {
216
        $className = sprintf('%s\\%s', self::PARTS_NAMESPACE, $type);
217
218
        if ($strict) {
219
            return get_class($part) === $className;
220
        }
221
222
        return is_a($part, $className);
223
    }
224
}
225