Skip to content

Commit

Permalink
Consolidate Calculator::class public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 10, 2024
1 parent 76acb04 commit 71afcaf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ use DateTime;
use Carbon\Carbon;

// get zodiac object from a date
$zodiac = Calculator::make('1980-09-15');
$zodiac = Calculator::zodiac('1980-09-15');

// method takes mixed formats
$zodiac = Calculator::make('first day of June 2008');
$zodiac = Calculator::zodiac('first day of June 2008');

// create from DateTime object
$zodiac = Calculator::make(new DateTime('1977-03-15'));
$zodiac = Calculator::zodiac(new DateTime('1977-03-15'));

// get zodiac from a Carbon object
$zodiac = Calculator::make(Carbon::yesterday());
$zodiac = Calculator::zodiac(Carbon::yesterday());

// get zodiac from unix timestamp
$zodiac = Calculator::make(228268800);
$zodiac = Calculator::zodiac(228268800);
```

```php
Expand All @@ -53,7 +53,7 @@ use DateTime;
use Carbon\Carbon;

// calculate zodiac sing
$zodiac = Calculator::make('1977-06-17');
$zodiac = Calculator::zodiac('1977-06-17');

$name = $zodiac->name(); // 'gemini'
$html = $zodiac->html(); // '♊︎'
Expand Down
33 changes: 16 additions & 17 deletions src/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Intervention\Zodiac\Interfaces\ZodiacInterface;
use Intervention\Zodiac\Interfaces\CalculatorInterface;
use InvalidArgumentException;
use ReflectionException;

class Calculator implements CalculatorInterface
{
Expand All @@ -32,33 +31,22 @@ class Calculator implements CalculatorInterface
Zodiacs\Virgo::class,
];

/**
* {@inheritdoc}
*
* @see ZodiacInterface::make()
* @throws NotReadableException
* @throws ReflectionException
*/
public static function make(int|string|DateTimeInterface $date): ZodiacInterface
{
return (new self())->zodiac($date);
}

/**
* {@inheritdoc}
*
* @throws NotReadableException
* @see ZodiacInterface::zodiac()
*/
public function zodiac(int|string|DateTimeInterface $date): ZodiacInterface
public static function zodiac(int|string|DateTimeInterface $date): ZodiacInterface
{
$date = $this->normalizeDate($date);
$calculator = new self();
$date = $calculator->normalizeDate($date);

foreach ($this::ZODIAC_CLASSNAMES as $classname) {
foreach ($calculator::ZODIAC_CLASSNAMES as $classname) {
try {
$zodiac = new $classname();
if ($date->isZodiac($zodiac)) {
return $zodiac->setTranslator($this->translator());
return $zodiac->setTranslator($calculator->translator());
}
} catch (InvalidFormatException | InvalidArgumentException) {
// try next zodiac
Expand All @@ -70,6 +58,17 @@ public function zodiac(int|string|DateTimeInterface $date): ZodiacInterface
);
}

/**
* Alias of zodiac()
*
* @see self::zodiac()
* @throws NotReadableException
*/
public static function make(int|string|DateTimeInterface $date): ZodiacInterface
{
return (new self())->zodiac($date);
}

/**
* Normalze given date to Carbon object
*
Expand Down
10 changes: 1 addition & 9 deletions src/Interfaces/CalculatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,5 @@ interface CalculatorInterface
* @param int|string|DateTimeInterface $date
* @return ZodiacInterface
*/
public static function make(int|string|DateTimeInterface $date): ZodiacInterface;

/**
* Get zodiac for given date
*
* @param int|string|DateTimeInterface $date
* @return ZodiacInterface
*/
public function zodiac(int|string|DateTimeInterface $date): ZodiacInterface;
public static function zodiac(int|string|DateTimeInterface $date): ZodiacInterface;
}
2 changes: 1 addition & 1 deletion src/Laravel/ZodiacBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(protected Application $app)
}

/**
* Make zodiac from input date
* Create zodiac from input date
*
* @param mixed $date
* @throws NotReadableException
Expand Down
5 changes: 4 additions & 1 deletion tests/CalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ final class CalculatorTest extends TestCase
#[DataProvider('validZodiacDataProvider')]
public function testValidCalculatorInputs(int|string|DateTimeInterface $input, string $resultClassname): void
{
$this->assertInstanceOf($resultClassname, (new Calculator())->zodiac($input));
$calculator = new Calculator();
$this->assertInstanceOf($resultClassname, $calculator->zodiac($input));
$this->assertInstanceOf($resultClassname, $calculator->make($input));
$this->assertInstanceOf($resultClassname, Calculator::zodiac($input));
$this->assertInstanceOf($resultClassname, Calculator::make($input));
}

Expand Down

0 comments on commit 71afcaf

Please sign in to comment.