Completed
Pull Request — master (#3)
by Andre
06:23
created

AbstractMapper::map()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace TheIconic\NameParser\Mapper;
4
5
use TheIconic\NameParser\Part\AbstractPart;
6
7
abstract class AbstractMapper
8
{
9
    /**
10
     * implements the mapping of parts
11
     *
12
     * @param array $parts - the name parts
13
     * @return array $parts - the mapped parts
14
     */
15
    abstract public function map(array $parts);
16
17
    /**
18
     * checks if there are still unmapped parts left before the given position
19
     *
20
     * @param array $parts
21
     * @param $index
22
     * @return bool
23
     */
24
    protected function hasUnmappedPartsBefore(array $parts, $index): bool
25
    {
26
        foreach ($parts as $k => $part) {
27
            if ($k === $index) {
28
                break;
29
            }
30
31
            if (!($part instanceof AbstractPart)) {
32
                return true;
33
            }
34
        }
35
36
        return false;
37
    }
38
39
    /**
40
     * @param string $type
41
     * @param array $parts
42
     * @return int|bool
43
     */
44
    protected function findFirstMapped(string $type, array $parts)
45
    {
46
        $total = count($parts);
47
48
        for ($i = 0; $i < $total; $i++) {
49
            if ($parts[$i] instanceof $type) {
50
                return $i;
51
            }
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * get the registry lookup key for the given word
59
     *
60
     * @param string $word the word
61
     * @return string the key
62
     */
63
    protected function getKey($word): string
64
    {
65
        return strtolower(str_replace('.', '', $word));
66
    }
67
}
68