Completed
Pull Request — master (#13)
by Andre
01:44
created

Name::isStrictType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace TheIconic\NameParser;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
7
class Name
8
{
9
    /**
10
     * @var array the parts that make up this name
11
     */
12
    protected $parts = [];
13
14
    /**
15
     * constructor takes the array of parts this name consists of
16
     *
17
     * @param array|null $parts
18
     */
19
    public function __construct(array $parts = null)
20
    {
21
        if (null !== $parts) {
22
            $this->setParts($parts);
23
        }
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    public function __toString(): string
30
    {
31
        return implode(' ', $this->getAll(true));
32
    }
33
34
    /**
35
     * set the parts this name consists of
36
     *
37
     * @param array $parts
38
     * @return $this
39
     */
40
    public function setParts(array $parts): Name
41
    {
42
        $this->parts = $parts;
43
44
        return $this;
45
    }
46
47
    /**
48
     * get the parts this name consists of
49
     *
50
     * @return array
51
     */
52
    public function getParts(): array
53
    {
54
        return $this->parts;
55
    }
56
57
    /**
58
     * @param bool $format
59
     * @return array
60
     */
61
    public function getAll(bool $format = false): array
62
    {
63
        $results = [];
64
        $keys = [
65
            'salutation' => [],
66
            'firstname' => [],
67
            'nickname' => [$format],
68
            'middlename' => [],
69
            'initials' => [],
70
            'lastname' => [],
71
            'suffix' => [],
72
        ];
73
74
        foreach ($keys as $key => $args) {
75
            $method = sprintf('get%s', ucfirst($key));
76
            if ($value = call_user_func_array(array($this, $method), $args)) {
77
                $results[$key] = $value;
78
            };
79
        }
80
81
        return $results;
82
    }
83
84
    /**
85
     * get the first name
86
     *
87
     * @return string
88
     */
89
    public function getFirstname(): string
90
    {
91
        return $this->export('Firstname');
92
    }
93
94
    /**
95
     * get the last name
96
     *
97
     * @param bool $pure
98
     * @return string
99
     */
100
    public function getLastname(bool $pure = false): string
101
    {
102
        return $this->export('Lastname', $pure);
103
    }
104
105
    /**
106
     * get the last name prefix
107
     *
108
     * @return string
109
     */
110
    public function getLastnamePrefix(): string
111
    {
112
        return $this->export('LastnamePrefix');
113
    }
114
115
    /**
116
     * get the initials
117
     *
118
     * @return string
119
     */
120
    public function getInitials(): string
121
    {
122
        return $this->export('Initial');
123
    }
124
125
    /**
126
     * get the suffix(es)
127
     *
128
     * @return string
129
     */
130
    public function getSuffix(): string
131
    {
132
        return $this->export('Suffix');
133
    }
134
135
    /**
136
     * get the salutation(s)
137
     *
138
     * @return string
139
     */
140
    public function getSalutation(): string
141
    {
142
        return $this->export('Salutation');
143
    }
144
145
    /**
146
     * get the nick name(s)
147
     *
148
     * @param bool $wrap
149
     * @return string
150
     */
151
    public function getNickname(bool $wrap = false): string
152
    {
153
        if ($wrap) {
154
            return sprintf('(%s)', $this->export('Nickname'));
155
        }
156
157
        return $this->export('Nickname');
158
    }
159
160
    /**
161
     * get the middle name(s)
162
     *
163
     * @return string
164
     */
165
    public function getMiddlename(): string
166
    {
167
        return $this->export('Middlename');
168
    }
169
170
    /**
171
     * helper method used by getters to extract and format relevant name parts
172
     *
173
     * @param string $type
174
     * @param bool $pure
0 ignored issues
show
Bug introduced by
There is no parameter named $pure. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
175
     * @return string
176
     */
177
    protected function export(string $type, bool $strict = false): string
178
    {
179
        $matched = [];
180
181
        foreach ($this->parts as $part) {
182
            if (!($part instanceof AbstractPart)) {
183
                continue;
184
            }
185
186
            $method = ($strict) ? 'isStrictType' : 'isType';
187
188
            if (call_user_func([$this, $method], $part, $type)) {
189
                $matched[] = $part->normalize();
190
            }
191
        }
192
193
        return implode(' ',  $matched);
194
    }
195
196
    /**
197
     * helper method to check if a part is of the given type
198
     *
199
     * @param AbstractPart $part
200
     * @param string $type
201
     * @return bool
202
     */
203
    protected function isType(AbstractPart $part, string $type) {
204
        return is_a($part, 'TheIconic\\NameParser\\Part\\' . $type);
205
    }
206
207
    /**
208
     * helper method to check if a part is the exact given type (not considering superclasses)
209
     *
210
     * @param AbstractPart $part
211
     * @param string $type
212
     * @return bool
213
     */
214
    protected function isStrictType(AbstractPart $part, string $type) {
215
        return get_class($part) === 'TheIconic\\NameParser\\Part\\' . $type;
216
    }
217
}
218