Completed
Push — master ( 0bf85f...81de7c )
by Andre
10s
created

Name::isType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 3
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
    /**
173
     * helper method used by getters to extract and format relevant name parts
174
     *
175
     * @param string $type
176
     * @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...
177
     * @return string
178
     */
179
    protected function export(string $type, bool $strict = false): string
180
    {
181
        $matched = [];
182
183
        foreach ($this->parts as $part) {
184
            if ($part instanceof AbstractPart && $this->isType($part, $type, $strict)) {
185
                $matched[] = $part->normalize();
186
            }
187
        }
188
189
        return implode(' ',  $matched);
190
    }
191
192
    /**
193
     * helper method to check if a part is of the given type
194
     *
195
     * @param AbstractPart $part
196
     * @param string $type
197
     * @param bool $strict
198
     * @return bool
199
     */
200
    protected function isType(AbstractPart $part, string $type, bool $strict = false): bool
201
    {
202
        $className = sprintf('%s\\%s', self::PARTS_NAMESPACE, $type);
203
204
        if ($strict) {
205
            return get_class($part) === $className;
206
        }
207
208
        return is_a($part, $className);
209
    }
210
}
211