Skip to content

Commit

Permalink
CarbonFiscalYear better README
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz committed Apr 1, 2023
1 parent b756682 commit 60ef098
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 3 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/fix-php-code-style-issues.yml
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
37 changes: 37 additions & 0 deletions .github/workflows/run-tests.yml
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,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("2023-04-02");
$date->startOfYear()->format("Y-m-d"); // 2023-04-01
$date->endOfYear()->format("Y-m-d"); // 2024-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
```

## Testing
Expand Down
70 changes: 70 additions & 0 deletions src/CarbonFiscalYear.php
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();
}
}
10 changes: 10 additions & 0 deletions src/Exceptions/InvalidFiscalYearException.php
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
{
//
}
23 changes: 23 additions & 0 deletions tests/CarbonFiscalYearTest.php
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");
});

0 comments on commit 60ef098

Please sign in to comment.