Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
CRAP
87.50% covered (warning)
87.50%
14 / 16
Chain
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
8.12
87.50% covered (warning)
87.50%
14 / 16
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 addFormatter
0.00% covered (danger)
0.00%
0 / 1
2.26
60.00% covered (warning)
60.00%
3 / 5
 addFormatters
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 formatSymbol
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
<?php
namespace Popy\Calendar\Formatter\SymbolFormatter;
use Popy\Calendar\FormatterInterface;
use Popy\Calendar\Parser\FormatToken;
use Popy\Calendar\Formatter\SymbolFormatterInterface;
use Popy\Calendar\ValueObject\DateRepresentationInterface;
/**
 * Chain/Collection implementation. Will delegate to internal formatters until
 * one of them returns a result.
 */
class Chain implements SymbolFormatterInterface
{
    /**
     * Formatter chain.
     *
     * @var array<SymbolFormatterInterface>
     */
    protected $formatters = [];
    /**
     * Class constructor.
     *
     * @param iterable<SymbolFormatterInterface> $formatters
     */
    public function __construct($formatters = [])
    {
        $this->addFormatters($formatters);
    }
    /**
     * Adds a formatter to the chain.
     *
     * @param SymbolFormatterInterface $formatter
     */
    public function addFormatter(SymbolFormatterInterface $formatter)
    {
        if ($formatter instanceof self) {
            // Reducing recursivity
            $this->addFormatters($formatter->formatters);
        } else {
            $this->formatters[] = $formatter;
        }
        return $this;
    }
    /**
     * Add formatters to the chain.
     *
     * @param iterable<SymbolFormatterInterface> $formatters
     */
    public function addFormatters($formatters)
    {
        foreach ($formatters as $formatter) {
            $this->addFormatter($formatter);
        }
        return $this;
    }
    /**
     * @inheritDoc
     */
    public function formatSymbol(
        DateRepresentationInterface $input,
        FormatToken $token,
        FormatterInterface $formatter
    ) {
        foreach ($this->formatters as $f) {
            if (null !== $res = $f->formatSymbol($input, $token, $formatter)) {
                return $res;
            }
        }
    }
}