<?php
namespace Newland\Toubiz\Sync\Neos\Importer;

/*
 * This file is part of the "toubiz-sync-neos" package.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 */

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;

/**
 * Abstract importer class.
 *
 * @Flow\Scope("singleton")
 */
abstract class AbstractImporter
{
    /**
     * @Flow\Inject
     * @var PersistenceManagerInterface
     */
    protected $persistenceManager;

    /**
     * @var mixed
     */
    protected $data;

    /**
     * @var string
     */
    protected $language;

    public function setLanguage(string $language): void
    {
        $this->language = $language;
    }

    /**
     * Main import method.
     *
     * Must be defined in each implementation.
     *
     * @param mixed $data
     * @return mixed
     */
    abstract public function import($data);

    /**
     * Returns a DateTime instance for given object.
     *
     * @param mixed $object
     * @return \DateTime
     */
    protected function asDateTime($object): \DateTime
    {
        if (is_string($object)) {
            return new \DateTime($object);
        }

        return $object;
    }

    /**
     * Converts date and time strings into a DateTime object.
     *
     * @param string $date A date in YYYY-MM-DD format
     * @param string $time A time in hh:mm:ss format
     * @return \DateTime
     */
    protected function buildDateTimeFromStrings($date, $time = ''): \DateTime
    {
        $object = new \DateTime($date);
        if ($time) {
            list($hour, $minute) = explode(':', $time);
            $object->setTime((int) $hour, (int) $minute);
        }

        return $object;
    }
}
