Skip to content

Commit

Permalink
added immutables
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz committed Apr 6, 2023
1 parent 60ef098 commit a469196
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 63 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ composer require laracraft-tech/carbon-extensions
The `CarbonFiscalYear` class helps you to work with **fiscal years**!
It easily lets you know the fiscal years **start** and **end** of a given date.

**Note:** There is also a `CarbonFiscalYearImmutable`, class which has the same API,
but it works _immutable_. For more information on _immutables_, check out the [carbon docs](https://carbon.nesbot.com/docs/).

```php
// set your fiscal year start month and day
CarbonFiscalYear::setFiscalYearStart(4, 1);
Expand All @@ -33,9 +36,9 @@ $date = CarbonFiscalYear::parse("2022-03-30");
$date->startOfYear()->format("Y-m-d"); // 2021-04-01
$date->endOfYear()->format("Y-m-d"); // 2022-03-31

$date = CarbonFiscalYear::parse("2022-04-02");
$date->startOfYear()->format("Y-m-d"); // 2022-04-01
$date->endOfYear()->format("Y-m-d"); // 2023-03-31
$date2 = CarbonFiscalYear::parse("2022-04-02");
$date2->startOfYear()->format("Y-m-d"); // 2022-04-01
$date2->endOfYear()->format("Y-m-d"); // 2023-03-31
```

## Testing
Expand Down
51 changes: 1 addition & 50 deletions src/CarbonFiscalYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@
namespace LaracraftTech\CarbonExtensions;

use Carbon\Carbon;
use LaracraftTech\CarbonExtensions\Exceptions\InvalidFiscalYearException;

class CarbonFiscalYear extends Carbon
{
/**
* The month of the year when the fiscal year starts.
* @var int
*/
protected static int $fiscalYearStartMonth;

/**
* The day in the month of the year when the fiscal year starts.
* @var int
*/
protected static int $fiscalYearStartDay;
use CarbonFiscalYearTrait;

/**
* @param $time
Expand All @@ -29,42 +18,4 @@ public function __construct($time = null, $tz = null)

$this->ensureFiscalYearStartIsSet();
}

/**
* Set the start of the fiscal year before you start using the class.
*
* @param int $month
* @param int $day
* @return void
*/
public static function setFiscalYearStart(int $month, int $day): void
{
self::$fiscalYearStartMonth = $month;
self::$fiscalYearStartDay = $day;
}

private function ensureFiscalYearStartIsSet()
{
if (empty(self::$fiscalYearStartMonth) || empty(self::$fiscalYearStartDay)) {
throw new InvalidFiscalYearException();
}
}

public function startOfYear(): CarbonFiscalYear
{
$year = $this->year;

if ($this->month < self::$fiscalYearStartMonth ||
($this->month == self::$fiscalYearStartMonth && $this->day < self::$fiscalYearStartDay)
) {
$year -= 1;
}

return $this->setDate($year, self::$fiscalYearStartMonth, self::$fiscalYearStartDay)->startOfDay();
}

public function endOfYear(): CarbonFiscalYear
{
return $this->startOfYear()->copy()->addYear()->subDay()->endOfDay();
}
}
10 changes: 10 additions & 0 deletions src/CarbonFiscalYearException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace LaracraftTech\CarbonExtensions;

use InvalidArgumentException;

class CarbonFiscalYearException extends InvalidArgumentException
{
//
}
21 changes: 21 additions & 0 deletions src/CarbonFiscalYearImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace LaracraftTech\CarbonExtensions;

use Carbon\CarbonImmutable;

class CarbonFiscalYearImmutable extends CarbonImmutable
{
use CarbonFiscalYearTrait;

/**
* @param $time
* @param $tz
*/
public function __construct($time = null, $tz = null)
{
parent::__construct($time, $tz);

$this->ensureFiscalYearStartIsSet();
}
}
56 changes: 56 additions & 0 deletions src/CarbonFiscalYearTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace LaracraftTech\CarbonExtensions;

trait CarbonFiscalYearTrait
{
/**
* The month of the year when the fiscal year starts.
* @var int
*/
protected static int $fiscalYearStartMonth;

/**
* The day in the month of the year when the fiscal year starts.
* @var int
*/
protected static int $fiscalYearStartDay;

/**
* Set the start of the fiscal year before you start using the class.
*
* @param int $month
* @param int $day
* @return void
*/
public static function setFiscalYearStart(int $month, int $day): void
{
self::$fiscalYearStartMonth = $month;
self::$fiscalYearStartDay = $day;
}

private function ensureFiscalYearStartIsSet()
{
if (empty(self::$fiscalYearStartMonth) || empty(self::$fiscalYearStartDay)) {
throw new CarbonFiscalYearException();
}
}

public function startOfYear(): CarbonFiscalYear
{
$year = $this->year;

if ($this->month < self::$fiscalYearStartMonth ||
($this->month == self::$fiscalYearStartMonth && $this->day < self::$fiscalYearStartDay)
) {
$year -= 1;
}

return $this->setDate($year, self::$fiscalYearStartMonth, self::$fiscalYearStartDay)->startOfDay();
}

public function endOfYear(): CarbonFiscalYear
{
return $this->startOfYear()->copy()->addYear()->subDay()->endOfDay();
}
}
10 changes: 0 additions & 10 deletions src/Exceptions/InvalidFiscalYearException.php

This file was deleted.

15 changes: 15 additions & 0 deletions tests/CarbonFiscalYearTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use LaracraftTech\CarbonExtensions\CarbonFiscalYear;
use LaracraftTech\CarbonExtensions\CarbonFiscalYearImmutable;

it('can detect fiscal year start and end', function () {
//set fiscal year
Expand All @@ -21,3 +22,17 @@
expect($date->startOfYear()->format("Y-m-d"))->toBe("2022-10-01")
->and($date->endOfYear()->format("Y-m-d"))->toBe("2023-09-30");
});

it('works with im/mutable', function () {
CarbonFiscalYear::setFiscalYearStart(4, 1);
$mutableDate = CarbonFiscalYear::parse("2022-03-30");
$mutableDate2 = $mutableDate->addYear();

expect($mutableDate)->toBe($mutableDate2);

CarbonFiscalYearImmutable::setFiscalYearStart(4, 1);
$immutableDate = CarbonFiscalYearImmutable::parse("2022-03-30");
$immutableDate2 = $immutableDate->addYear();

expect($immutableDate)->not->toBe($immutableDate2);
});

0 comments on commit a469196

Please sign in to comment.