chrisandchris/passive-record-orm

View on GitHub
src/ChrisAndChris/Common/RowMapperBundle/Entity/AbstractEntity.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
namespace ChrisAndChris\Common\RowMapperBundle\Entity;

/**
 * @name AbstractEntity
 * @version    1.0.0
 * @since      v2.1.0
 * @package    RowMapperBundle
 * @author     ChrisAndChris
 * @link       https://github.com/chrisandchris
 */
abstract class AbstractEntity {

    /**
     * Parses and formats a date using the DateTime-format method
     *
     * @param \DateTime $date
     * @param string    $format
     * @return null|string
     */
    public function _formatDate($date, $format = 'Y-m-d') {
        $date = $this->_parseDate($date);

        return $date->format($format);
    }

    /**
     * Parse a date string (or DateTime object)
     *
     * @param $date
     * @return \DateTime|null
     */
    public function _parseDate($date) {
        if ($date == null) {
            return null;
        }
        if ($date instanceof \DateTime) {
            return $date;
        }

        return new \DateTime($date);
    }
}