<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

require_once __DIR__ . '/../../../TestInit.php';

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
 * @group DDC-1998
 */
class DDC1998Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
    protected function setUp()
    {
        parent::setUp();

        Type::addType(DDC1998EwtTimestamp::MYTYPE, __NAMESPACE__ . '\DDC1998EwtTimestamp');

        $this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
        $this->_schemaTool->createSchema(array(
            $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1998ProductTemplate'),
        ));
    }

    public function testIssue()
    {
        $p1                     = new DDC1998ProductTemplate();
        $p1->productTemplateID  = 1;
        $p1->userID             = 1;
        $p1->name               = "P1";
        $p1->active             = true;
        $p1->dateFrom           = time();
        $p1->dateLocked         = strtotime("+ 1 days");
        $p1->dateTo             = strtotime("+ 2 days");

        $this->_em->persist($p1);
        $this->_em->flush();

        $this->_em->remove($p1);
        $this->_em->flush();
    }
}

class DDC1998EwtTimestamp extends Type
{
    const MYTYPE = 'EwtTimestamp'; // modify to match your type name

    /**
     * {@inheritdoc}
     */
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return $platform->getDateTypeDeclarationSQL($fieldDeclaration);
    }

    /**
     * {@inheritdoc}
     */
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if ($value === null) {
            return null;
        }

        return date($platform->getDateTimeFormatString(), $value);
    }

    /**
     * {@inheritdoc}
     */
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {

        if ($value === null) {
            return null;
        }

        return \DateTime::createFromFormat($platform->getDateFormatString(), $value)
            ->getTimestamp();
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return self::MYTYPE;
    }
}

/**
 * @Entity
 * @Table
 */
class DDC1998ProductTemplate
{
    /**
     * @Id
     * @Column (name="productTemplateID", type="integer")
     *
     */
    public $productTemplateID;

    /**
     * @Id
     * @Column (name="dateFrom", type="EwtTimestamp")
     */
    public $dateFrom;

    /**
     * @Column (name="name", type="string", length=255)
     */
    public $name;

    /**
     * @Column (name="active", type="boolean")
     */
    public $active;

    /**
     * @Column (name="dateTo", type="EwtTimestamp", nullable=true)
     */
    public $dateTo;

    /**
     * @Column (name="dateLocked", type="EwtTimestamp", nullable=true)
     */
    public $dateLocked;

    /**
     * @Column (name="userID", type="integer", nullable=true)
     */
    public $userID;

}