Passed
Push — master ( 3db342...8e1c26 )
by Magnar Ovedal
05:52 queued 01:22
created

ChainableFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 41
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 6 2
A getNext() 0 3 1
A setNext() 0 3 1
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
abstract class ChainableFormatter implements DateFormatter
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 2
    public function apply(iterable $dates): Traversable
40
    {
41 2
        if ($this->next === null) {
42 1
            yield from $this->applyCurrent($dates);
43
        } else {
44 1
            yield from $this->next->apply($this->applyCurrent($dates));
45
        }
46 2
    }
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