Skip to content

Commit

Permalink
Implement handling of numeric unix timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 10, 2024
1 parent 725c6cd commit 5faf69e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);

Expand All @@ -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.');
}
Expand Down
8 changes: 4 additions & 4 deletions src/Interfaces/CalculatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
39 changes: 38 additions & 1 deletion tests/CalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Carbon\Carbon;
use DateTime;
use DateTimeInterface;
use Generator;
use Intervention\Zodiac\Calculator;
use Intervention\Zodiac\Exceptions\NotReadableException;
Expand All @@ -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));
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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')]
Expand Down

0 comments on commit 5faf69e

Please sign in to comment.