From b636587945443b08d363b0f38440245746ba5e1d Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Fri, 15 Jun 2018 11:04:12 +1200 Subject: [PATCH] Respect semver and add tests --- src/ORM/FieldType/DBDate.php | 11 +++++++++-- src/ORM/FieldType/DBDatetime.php | 7 ++++--- tests/php/ORM/DBDatetimeTest.php | 20 ++++++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/ORM/FieldType/DBDate.php b/src/ORM/FieldType/DBDate.php index 920eb905f..13716b7eb 100644 --- a/src/ORM/FieldType/DBDate.php +++ b/src/ORM/FieldType/DBDate.php @@ -37,6 +37,8 @@ class DBDate extends DBField /** * Fixed locale to use for ISO date formatting. This is necessary to prevent * locale-specific numeric localisation breaking internal date strings. + * + * @internal (remove internal in 4.2) */ const ISO_LOCALE = 'en_US'; @@ -214,6 +216,8 @@ class DBDate extends DBField /** * Return formatter in a given locale. Useful if localising in a format other than the current locale. * + * @internal (Remove internal in 4.2) + * * @param string|null $locale The current locale, or null to use default * @param string|null $pattern Custom pattern to use for this, if required * @param int $dateLength @@ -262,11 +266,14 @@ class DBDate extends DBField * for the day of the month ("1st", "2nd", "3rd" etc) * * @param string $format Format code string. See http://userguide.icu-project.org/formatparse/datetime - * @param string $locale Custom locale to use + * @param string $locale Custom locale to use (add to signature in 5.0) * @return string The date in the requested format */ - public function Format($format, $locale = null) + public function Format($format) { + // Note: soft-arg uses func_get_args() to respect semver. Add to signature in 5.0 + $locale = func_num_args() > 1 ? func_get_arg(1) : null; + if (!$this->value) { return null; } diff --git a/src/ORM/FieldType/DBDatetime.php b/src/ORM/FieldType/DBDatetime.php index e56d99265..dd9c97607 100644 --- a/src/ORM/FieldType/DBDatetime.php +++ b/src/ORM/FieldType/DBDatetime.php @@ -2,15 +2,14 @@ namespace SilverStripe\ORM\FieldType; +use Exception; use IntlDateFormatter; +use InvalidArgumentException; use SilverStripe\Forms\DatetimeField; -use SilverStripe\i18n\i18n; use SilverStripe\ORM\DB; use SilverStripe\Security\Member; use SilverStripe\Security\Security; use SilverStripe\View\TemplateGlobalProvider; -use Exception; -use InvalidArgumentException; /** * Represents a date-time field. @@ -233,6 +232,8 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider /** * Return formatter in a given locale. Useful if localising in a format other than the current locale. * + * @internal (Remove internal in 4.2) + * * @param string|null $locale The current locale, or null to use default * @param string|null $pattern Custom pattern to use for this, if required * @param int $dateLength diff --git a/tests/php/ORM/DBDatetimeTest.php b/tests/php/ORM/DBDatetimeTest.php index a824ecdae..fd0bd91db 100644 --- a/tests/php/ORM/DBDatetimeTest.php +++ b/tests/php/ORM/DBDatetimeTest.php @@ -2,10 +2,9 @@ namespace SilverStripe\ORM\Tests; +use SilverStripe\Dev\SapphireTest; use SilverStripe\i18n\i18n; use SilverStripe\ORM\FieldType\DBDatetime; -use SilverStripe\Dev\SapphireTest; -use SilverStripe\Security\Member; /** * Tests for {@link Datetime} class. @@ -70,6 +69,23 @@ class DBDatetimeTest extends SapphireTest $this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d MMM y H m s')); } + /** + * Coverage for dates using hindi-numerals + */ + public function testHindiNumerals() + { + // Parent locale is english; Can be localised to arabic + $date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24'); + $this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s')); + $this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s', 'ar')); + + // Parent locale is arabic; Datavalue uses ISO date + i18n::set_locale('ar'); + $date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24'); + $this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s')); + $this->assertEquals('1600-10-10 15:32:24', $date->getValue()); + } + public function testNice() { $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');