<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\DataTransformer;

use Symfony\Component\Form\DataTransformerInterface;

class TransformDateOfToIntAdditional implements DataTransformerInterface
{
    public function transform($value): mixed
    {
        if ($value) {
            if (mb_strlen($value) < 4) {
                $month = '0'.mb_substr($value, 0, 1);
                $day = mb_substr($value, 1, 2);
            } else {
                $month = mb_substr($value, 0, 2);
                $day = mb_substr($value, 2, 2);
            }
            $date = $day.'-'.$month.'-'.date('Y');

            return new \DateTime($date);
        }

        $date = '01-01-'.date('Y');

        return new \DateTime($date);
    }

    public function reverseTransform($value): mixed
    {
        if ($value) {
            return date('nd', $value->getTimestamp());
        }

        return '101';
    }
}
