-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Check & fix styling | ||
|
||
on: [push] | ||
|
||
jobs: | ||
php-cs-fixer: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.head_ref }} | ||
|
||
- name: Run PHP CS Fixer | ||
uses: docker://oskarstark/php-cs-fixer-ga | ||
with: | ||
args: --config=.php-cs-fixer.dist.php --allow-risky=yes | ||
|
||
- name: Commit changes | ||
uses: stefanzweifel/git-auto-commit-action@v4 | ||
with: | ||
commit_message: Fix styling |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: true | ||
matrix: | ||
os: [ubuntu-latest, windows-latest] | ||
php: [8.2, 8.1, 8.0, 7.4] | ||
stability: [prefer-lowest, prefer-stable] | ||
|
||
name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo | ||
coverage: none | ||
|
||
- name: Setup problem matchers | ||
run: | | ||
echo "::add-matcher::${{ runner.tool_cache }}/php.json" | ||
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" | ||
- name: Install dependencies | ||
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction | ||
|
||
- name: Execute tests | ||
run: vendor/bin/pest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
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; | ||
|
||
/** | ||
* @param $time | ||
* @param $tz | ||
*/ | ||
public function __construct($time = null, $tz = null) | ||
{ | ||
parent::__construct($time, $tz); | ||
|
||
$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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace LaracraftTech\CarbonExtensions\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
|
||
class InvalidFiscalYearException extends InvalidArgumentException | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use LaracraftTech\CarbonExtensions\CarbonFiscalYear; | ||
|
||
it('can detect fiscal year start and end', function () { | ||
//set fiscal year | ||
CarbonFiscalYear::setFiscalYearStart(4, 1); | ||
|
||
$date = CarbonFiscalYear::parse("2022-03-30"); | ||
expect($date->startOfYear()->format("Y-m-d"))->toBe("2021-04-01") | ||
->and($date->endOfYear()->format("Y-m-d"))->toBe("2022-03-31"); | ||
|
||
$date = CarbonFiscalYear::parse("2022-04-02"); | ||
expect($date->startOfYear()->format("Y-m-d"))->toBe("2022-04-01") | ||
->and($date->endOfYear()->format("Y-m-d"))->toBe("2023-03-31"); | ||
|
||
//another fiscal year | ||
CarbonFiscalYear::setFiscalYearStart(10, 1); | ||
|
||
$date = CarbonFiscalYear::parse("2023-05-11"); | ||
expect($date->startOfYear()->format("Y-m-d"))->toBe("2022-10-01") | ||
->and($date->endOfYear()->format("Y-m-d"))->toBe("2023-09-30"); | ||
}); |