Skip to content

Commit

Permalink
Merge pull request #3036 from briannesbitt/feature/is-with-enum
Browse files Browse the repository at this point in the history
Add support for Month and WeekDay enums in is() method
  • Loading branch information
kylekatarnls authored Jun 20, 2024
2 parents 415782b + 2ae23c9 commit b3d0d94
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,7 @@ public static function instance(DateTimeInterface $date): static;
*
* @param string $tester day name, month name, hour, date, etc. as string
*/
public function is(string $tester): bool;
public function is(WeekDay|Month|string $tester): bool;

/**
* Determines if the instance is greater (after) than another
Expand Down
9 changes: 8 additions & 1 deletion src/Carbon/Traits/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace Carbon\Traits;

use BackedEnum;
use BadMethodCallException;
use Carbon\CarbonInterface;
use Carbon\Exceptions\BadComparisonUnitException;
use Carbon\FactoryImmutable;
use Carbon\Month;
use Carbon\WeekDay;
use DateTimeInterface;
use InvalidArgumentException;

Expand Down Expand Up @@ -873,8 +876,12 @@ public static function canBeCreatedFromFormat(?string $date, string $format): bo
*
* @param string $tester day name, month name, hour, date, etc. as string
*/
public function is(string $tester): bool
public function is(WeekDay|Month|string $tester): bool
{
if ($tester instanceof BackedEnum) {
$tester = $tester->name;
}

$tester = trim($tester);

if (preg_match('/^\d+$/', $tester)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/Carbon/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use BadMethodCallException;
use Carbon\Carbon;
use Carbon\Month;
use Carbon\WeekDay;
use DateTime;
use PHPUnit\Framework\Attributes\DataProvider;
use stdClass;
Expand Down Expand Up @@ -1034,11 +1036,15 @@ public function testIs()
$this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2020-06-02'));
$this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('Sunday'));
$this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('Monday'));
$this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is(WeekDay::Sunday));
$this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is(WeekDay::Monday));
$this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('June'));
$this->assertTrue(Carbon::parse('2019-05-31 12:23:45')->is('May'));
$this->assertTrue(Carbon::parse('2019-05-31 12:23:45')->is('mAy'));
$this->assertFalse(Carbon::parse('2019-05-31 12:23:45')->is('April'));
$this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('May'));
$this->assertTrue(Carbon::parse('2019-05-31 12:23:45')->is(Month::May));
$this->assertFalse(Carbon::parse('2019-05-31 12:23:45')->is(Month::April));
$this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('12:23'));
$this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('12:26'));
$this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('12:23:00'));
Expand Down

0 comments on commit b3d0d94

Please sign in to comment.