Completed
Pull Request — master (#22)
by
unknown
02:30
created

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