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

FirstOf::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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