diff --git a/src/Calculator.php b/src/Calculator.php index 1e7fb6f..c5d8e09 100644 --- a/src/Calculator.php +++ b/src/Calculator.php @@ -39,7 +39,7 @@ class Calculator implements CalculatorInterface * @throws NotReadableException * @throws ReflectionException */ - public static function make(string|DateTimeInterface $date): ZodiacInterface + public static function make(int|string|DateTimeInterface $date): ZodiacInterface { return (new self())->zodiac($date); } @@ -50,7 +50,7 @@ public static function make(string|DateTimeInterface $date): ZodiacInterface * @throws NotReadableException * @see ZodiacInterface::zodiac() */ - public function zodiac(string|DateTimeInterface $date): ZodiacInterface + public function zodiac(int|string|DateTimeInterface $date): ZodiacInterface { $date = $this->normalizeDate($date); @@ -77,10 +77,13 @@ public function zodiac(string|DateTimeInterface $date): ZodiacInterface * @throws NotReadableException * @return ZodiacComparableDate */ - private function normalizeDate(string|DateTimeInterface $date): ZodiacComparableDate + private function normalizeDate(int|string|DateTimeInterface $date): ZodiacComparableDate { try { - $date = Carbon::parse($date); + $date = match (true) { + is_numeric($date) => Carbon::createFromTimestamp($date), + default => Carbon::parse($date), + }; } catch (InvalidFormatException | InvalidArgumentException) { throw new NotReadableException('Unable to create zodiac from value.'); } diff --git a/src/Interfaces/CalculatorInterface.php b/src/Interfaces/CalculatorInterface.php index 8e01ec9..e6f9368 100644 --- a/src/Interfaces/CalculatorInterface.php +++ b/src/Interfaces/CalculatorInterface.php @@ -11,16 +11,16 @@ interface CalculatorInterface /** * Get zodiac for given date * - * @param string|DateTimeInterface $date + * @param int|string|DateTimeInterface $date * @return ZodiacInterface */ - public static function make(string|DateTimeInterface $date): ZodiacInterface; + public static function make(int|string|DateTimeInterface $date): ZodiacInterface; /** * Get zodiac for given date * - * @param string|DateTimeInterface $date + * @param int|string|DateTimeInterface $date * @return ZodiacInterface */ - public function zodiac(string|DateTimeInterface $date): ZodiacInterface; + public function zodiac(int|string|DateTimeInterface $date): ZodiacInterface; } diff --git a/tests/CalculatorTest.php b/tests/CalculatorTest.php index c8dbbdd..055412d 100644 --- a/tests/CalculatorTest.php +++ b/tests/CalculatorTest.php @@ -6,6 +6,7 @@ use Carbon\Carbon; use DateTime; +use DateTimeInterface; use Generator; use Intervention\Zodiac\Calculator; use Intervention\Zodiac\Exceptions\NotReadableException; @@ -27,7 +28,7 @@ final class CalculatorTest extends TestCase { #[DataProvider('validZodiacDataProvider')] - public function testValidCalculatorInputs(string|DateTime $input, string $resultClassname): void + public function testValidCalculatorInputs(int|string|DateTimeInterface $input, string $resultClassname): void { $this->assertInstanceOf($resultClassname, (new Calculator())->zodiac($input)); $this->assertInstanceOf($resultClassname, Calculator::make($input)); @@ -49,6 +50,7 @@ public static function validZodiacDataProvider(): Generator yield ['1977-01-15', Capricorn::class]; yield ['1977-01-26', Aquarius::class]; yield ['1977-02-27', Pisces::class]; + yield [new DateTime('1977-03-27'), Aries::class]; yield [new DateTime('1977-04-27'), Taurus::class]; yield [new DateTime('1977-05-27'), Gemini::class]; @@ -65,6 +67,7 @@ public static function validZodiacDataProvider(): Generator yield [new DateTime('1977-01-15'), Capricorn::class]; yield [new DateTime('1977-01-26'), Aquarius::class]; yield [new DateTime('1977-02-27'), Pisces::class]; + yield [Carbon::parse('1977-03-27'), Aries::class]; yield [Carbon::parse('1977-04-27'), Taurus::class]; yield [Carbon::parse('1977-05-27'), Gemini::class]; @@ -81,6 +84,40 @@ public static function validZodiacDataProvider(): Generator yield [Carbon::parse('1977-01-15'), Capricorn::class]; yield [Carbon::parse('1977-01-26'), Aquarius::class]; yield [Carbon::parse('1977-02-27'), Pisces::class]; + + yield ['228268800', Aries::class]; + yield ['230947200', Taurus::class]; + yield ['233539200', Gemini::class]; + yield ['236217600', Cancer::class]; + yield ['238809600', Leo::class]; + yield ['235699200', Gemini::class]; + yield ['241488000', Virgo::class]; + yield ['244166400', Libra::class]; + yield ['246758400', Scorpio::class]; + yield ['249436800', Sagittarius::class]; + yield ['252028800', Capricorn::class]; + yield ['252374400', Capricorn::class]; + yield ['220924800', Capricorn::class]; + yield ['222134400', Capricorn::class]; + yield ['223084800', Aquarius::class]; + yield ['225849600', Pisces::class]; + + yield [228268800, Aries::class]; + yield [230947200, Taurus::class]; + yield [233539200, Gemini::class]; + yield [236217600, Cancer::class]; + yield [238809600, Leo::class]; + yield [235699200, Gemini::class]; + yield [241488000, Virgo::class]; + yield [244166400, Libra::class]; + yield [246758400, Scorpio::class]; + yield [249436800, Sagittarius::class]; + yield [252028800, Capricorn::class]; + yield [252374400, Capricorn::class]; + yield [220924800, Capricorn::class]; + yield [222134400, Capricorn::class]; + yield [223084800, Aquarius::class]; + yield [225849600, Pisces::class]; } #[DataProvider('invalidZodiacDataProvider')]