Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
CRAP
34.29% covered (danger)
34.29%
12 / 35
SimpleWeeks
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
38.38
34.29% covered (danger)
34.29%
12 / 35
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 fromUnixTime
0.00% covered (danger)
0.00%
0 / 1
3.01
90.91% covered (success)
90.91%
10 / 11
 toUnixTime
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 22
<?php
namespace Popy\Calendar\Converter\UnixTimeConverter;
use Popy\Calendar\Converter\Conversion;
use Popy\Calendar\Converter\UnixTimeConverterInterface;
use Popy\Calendar\ValueObject\DateSolarRepresentationInterface;
use Popy\Calendar\ValueObject\DateFragmentedRepresentationInterface;
/**
 * Simple weeks.
 */
class SimpleWeeks implements UnixTimeConverterInterface
{
    /**
     * Week length.
     *
     * @var integer
     */
    protected $length;
    /**
     * Class constructor.
     *
     * @param integer $length Week length.
     */
    public function __construct($length)
    {
        $this->length = $length;
    }
    /**
     * @inheritDoc
     */
    public function fromUnixTime(Conversion $conversion)
    {
        $input = $conversion->getTo();
        if (
            !$input instanceof DateFragmentedRepresentationInterface
            || !$input instanceof DateSolarRepresentationInterface
        ) {
            return;
        }
        $dateParts = $input->getDateParts()->withTransversals([
            $input->getYear(),
            intval($input->getDayIndex() / $this->length),
            $input->getDayIndex() % $this->length
        ]);
        $conversion->setTo($input->withDateParts($dateParts));
    }
    /**
     * @inheritDoc
     */
    public function toUnixTime(Conversion $conversion)
    {
        $input = $conversion->getTo();
        if (
            !$input instanceof DateFragmentedRepresentationInterface
            || !$input instanceof DateSolarRepresentationInterface
        ) {
            return;
        }
        $parts = $input->getDateParts();
        if (null === $input->getYear()) {
            $input = $input->withYear(
                $parts->getTransversal(0),
                $input->isLeapYear()
            );
        }
        if (
            null === $input->getDayIndex()
            && null !== $weekIndex = $parts->getTransversal(1)
        ) {
            $dayOfWeek = (int)$parts->getTransversal(2);
            $input = $input->withDayIndex(
                $dayOfWeek + $weekIndex * $this->length,
                $input->getEraDayIndex()
            );
        }
        $conversion->setTo($input);
    }
}