Completed
Push — master ( c4f174...a33cb5 )
by Andre
02:14
created

AbstractMapper::findFirstMapped()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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