Passed
Push — master ( e01057...7ac505 )
by Magnar Ovedal
03:13
created

Series::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\WordConverter;
6
7
use InvalidArgumentException;
8
use Stadly\PasswordPolice\WordConverter;
9
use Traversable;
10
11
final class Series implements WordConverter
12
{
13
    /**
14
     * @var WordConverter[] Word converters.
15
     */
16
    private $wordConverters;
17
18
    /**
19
     * @param WordConverter ...$wordConverters Word converters.
20
     */
21 3
    public function __construct(WordConverter ...$wordConverters)
22
    {
23 3
        if ($wordConverters === []) {
24 1
            throw new InvalidArgumentException('At least one word converter must be specified.');
25
        }
26
27 2
        $this->wordConverters = $wordConverters;
28 2
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 2
    public function convert(string $word): Traversable
34
    {
35 2
        yield from $this->convertWord($word, $this->wordConverters);
36 2
    }
37
38 2
    private function convertWord(string $word, array $wordConverters): Traversable
39
    {
40 2
        $wordConverter = array_shift($wordConverters);
41
42 2
        if ($wordConverters === []) {
43 2
            yield from $wordConverter->convert($word);
44 2
            return;
45
        }
46
47 1
        foreach ($wordConverter->convert($word) as $converted) {
48 1
            yield from $this->convertWord($converted, $wordConverters);
49
        }
50 1
    }
51
}
52