Completed
Push — master ( 310596...ecf895 )
by Hannes
02:21
created

FirstOf::match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\autogiro\Parser\Matcher;
6
7
use byrokrat\autogiro\Line;
8
use byrokrat\autogiro\Exception\InvalidContentException;
9
10
/**
11
 * Grab content of the first valid sub-matcher
12
 */
13
class FirstOf implements Matcher
14
{
15
    /**
16
     * @var Matcher[] Loaded matchers
17
     */
18
    private $matchers;
19
20 2
    public function __construct(Matcher ...$matchers)
21
    {
22 2
        $this->matchers = $matchers;
23 2
    }
24
25 2
    public function match(Line $line): string
26
    {
27 2
        foreach ($this->matchers as $matcher) {
28
            try {
29 1
                return $matcher->match($line);
30 1
            } catch (InvalidContentException $exception) {
31
                // Skipping invalid matches in favour of first valid match
32
            }
33
        }
34
35 1
        throw new InvalidContentException(
36 1
            'Unable to find a match using a set of matchers'
37
        );
38
    }
39
}
40