Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
2 / 4
CRAP
86.67% covered (warning)
86.67%
13 / 15
Chain
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
2 / 4
8.15
86.67% covered (warning)
86.67%
13 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 addMapper
0.00% covered (danger)
0.00%
0 / 1
2.06
75.00% covered (warning)
75.00%
3 / 4
 addMappers
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 map
0.00% covered (danger)
0.00%
0 / 1
3.07
80.00% covered (warning)
80.00%
4 / 5
<?php
namespace Popy\Calendar\Parser\ResultMapper;
use Popy\Calendar\Parser\DateLexerResult;
use Popy\Calendar\Parser\ResultMapperInterface;
use Popy\Calendar\ValueObject\DateRepresentationInterface;
/**
 * Chain implementation.
 */
class Chain implements ResultMapperInterface
{
    /**
     * Mapper chain.
     *
     * @var array<ResultMapperInterface>
     */
    protected $mappers = [];
    
    /**
     * Class constructor.
     *
     * @param iterable<ResultMapperInterface> $mappers Mapper chain.
     */
    public function __construct($mappers = [])
    {
        $this->addMappers($mappers);
    }
    
    /**
     * Adds a Mapper to the chain.
     *
     * @param ResultMapperInterface $mapper
     */
    public function addMapper(ResultMapperInterface $mapper)
    {
        if ($mapper instanceof self) {
            return $this->addMappers($mapper->mappers);
        }
        $this->mappers[] = $mapper;
    
        return $this;
    }
    
    /**
     * Add mappers to the chain.
     *
     * @param iterable<ResultMapperInterface> $mappers
     */
    public function addMappers($mappers)
    {
        foreach ($mappers as $mapper) {
            $this->addMapper($mapper);
        }
    
        return $this;
    }
    /**
     * @inheritDoc
     */
    public function map(DateLexerResult $parts, DateRepresentationInterface $date)
    {
        foreach ($this->mappers as $mapper) {
            if (null === $date = $mapper->map($parts, $date)) {
                return;
            }
        }
        return $date;
    }
}