Commit 7b2e264e authored by Hermann Mayer's avatar Hermann Mayer

Added own DateTime implementation which uses the PHP Intl Extension for…

Added own DateTime implementation which uses the PHP Intl Extension for localized formatting. Integrated this IntlDateTime class into a Doctrine DBAL Type, so it is now standard for all DateTime properties.
parent 1d11d2d8
......@@ -46,6 +46,9 @@ doctrine:
password: %database_password%
charset: UTF8
types:
datetime: Jity\HomepageBundle\Doctrine\Extension\DBAL\Types\IntlDateTimeType
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
......
......@@ -324,8 +324,8 @@ class BlogController extends Controller
$date = $entity['createdAt'];
$year = $date->format('Y');
$month = $date->format('F');
$monthNumber = $date->format('m');
$month = $date->format('MMMM');
$monthNumber = $date->format('M');
if (array_key_exists($year, $archivMap) &&
array_key_exists($monthNumber, $archivMap[$year])) {
......
<?php
/*
* This file is part of the Jity package.
*
* (c) Hermann Mayer <hermann.mayer92@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Jity\HomepageBundle\Doctrine\Extension\DBAL\Types;
/**
* IntlDateTime
*
* DateTime Implementation which use the Intl Extension for formatting.
*
* @author Hermann Mayer <hermann.mayer92@gmail.com>
*/
class IntlDateTime extends \DateTime
{
/**
* createFromFormat
*
* Returns new DateTime object formatted according to the specified format.
*
* @param string $format The format that the passed in string should be in
* @param string $time String representing the time
* @param DateTimeZone $timezone A DateTimeZone object representing the desired time zone
*
* @static
* @access public
* @return IntlDateTime
*/
static public function createFromFormat($format, $time, $timezone = null)
{
// Get the Late Static Binding Child (IntlDateTime)
$cur = new static();
$cur->setTimestamp(
parent::createFromFormat($format, $time)->getTimestamp()
);
if ($timezone instanceof \DateTimeZone) {
$cur->setTimezone($timezone);
}
return $cur;
}
/**
* format
*
* Returns date formatted according to given format.
*
* @param string $pattern Format accepted by date()
* @param string $locale Locale to use when formatting
*
* @access public
* @return string
*/
public function format($pattern, $locale = null)
{
if (!class_exists('IntlDateFormatter')) {
// We dont use the Intl Extension if we dont find it
return parent::format($pattern);
}
$formatter = \IntlDateFormatter::create(
$locale !== null ? $locale : \Locale::getDefault(),
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM,
null,
null,
(!empty($pattern)) ? $pattern : null
);
return $formatter->format($this);
}
}
<?php
/*
* This file is part of the Jity package.
*
* (c) Hermann Mayer <hermann.mayer92@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Jity\HomepageBundle\Doctrine\Extension\DBAL\Types;
use Doctrine\DBAL\Types\DateTimeType,
Doctrine\DBAL\Platforms\AbstractPlatform,
Doctrine\DBAL\Types\ConversionException;
/**
* IntlDateTimeType
*
* Doctrine DBAL Type which converts all entity DateTime
* properties to our own DateTime Implementation which
* use the Intl Extension for formatting.
*
* @uses DateTimeType
* @author Hermann Mayer <hermann.mayer92@gmail.com>
*/
class IntlDateTimeType extends DateTimeType
{
/**
* convertToPHPValue
*
* Converts a entity DateTime property to IntlDateTime.
*
* @param mixed $value Value to convert
* @param AbstractPlatform $platform Specific Platform
*
* @access public
* @return IntlDateTime|null
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value instanceof \DateTime) {
return $value;
}
$val = IntlDateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
if ( ! $val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
}
return $val;
}
}
article:
pattern: /{slug}.{_format}/{parameters}
defaults: { _controller: JityHomepageBundle:Blog:show, _format: html, parameters: ~ }
requirements:
_format: html|rss
parameters: ".+"
article_static_format:
pattern: /{slug}/{parameters}
defaults: { _controller: JityHomepageBundle:Blog:show, _format: html, parameters: ~ }
requirements:
parameters: ".+"
article_comment_create:
pattern: /comment/{id}/create/anonymous/{anonymous}
defaults: { _controller: "JityHomepageBundle:Blog:createComment", anonymous: true }
......@@ -26,3 +13,16 @@ article_comment_update:
defaults: { _controller: "JityHomepageBundle:Blog:updateComment" }
requirements: { _method: post }
article:
pattern: /{slug}.{_format}/{parameters}
defaults: { _controller: JityHomepageBundle:Blog:show, _format: html, parameters: ~ }
requirements:
_format: html|rss
parameters: ".+"
article_static_format:
pattern: /{slug}/{parameters}
defaults: { _controller: JityHomepageBundle:Blog:show, _format: html, parameters: ~ }
requirements:
parameters: ".+"
......@@ -6,7 +6,7 @@
<div class="blog-date pull-left">
<div class="blog-date-day">{{ article.createdAt|date('d') }}</div>
{{ article.createdAt|date('M Y') }}
{{ article.createdAt|date('MMM Y') }}
</div>
<div class="blog-article-preview-wrapper">
......
<?php
/*
* This file is part of the Jity package.
*
* (c) Hermann Mayer <hermann.mayer92@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Jity\HomepageBundle\Twig\Extension;
use Twig_Extension_Core,
Twig_Filter_Method,
Twig_Environment,
JMS\DiExtraBundle\Annotation as DI;
/**
* CoreExtension
*
* Core Twig extension to extend or override some defaults.
*
* @DI\Service("jity.twig.extension.core")
* @DI\Tag("twig.extension")
*
* @uses Twig_Extension_Core
* @author Hermann Mayer <hermann.mayer92@gmail.com>
*/
class CoreExtension extends Twig_Extension_Core
{
/**
* We dont need an default DateTime format.
* The default local setting will be used, or
* a format is given.
*
* @var dateFormats
* @access protected
*/
protected $dateFormats = array(null, '%d days');
/**
* getFilters
*
* Returns a list of filters to add to the existing list.
*
* @access public
* @return void
*/
public function getFilters()
{
return array_merge(parent::getFilters(), array(
// Override the default date filter
'date' => new Twig_Filter_Method($this, 'dateFormat', array(
'needs_environment' => true
))
));
}
/**
* dateFormat
*
* Wrap the default date filter, which exclusion of the
* format flag. A empty default format will be filled by
* local specific format with the help of IntlDateTime.
*
* @param Twig_Environment $env Twig Environment
* @param int|DateTime $date DateTime Object or Timestamp to format
* @param string $format Optional Pattern for Formatting
*
* @access public
* @return string
*/
public function dateFormat(Twig_Environment $env, $date, $format = null)
{
return twig_date_format_filter($env, $date, $format);
}
}
......@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Jity\HomepageBundle\Twig;
namespace Jity\HomepageBundle\Twig\Extension;
use Twig_Extension,
Twig_Filter_Method,
......@@ -27,7 +27,7 @@ use Twig_Extension,
*
* Twig extension to parse dynamically loaded templates.
*
* @DI\Service("jity.twig.jity_extension")
* @DI\Service("jity.twig.extension.jity")
* @DI\Tag("twig.extension")
*
* @uses Twig_Extension
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment