Skip to content

Commit

Permalink
Add generic unitOfUnit method
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 19, 2023
1 parent e49fabe commit 5a0dc7b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
36 changes: 34 additions & 2 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ public function get($name)
'microsecond' => 'u',
// @property int 0 (for Sunday) through 6 (for Saturday)
'dayOfWeek' => 'w',
// @property-read int 1 (for Monday) through 7 (for Sunday)
// @property int 1 (for Monday) through 7 (for Sunday)
'dayOfWeekIso' => 'N',
// @property int ISO-8601 week number of year, weeks starting on Monday
'weekOfYear' => 'W',
Expand Down Expand Up @@ -1308,6 +1308,16 @@ public function set($name, $value = null)

break;

case 'dayOfWeek':
$this->addDays($value - $this->dayOfWeek);

break;

case 'dayOfWeekIso':
$this->addDays($value - $this->dayOfWeekIso);

break;

case 'timestamp':
$this->setTimestamp($value);

Expand Down Expand Up @@ -1349,7 +1359,7 @@ public function set($name, $value = null)
], true) ? 1 : 0) + (int) floor($start->diffInUnit($firstUnit, $this));

// We check $value a posteriori to give precedence to UnknownUnitException
if (!is_int($value)) {
if (!\is_int($value)) {
throw new UnitException("->$name expects integer value");
}

Expand Down Expand Up @@ -1446,9 +1456,15 @@ public function getTranslatedShortMonthName($context = null)
/**
* Get/set the day of year.
*
* @template T of int|null
*
* @param int|null $value new value for day of year if using as setter.
*
* @psalm-param T $value
*
* @return static|int
*
* @psalm-return (T is int ? static : int)
*/
public function dayOfYear($value = null)
{
Expand Down Expand Up @@ -2763,6 +2779,22 @@ public function __call($method, $parameters)
}
}

if (preg_match('/^([a-z]{2,})(In|Of)([A-Z][a-z]+)$/', $method, $match)) {
$value = null;

try {
$value = isset($parameters[0])
? $this->set($method, $parameters[0])
: $this->get($method);
} catch (UnknownGetterException|UnknownSetterException) {
// continue to macro
}

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

return static::bindMacroContext($this, function () use (&$method, &$parameters) {
$macro = $this->getLocalMacro($method);

Expand Down
47 changes: 45 additions & 2 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Tests\Carbon;

use BadMethodCallException;
use Carbon\Carbon;
use Carbon\Exceptions\UnitException;
use DateTimeZone;
Expand Down Expand Up @@ -129,10 +130,52 @@ public function testSecondSetter()

public function testUnitOfUnit()
{
$date = Carbon::create(2018, 1, 27, 20, 12, 42, 'America/Toronto');
$date = Carbon::create(2023, 1, 27, 20, 12, 42, 'America/Toronto');
$date->minuteOfYear = (95 * 24 + 3) * 60 + 50;

$this->assertSame('2018-04-06 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));
$this->assertSame('2023-04-06 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));

$date->dayOfWeek = 2;

$this->assertSame('2023-04-04 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));

$date->dayOfWeek = 6;

$this->assertSame('2023-04-08 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));

$date->dayOfWeek = 0;

$this->assertSame('2023-04-02 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));

$date->dayOfWeekIso = 7;

$this->assertSame('2023-04-02 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));

$date->dayOfWeek = 4;

$this->assertSame('2023-04-06 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));

$date->dayOfWeekIso = 7;

$this->assertSame('2023-04-09 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));
}

public function testUnitOfUnitMethod()
{
$date = Carbon::create(2023, 1, 27, 20, 12, 42, 'America/Toronto');
$date->minuteOfYear((95 * 24 + 3) * 60 + 50);

Check failure on line 166 in tests/Carbon/SettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\Carbon::minuteOfYear().

Check failure on line 166 in tests/Carbon/SettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\Carbon::minuteOfYear().

$this->assertSame('2023-04-06 04:50:42 America/Toronto', $date->format('Y-m-d H:i:s e'));
}

public function testUnitOfUnitUnknownMethod()
{
$this->expectExceptionObject(new BadMethodCallException(
'Method fooOfBar does not exist.',
));

$date = Carbon::create(2023, 1, 27, 20, 12, 42, 'America/Toronto');
$date->fooOfBar((95 * 24 + 3) * 60 + 50);

Check failure on line 178 in tests/Carbon/SettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\Carbon::fooOfBar().

Check failure on line 178 in tests/Carbon/SettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\Carbon::fooOfBar().
}

public function testUnitOfUnitFloat()
Expand Down
21 changes: 21 additions & 0 deletions tests/CarbonImmutable/GettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,27 @@ public function testUnitOfUnit()
$this->assertSame(1, $date->dayOfMinute);
}

public function testUnitOfUnitMethod()
{
$date = Carbon::createFromDate(2018, 7, 6);
$this->assertSame(6, $date->dayOfQuarter());

Check failure on line 476 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::dayOfQuarter().

Check failure on line 476 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::dayOfQuarter().

$date = Carbon::createFromDate(2018, 8, 6);
$this->assertSame(6 + 31, $date->dayOfQuarter());

Check failure on line 479 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::dayOfQuarter().

Check failure on line 479 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::dayOfQuarter().

$date = Carbon::create(2018, 4, 6, 4, 50, 0, 'UTC');
$this->assertSame((95 * 24 + 4) * 60 + 50, $date->minuteOfYear());

Check failure on line 482 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::minuteOfYear().

Check failure on line 482 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::minuteOfYear().

$date = Carbon::create(2018, 4, 6, 4, 50, 0, 'America/Toronto');
$this->assertSame((95 * 24 + 3) * 60 + 50, $date->minuteOfYear());

Check failure on line 485 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::minuteOfYear().

Check failure on line 485 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::minuteOfYear().

$date = Carbon::create(2018, 4, 6, 4, 50, 0, 'America/Toronto');
$this->assertSame(0, $date->yearOfMinute());

Check failure on line 488 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::yearOfMinute().

Check failure on line 488 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::yearOfMinute().

$date = Carbon::create(2018, 4, 6, 4, 50, 0, 'America/Toronto');
$this->assertSame(1, $date->dayOfMinute());

Check failure on line 491 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::dayOfMinute().

Check failure on line 491 in tests/CarbonImmutable/GettersTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Call to an undefined method Carbon\CarbonImmutable::dayOfMinute().
}

public function testUnitInUnit()
{
$date = Carbon::createFromDate(2018, 7, 6);
Expand Down

0 comments on commit 5a0dc7b

Please sign in to comment.