Chain   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addMappers() 0 7 2
A __construct() 0 3 1
A addMapper() 0 9 2
A map() 0 9 3
1
<?php
2
3
namespace Popy\Calendar\Parser\ResultMapper;
4
5
use Popy\Calendar\Parser\DateLexerResult;
6
use Popy\Calendar\Parser\ResultMapperInterface;
7
use Popy\Calendar\ValueObject\DateRepresentationInterface;
8
9
/**
10
 * Chain implementation.
11
 */
12
class Chain implements ResultMapperInterface
13
{
14
    /**
15
     * Mapper chain.
16
     *
17
     * @var array<ResultMapperInterface>
18
     */
19
    protected $mappers = [];
20
    
21
    /**
22
     * Class constructor.
23
     *
24
     * @param iterable<ResultMapperInterface> $mappers Mapper chain.
25
     */
26
    public function __construct($mappers = [])
27
    {
28
        $this->addMappers($mappers);
29
    }
30
    
31
    /**
32
     * Adds a Mapper to the chain.
33
     *
34
     * @param ResultMapperInterface $mapper
35
     */
36
    public function addMapper(ResultMapperInterface $mapper)
37
    {
38
        if ($mapper instanceof self) {
39
            return $this->addMappers($mapper->mappers);
40
        }
41
42
        $this->mappers[] = $mapper;
43
    
44
        return $this;
45
    }
46
    
47
    /**
48
     * Add mappers to the chain.
49
     *
50
     * @param iterable<ResultMapperInterface> $mappers
51
     */
52
    public function addMappers($mappers)
53
    {
54
        foreach ($mappers as $mapper) {
55
            $this->addMapper($mapper);
56
        }
57
    
58
        return $this;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function map(DateLexerResult $parts, DateRepresentationInterface $date)
65
    {
66
        foreach ($this->mappers as $mapper) {
67
            if (null === $date = $mapper->map($parts, $date)) {
68
                return;
69
            }
70
        }
71
72
        return $date;
73
    }
74
}
75