Passed
Push — master ( b4c5df...a8f09d )
by Magnar Ovedal
02:39
created

FormatterChaining::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
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\DateFormatter;
6
7
use DateTimeInterface;
8
use Stadly\PasswordPolice\DateFormatter;
9
use Stadly\PasswordPolice\WordFormatter;
10
use Traversable;
11
12
trait FormatterChaining
13
{
14
    /**
15
     * @var WordFormatter|null Next formatter in the chain.
16
     */
17
    private $next;
18
19
    /**
20
     * {@inheritDoc}
21
     */
22 2
    public function setNext(?WordFormatter $next): void
23
    {
24 2
        $this->next = $next;
25 2
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 2
    public function getNext(): ?WordFormatter
31
    {
32 2
        return $this->next;
33
    }
34
35
    /**
36
     * @param iterable<DateTimeInterface> $dates Dates to format.
37
     * @return Traversable<string> The dates formatted by the formatter chain. May contain duplicates.
38
     */
39 7
    public function apply(iterable $dates): Traversable
40
    {
41 7
        if ($this->next === null) {
42 4
            yield from $this->applyCurrent($dates);
43
        } else {
44 4
            yield from $this->next->apply($this->applyCurrent($dates));
45
        }
46 7
    }
47
48
    /**
49
     * @param iterable<DateTimeInterface> $dates Dates to format.
50
     * @return Traversable<string> The dates formatted by this date formatter. May contain duplicates.
51
     */
52
    abstract protected function applyCurrent(iterable $dates): Traversable;
53
}
54