From 60ef0989304017dd799805fa9f096a7ab69006b1 Mon Sep 17 00:00:00 2001 From: Sairahcaz Date: Sat, 1 Apr 2023 14:26:16 +0200 Subject: [PATCH] CarbonFiscalYear better README --- .../workflows/fix-php-code-style-issues.yml | 23 ++++++ .github/workflows/run-tests.yml | 37 ++++++++++ README.md | 6 +- src/CarbonFiscalYear.php | 70 +++++++++++++++++++ src/Exceptions/InvalidFiscalYearException.php | 10 +++ tests/CarbonFiscalYearTest.php | 23 ++++++ 6 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/fix-php-code-style-issues.yml create mode 100644 .github/workflows/run-tests.yml create mode 100644 src/CarbonFiscalYear.php create mode 100644 src/Exceptions/InvalidFiscalYearException.php create mode 100644 tests/CarbonFiscalYearTest.php diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml new file mode 100644 index 0000000..4917b37 --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -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 diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..0ce0d08 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -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 diff --git a/README.md b/README.md index d4ba272..0867b2e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/CarbonFiscalYear.php b/src/CarbonFiscalYear.php new file mode 100644 index 0000000..edf9c3e --- /dev/null +++ b/src/CarbonFiscalYear.php @@ -0,0 +1,70 @@ +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(); + } +} diff --git a/src/Exceptions/InvalidFiscalYearException.php b/src/Exceptions/InvalidFiscalYearException.php new file mode 100644 index 0000000..c062c55 --- /dev/null +++ b/src/Exceptions/InvalidFiscalYearException.php @@ -0,0 +1,10 @@ +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"); +});