Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unmodified English for period conversion #2548

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
},
"config": {
"process-timeout": 0,
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"extra": {
"branch-alias": {
Expand Down
5 changes: 0 additions & 5 deletions phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
<exclude name="CouplingBetweenObjects" />
<exclude name="CountInLoopExpression" />
</rule>
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
<properties>
<property name="maximum" value="25" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable">
<properties>
<property name="exceptions" value="ci,id,to,tz" />
Expand Down
7 changes: 4 additions & 3 deletions src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ abstract class AbstractTranslator extends Translation\Translator
public static function get($locale = null)
{
$locale = $locale ?: 'en';
$key = static::class === Translator::class ? $locale : static::class.'|'.$locale;

if (!isset(static::$singletons[$locale])) {
static::$singletons[$locale] = new static($locale);
if (!isset(static::$singletons[$key])) {
static::$singletons[$key] = new static($locale);
}

return static::$singletons[$locale];
return static::$singletons[$key];
}

public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
Expand Down
7 changes: 6 additions & 1 deletion src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
* @method $this floorMicroseconds(float $precision = 1) Truncate the current instance microsecond with given precision.
* @method $this ceilMicrosecond(float $precision = 1) Ceil the current instance microsecond with given precision.
* @method $this ceilMicroseconds(float $precision = 1) Ceil the current instance microsecond with given precision.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CarbonPeriod implements Iterator, Countable, JsonSerializable
{
Expand Down Expand Up @@ -2179,7 +2181,10 @@ public function ceilUnit($unit, $precision = 1)
*/
public function round($precision = null, $function = 'round')
{
return $this->roundWith($precision ?? (string) $this->getDateInterval(), $function);
return $this->roundWith(
$precision ?? $this->getDateInterval()->setLocalTranslator(TranslatorImmutable::get('en'))->forHumans(),
$function
);
}

/**
Expand Down
99 changes: 99 additions & 0 deletions src/Carbon/TranslatorImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon;

use Carbon\Exceptions\ImmutableException;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;

class TranslatorImmutable extends Translator
{
/** @var bool */
private $constructed = false;

public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
{
parent::__construct($locale, $formatter, $cacheDir, $debug);
$this->constructed = true;
}

/**
* @codeCoverageIgnore
*/
public function setDirectories(array $directories)
{
$this->disallowMutation(__METHOD__);

return parent::setDirectories($directories);
}

public function setLocale($locale)
{
$this->disallowMutation(__METHOD__);

return parent::setLocale($locale);
}

/**
* @codeCoverageIgnore
*/
public function setMessages($locale, $messages)
{
$this->disallowMutation(__METHOD__);

return parent::setMessages($locale, $messages);
}

/**
* @codeCoverageIgnore
*/
public function setTranslations($messages)
{
$this->disallowMutation(__METHOD__);

return parent::setTranslations($messages);
}

/**
* @codeCoverageIgnore
*/
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
{
$this->disallowMutation(__METHOD__);

parent::setConfigCacheFactory($configCacheFactory);
}

public function resetMessages($locale = null)
{
$this->disallowMutation(__METHOD__);

return parent::resetMessages($locale);
}

/**
* @codeCoverageIgnore
*/
public function setFallbackLocales(array $locales)
{
$this->disallowMutation(__METHOD__);

parent::setFallbackLocales($locales);
}

private function disallowMutation($method)
{
if ($this->constructed) {
throw new ImmutableException($method.' not allowed on '.static::class);
}
}
}
39 changes: 39 additions & 0 deletions tests/CarbonPeriod/ToDatePeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Tests\CarbonPeriod;

use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Carbon\Translator;
use DatePeriod;
use Tests\AbstractTestCase;

Expand Down Expand Up @@ -58,4 +60,41 @@ public function testToArrayIsNotEmptyArray()
$this->assertSame('2021-01-05', $newInstance->getStartDate()->format('Y-m-d'));
$this->assertSame(3, $newInstance->getRecurrences());
}

public function testWithIntervalLocalized()
{
CarbonInterval::setLocale('fr');
$period = CarbonPeriod::create('2021-01-05', 3);
$result = $period->floor()->toDatePeriod();

$this->assertSame(DatePeriod::class, \get_class($result));
$this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
$this->assertNull($result->getEndDate());

if (method_exists($result, 'getRecurrences')) {
$this->assertSame(3, $result->getRecurrences());
}

CarbonInterval::setLocale('en');
}

public function testWithModifiedEnglish()
{
$translator = Translator::get('en');
$translator->setTranslations([
'day' => ':count boring day|:count boring days',
]);
$period = CarbonPeriod::create('2021-01-05', 3);
$result = $period->floor()->toDatePeriod();

$this->assertSame(DatePeriod::class, \get_class($result));
$this->assertSame('2021-01-05', $result->getStartDate()->format('Y-m-d'));
$this->assertNull($result->getEndDate());

if (method_exists($result, 'getRecurrences')) {
$this->assertSame(3, $result->getRecurrences());
}

$translator->resetMessages();
}
}
10 changes: 10 additions & 0 deletions tests/Language/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
namespace Tests\Language;

use Carbon\Carbon;
use Carbon\Exceptions\ImmutableException;
use Carbon\Exceptions\NotLocaleAwareException;
use Carbon\Translator;
use Carbon\TranslatorImmutable;
use ReflectionMethod;
use Tests\AbstractTestCase;

Expand Down Expand Up @@ -76,4 +78,12 @@ public function testNotLocaleAwareException()
$exception->getMessage()
);
}

public function testTranslatorImmutable()
{
$this->expectExceptionObject(
new ImmutableException('setTranslations not allowed on '.TranslatorImmutable::class)
);
TranslatorImmutable::get('en')->setTranslations([]);
}
}