From 734e211894693e52bbaed139b135af7ce0725597 Mon Sep 17 00:00:00 2001 From: Moamen Eltouny Date: Tue, 1 Dec 2026 11:44:49 +0200 Subject: [PATCH 1/6] fix: never print negative zero when formatting money with a locale --- src/Money.php | 16 +++++++++++----- tests/Unit/MoneyTest.php | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Money.php b/src/Money.php index af90bcf..7d9540d 100644 --- a/src/Money.php +++ b/src/Money.php @@ -47,17 +47,23 @@ public static function format( } if ($locale !== null) { - // Like the built-in path, an amount that rounds to zero must not print as "-$0.00". - if ($amount < 0 && round($amount, $decimals ?? self::fractionDigits($currency)) == 0) { - $amount = 0; - } - $formatter = Intl::formatter($locale, 'CURRENCY'); if ($decimals !== null) { Intl::fractionDigits($formatter, $decimals); } + // Like the built-in path, an amount that rounds to zero must not print as "-$0.00". Intl rounds + // with its own currency digits, which can differ from ISO 4217 (IQD has 0, not 3), so compare + // against Intl's own zero instead of rounding here. + if ($amount < 0) { + $zero = Intl::result($formatter->formatCurrency(0, $currency), $formatter); + + if (Intl::result($formatter->formatCurrency(abs($amount), $currency), $formatter) === $zero) { + return $zero; + } + } + return Intl::result($formatter->formatCurrency($amount, $currency), $formatter); } diff --git a/tests/Unit/MoneyTest.php b/tests/Unit/MoneyTest.php index 4f88be0..7a4aba8 100644 --- a/tests/Unit/MoneyTest.php +++ b/tests/Unit/MoneyTest.php @@ -61,6 +61,23 @@ ->and(Money::format(-0.005, 'USD', null, 'en_US'))->toBe('-$0.01'); })->skip(withoutIntl(), 'ext-intl is not installed.'); +it('never prints negative zero when Intl uses other currency digits than ISO 4217', function (string $currency, string $locale) { + // Intl decides the digits with a locale (IQD has 0 in ICU, 3 in ISO 4217), so take the amount from Intl itself. + $digits = (new NumberFormatter($locale . '@currency=' . $currency, NumberFormatter::CURRENCY)) + ->getAttribute(NumberFormatter::MAX_FRACTION_DIGITS); + $amount = -0.4 / 10 ** $digits; + + expect(Money::format($amount, $currency, null, $locale))->toBe(Money::format(0, $currency, null, $locale)) + ->and(Money::format(-$amount * 2, $currency, null, $locale))->not->toBe(Money::format(0, $currency, null, $locale)); +})->with([ + 'IQD in en_US' => ['IQD', 'en_US'], + 'IQD in de_DE' => ['IQD', 'de_DE'], + 'IQD in ar_EG' => ['IQD', 'ar_EG'], + 'IRR in en_US' => ['IRR', 'en_US'], + 'YER in ar_EG' => ['YER', 'ar_EG'], + 'USD in en_US' => ['USD', 'en_US'], +])->skip(withoutIntl(), 'ext-intl is not installed.'); + it('needs ext-intl to format money with a locale', function () { Money::format(1, 'USD', null, 'en_US'); })->skip(! withoutIntl(), 'ext-intl is installed.')->throws(MissingIntlExtension::class); From 28d74732a766829abd5c542a07aa0ffe31464659 Mon Sep 17 00:00:00 2001 From: Moamen Eltouny Date: Tue, 1 Dec 2026 11:44:49 +0200 Subject: [PATCH 2/6] refactor: move the internal helpers out of the Support namespace --- MAINTAINING.md | 2 +- src/{Support => }/Assert.php | 2 +- src/Bytes.php | 2 -- src/Duration.php | 1 - src/Exceptions/MissingIntlExtension.php | 2 +- src/{Support => }/Excerpt.php | 2 +- src/{Support => }/Intl.php | 2 +- src/Money.php | 3 --- src/Number.php | 2 -- 9 files changed, 5 insertions(+), 13 deletions(-) rename src/{Support => }/Assert.php (95%) rename src/{Support => }/Excerpt.php (93%) rename src/{Support => }/Intl.php (98%) diff --git a/MAINTAINING.md b/MAINTAINING.md index 92a8069..d6da160 100644 --- a/MAINTAINING.md +++ b/MAINTAINING.md @@ -18,7 +18,7 @@ One branch per PHP minor, each requiring exactly that minor (`"php": ">=8.N <8.N ## Public API invariants -- The public API is the static methods of `Arr`, `Str`, `Number`, `Bytes`, `Money` and `Duration`, and `Exceptions\MissingIntlExtension`. Everything under `Support\` is `@internal`. +- The public API is the static methods of `Arr`, `Str`, `Number`, `Bytes`, `Money` and `Duration`, and `Exceptions\MissingIntlExtension`. `Assert`, `Excerpt` and `Intl` are `@internal` helpers, not part of the public API. - Without a `$locale`, output comes from the built-in English formatter and is identical on every machine, with or without `ext-intl`. - With a `$locale`, `ext-intl` is used; without it, `MissingIntlExtension` is thrown. Invalid locales never leak `ValueError` or `IntlException`. - Rounding is half away from zero everywhere, a rounded value carries to the next unit (`999 999 B` is `1 MB`), and no formatter prints negative zero. diff --git a/src/Support/Assert.php b/src/Assert.php similarity index 95% rename from src/Support/Assert.php rename to src/Assert.php index bf6c98f..497d834 100644 --- a/src/Support/Assert.php +++ b/src/Assert.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Pharaonic\Readable\Support; +namespace Pharaonic\Readable; use InvalidArgumentException; diff --git a/src/Bytes.php b/src/Bytes.php index 7b7b21e..b61d6d1 100644 --- a/src/Bytes.php +++ b/src/Bytes.php @@ -5,8 +5,6 @@ namespace Pharaonic\Readable; use InvalidArgumentException; -use Pharaonic\Readable\Support\Assert; -use Pharaonic\Readable\Support\Excerpt; /** * Readable byte sizes. diff --git a/src/Duration.php b/src/Duration.php index 6e010f1..f7c2a03 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -8,7 +8,6 @@ use DateTimeInterface; use Exception; use InvalidArgumentException; -use Pharaonic\Readable\Support\Excerpt; /** * Readable durations in English: "1 hour 1 minute", or "1h 1m" in short form. diff --git a/src/Exceptions/MissingIntlExtension.php b/src/Exceptions/MissingIntlExtension.php index 3bc5177..14eb1c2 100644 --- a/src/Exceptions/MissingIntlExtension.php +++ b/src/Exceptions/MissingIntlExtension.php @@ -4,7 +4,7 @@ namespace Pharaonic\Readable\Exceptions; -use Pharaonic\Readable\Support\Excerpt; +use Pharaonic\Readable\Excerpt; use RuntimeException; /** diff --git a/src/Support/Excerpt.php b/src/Excerpt.php similarity index 93% rename from src/Support/Excerpt.php rename to src/Excerpt.php index 7f884da..d3a72b1 100644 --- a/src/Support/Excerpt.php +++ b/src/Excerpt.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Pharaonic\Readable\Support; +namespace Pharaonic\Readable; /** * @internal diff --git a/src/Support/Intl.php b/src/Intl.php similarity index 98% rename from src/Support/Intl.php rename to src/Intl.php index 080cb82..5339056 100644 --- a/src/Support/Intl.php +++ b/src/Intl.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Pharaonic\Readable\Support; +namespace Pharaonic\Readable; use IntlException; use InvalidArgumentException; diff --git a/src/Money.php b/src/Money.php index 7d9540d..b1fb97b 100644 --- a/src/Money.php +++ b/src/Money.php @@ -5,9 +5,6 @@ namespace Pharaonic\Readable; use InvalidArgumentException; -use Pharaonic\Readable\Support\Assert; -use Pharaonic\Readable\Support\Excerpt; -use Pharaonic\Readable\Support\Intl; /** * Readable money amounts. Formatting only: no arithmetic and no exchange rates. diff --git a/src/Number.php b/src/Number.php index 3433528..2862f0e 100644 --- a/src/Number.php +++ b/src/Number.php @@ -5,8 +5,6 @@ namespace Pharaonic\Readable; use InvalidArgumentException; -use Pharaonic\Readable\Support\Assert; -use Pharaonic\Readable\Support\Intl; /** * Readable numbers. From 2d496bd01b419bdea3aae74152429d9f7d848408 Mon Sep 17 00:00:00 2001 From: Moamen Eltouny Date: Tue, 1 Dec 2026 11:44:49 +0200 Subject: [PATCH 3/6] ci: run checks in gated stages behind a final quality gate --- .github/workflows/build.yml | 290 +++++++++++++++++++++++------------ .github/workflows/labels.yml | 2 +- 2 files changed, 194 insertions(+), 98 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 41b5373..0687861 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,118 +1,214 @@ name: Build - -on: +'on': push: branches: - - '8.*.x' - - develop + - develop + - 8.*.x paths-ignore: - - 'README.md' - - 'docs/**' - + - README.md + - docs/** pull_request: branches: - - '8.*.x' - - develop - - workflow_dispatch: - -permissions: - contents: read - + - develop + - 8.*.x + workflow_dispatch: null concurrency: - group: ${{ github.workflow }}-${{ github.ref }} + group: build-${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true - +permissions: + contents: read jobs: - php: - name: Resolve PHP version + matrix: runs-on: ubuntu-latest outputs: - version: ${{ steps.php.outputs.version }} - + php: ${{ steps.php.outputs.versions }} + lowest: ${{ steps.php.outputs.lowest }} steps: - - name: Checkout - uses: actions/checkout@v7 - - # Every branch declares exactly one PHP minor (">=8.N <8.N+1"), so the - # version comes from composer.json on any branch. - - name: Detect target PHP version - id: php - shell: bash - run: | - constraint="$(jq -r '.require.php' composer.json)" - - if [[ ! "$constraint" =~ ^\>=(8\.[0-9]+) ]]; then - echo "Cannot read the target PHP minor from require.php: $constraint" >&2 - exit 1 - fi - - echo "version=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT" - - check: - name: Check - needs: php + - name: Checkout + uses: actions/checkout@v7 + - name: Read supported PHP minors + id: php + shell: bash + run: | + constraint="$(jq -r '.require.php' composer.json)" + if [[ ! "$constraint" =~ ^\>=8\.([0-9]+)\ \<8\.([0-9]+)$ ]] || (( BASH_REMATCH[2] != BASH_REMATCH[1] + 1 )); then + echo "require.php must declare exactly one PHP minor as >=8.N <8.N+1, got: $constraint" >&2 + exit 1 + fi + version="8.${BASH_REMATCH[1]}" + echo "versions=$(jq -cn --arg php "$version" '[$php]')" >> "$GITHUB_OUTPUT" + echo "lowest=$version" >> "$GITHUB_OUTPUT" + validate: + needs: + - matrix runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v7 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ needs.php.outputs.version }} - extensions: mbstring, intl - coverage: none - tools: composer:v2 - - - name: Install dependencies - uses: ramsey/composer-install@v4 - - - name: Check - run: composer check - + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Validate + run: composer validate --strict + style: + needs: + - matrix + - validate + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Style + run: composer format:check + analyse: + needs: + - matrix + - style + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Analyse + run: composer analyse tests: - name: Tests - ${{ matrix.os }} - ${{ matrix.dependencies }}${{ matrix.intl && ' - intl' || '' }} - needs: php - runs-on: ${{ matrix.os }} - + name: Tests - PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependencies }}${{ !matrix.intl && ' - without intl' || '' }} + needs: + - matrix + - analyse strategy: - max-parallel: 2 - - fail-fast: false - + fail-fast: true matrix: - os: - - ubuntu-latest - - windows-latest + php: ${{ fromJSON(needs.matrix.outputs.php) }} dependencies: - - lowest - - highest + - lowest + - highest + os: + - ubuntu-latest + - windows-latest intl: - - true + - true include: - # ext-intl is optional; the built-in formatters must work without it. - - os: ubuntu-latest - dependencies: highest - intl: false - + # ext-intl is optional: the built-in formatters must work without it. + - php: ${{ needs.matrix.outputs.lowest }} + dependencies: highest + os: ubuntu-latest + intl: false + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ matrix.php }} + extensions: mbstring, ${{ matrix.intl && 'intl' || ':intl' }} + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: ${{ matrix.dependencies }} + - name: Run tests + run: composer test + security: + needs: + - matrix + - tests + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Security audit + run: composer security + archive: + needs: + - matrix + - security + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Verify Composer archive + shell: bash + run: | + mkdir -p /tmp/pharaonic-ci-archive + composer archive --format=zip --dir=/tmp/pharaonic-ci-archive --file=package + unzip -t /tmp/pharaonic-ci-archive/package.zip + quality: + name: Quality gate + if: ${{ always() }} + needs: + - matrix + - validate + - style + - analyse + - tests + - security + - archive + runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v7 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ needs.php.outputs.version }} - extensions: mbstring, ${{ matrix.intl && 'intl' || ':intl' }} - coverage: none - tools: composer:v2 - - - name: Install dependencies - uses: ramsey/composer-install@v4 - with: - dependency-versions: ${{ matrix.dependencies }} - - - name: Run tests - run: composer test + - name: Require every stage to succeed + env: + NEEDS_JSON: ${{ toJSON(needs) }} + run: | + python3 - <<'PY_CHECK' + import json, os, sys + results = json.loads(os.environ['NEEDS_JSON']) + failed = {name: job['result'] for name, job in results.items() if job['result'] != 'success'} + if failed: + print('Required stages did not succeed:', failed) + sys.exit(1) + print('Every required stage succeeded.') + PY_CHECK diff --git a/.github/workflows/labels.yml b/.github/workflows/labels.yml index 8bdc79a..d96990d 100644 --- a/.github/workflows/labels.yml +++ b/.github/workflows/labels.yml @@ -22,7 +22,7 @@ jobs: # Creates and updates the labels in .github/labels.yml. Labels that are # not listed are left alone. - name: Sync labels - uses: crazy-max/ghaction-github-labeler@v5 + uses: crazy-max/ghaction-github-labeler@548a7c3603594ec17c819e1239f281a3b801ab4d # v6.0.0 with: yaml-file: .github/labels.yml skip-delete: true From 0ce487b120ffa6e09e153b87f342b21fef763cf7 Mon Sep 17 00:00:00 2001 From: Moamen Eltouny Date: Tue, 1 Dec 2026 11:44:49 +0200 Subject: [PATCH 4/6] chore: align the repository baseline with the template --- .github/ISSUE_TEMPLATE/bug_report.yml | 1 + .github/dependabot.yml | 4 ++++ CODE_OF_CONDUCT.md | 4 +++- CONTRIBUTING.md | 4 ++-- README.md | 5 ++--- composer.json | 3 ++- phpunit.xml.dist | 7 ++----- tests/Feature/.gitkeep | 0 8 files changed, 16 insertions(+), 12 deletions(-) delete mode 100644 tests/Feature/.gitkeep diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 18a4ff5..f4aa199 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -1,5 +1,6 @@ name: Bug report description: Report something that is not working as expected. +title: "[Bug]: " labels: - "type: bug" - "status: needs triage" diff --git a/.github/dependabot.yml b/.github/dependabot.yml index ddafb34..473f3fb 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,6 +5,8 @@ updates: - package-ecosystem: composer directory: "/" target-branch: develop + labels: + - "area: dependencies" schedule: interval: weekly groups: @@ -14,6 +16,8 @@ updates: - package-ecosystem: github-actions directory: "/" target-branch: develop + labels: + - "area: dependencies" schedule: interval: weekly groups: diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index cb3afbf..8dbb760 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -2,7 +2,7 @@ ## Our standards -Be respectful, constructive, and focused on the technical work. Harassment, discrimination, personal attacks, and intentionally disruptive behavior are not acceptable. +Be respectful, constructive, and focused on the technical work. Respect other people's privacy and never publish their private information. Harassment, discrimination, personal attacks, and intentionally disruptive behavior are not acceptable. ## Scope @@ -11,3 +11,5 @@ This code of conduct applies to repository discussions, issues, pull requests, r ## Enforcement Project maintainers may edit, remove, or reject contributions and participation that violate these standards. + +Report conduct concerns privately to the maintainers rather than in public threads. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c170925..0eaecdc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,7 +33,7 @@ git remote add upstream https://github.com/Pharaonic/php-readable.git ## Choose the target branch -Check out the branch that matches the PHP version you are targeting: +Check out the branch that matches the PHP version you are targeting. For a bug fix, use the oldest line that has the bug: ```bash git checkout 8.2.x @@ -85,7 +85,7 @@ All checks must pass. CI also runs the tests with the lowest allowed dependency git push origin fix/compact-rounding ``` -Open the pull request against the version branch you started from, and fill in the pull request template. Do not submit the same change to multiple version branches unless asked to. +Open the pull request against the version branch you started from, and fill in the pull request template. Maintainers port fixes from the oldest affected line to the newer lines, so do not submit the same change to several branches. ## Code of Conduct diff --git a/README.md b/README.md index 4b4f13d..156c375 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,14 @@

PHP Version : 8.2 License - Tests + Build
- Source + Source Packagist Version Packagist Downloads

Human-friendly numbers, sizes, money and durations for PHP.

-
Format numbers, byte sizes, money and durations into readable text with predictable output, plus optional Intl locales.

## Installation diff --git a/composer.json b/composer.json index e597ff9..db89502 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "require-dev": { "laravel/pint": "^1.20", "pestphp/pest": "^3.8", - "phpstan/phpstan": "^2.1" + "phpstan/phpstan": "^2.1", + "phpunit/phpunit": "^11.5.50" }, "suggest": { "ext-intl": "Required only when passing a $locale to the Number, Money and Bytes formatters." diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8b80bda..2f64a88 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,11 +7,8 @@ failOnWarning="true" failOnDeprecation="true"> - - tests/Unit - - - tests/Feature + + tests diff --git a/tests/Feature/.gitkeep b/tests/Feature/.gitkeep deleted file mode 100644 index e69de29..0000000 From 44b6b82ff50d024e2705c95970d215ed035adc27 Mon Sep 17 00:00:00 2001 From: Moamen Eltouny Date: Tue, 1 Dec 2026 11:44:49 +0200 Subject: [PATCH 5/6] docs: add the upgrade guide and the 8.2.3 changelog --- CHANGELOG.md | 12 ++++++++++++ docs/sections/14-upgrade-guide.md | 23 +++++++++++++++++++++++ docs/sidebar.md | 2 ++ 3 files changed, 37 insertions(+) create mode 100644 docs/sections/14-upgrade-guide.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a57949..291d568 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes to this project are documented in this file. +## 8.2.3 - Unreleased + +### Fixed + +- `Money::format()` with a `$locale` no longer prints a negative zero for currencies whose Intl fraction digits differ from ISO 4217: `Money::format(-0.4, 'IQD', locale: 'en_US')` returned `"-IQD 0"` and now returns `"IQD 0"`. The amount is compared with Intl's own formatted zero instead of being rounded with the ISO 4217 digits. + +### Changed + +- The internal `Assert`, `Excerpt` and `Intl` helpers moved from `Pharaonic\Readable\Support` to `Pharaonic\Readable`. They stay `@internal` and are not part of the public API. +- The documentation has an upgrade guide. +- CI runs validation, style, static analysis, tests, the security audit and an archive check in order, behind one quality gate. + ## 8.2.2 - 2026-10-11 ### Fixed diff --git a/docs/sections/14-upgrade-guide.md b/docs/sections/14-upgrade-guide.md new file mode 100644 index 0000000..90899b4 --- /dev/null +++ b/docs/sections/14-upgrade-guide.md @@ -0,0 +1,23 @@ +## Upgrade Guide + +Readable has one release line per PHP version, and every line has the same classes, methods and output. This page covers the `8.2.x` line, for PHP 8.2. + +### Patch Releases + +Patch releases such as `8.2.0` to `8.2.3` never break your code. Update with: + +```bash no-line-numbers +composer update pharaonic/php-readable +``` + +Output changes only to fix a bug, and every such change is listed in the changelog. + +### Moving To Another PHP Version + +No code changes are needed. Composer picks the line that matches your PHP version, so after upgrading PHP run `composer update pharaonic/php-readable`. + +The constraint that `composer require` writes (for example `^8.2`) already allows the newer lines. If you pinned a line with a tilde constraint such as `~8.2.0`, widen it first, or Composer stays on `8.2.x` and cannot install on the new PHP version. + +### Changelog + +Every release is listed in the [changelog](https://github.com/Pharaonic/php-readable/blob/8.2.x/CHANGELOG.md) and on the [GitHub releases](https://github.com/Pharaonic/php-readable/releases) page. diff --git a/docs/sidebar.md b/docs/sidebar.md index d7ab76d..ecb4c1f 100644 --- a/docs/sidebar.md +++ b/docs/sidebar.md @@ -17,3 +17,5 @@ - [Troubleshooting](#troubleshooting) - Community - [Contributors](#contributors) +- Upgrades + - [Upgrade Guide](#upgrade-guide) From d2417a71725f8b9dfabb4a337395bbb6776f7b4e Mon Sep 17 00:00:00 2001 From: Moamen Eltouny Date: Tue, 1 Dec 2026 11:51:49 +0200 Subject: [PATCH 6/6] refactor: move the Intl helper to an integration service --- CHANGELOG.md | 2 +- MAINTAINING.md | 2 +- src/Money.php | 11 +++++----- src/Number.php | 21 ++++++++++--------- .../Integration/IntlService.php} | 7 ++++--- 5 files changed, 23 insertions(+), 20 deletions(-) rename src/{Intl.php => Services/Integration/IntlService.php} (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 291d568..ae0018b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to this project are documented in this file. ### Changed -- The internal `Assert`, `Excerpt` and `Intl` helpers moved from `Pharaonic\Readable\Support` to `Pharaonic\Readable`. They stay `@internal` and are not part of the public API. +- The internal `Support\Assert` and `Support\Excerpt` helpers moved to `Pharaonic\Readable`, and `Support\Intl` became `Pharaonic\Readable\Services\Integration\IntlService`. They stay `@internal` and are not part of the public API. - The documentation has an upgrade guide. - CI runs validation, style, static analysis, tests, the security audit and an archive check in order, behind one quality gate. diff --git a/MAINTAINING.md b/MAINTAINING.md index d6da160..e9c0d9d 100644 --- a/MAINTAINING.md +++ b/MAINTAINING.md @@ -18,7 +18,7 @@ One branch per PHP minor, each requiring exactly that minor (`"php": ">=8.N <8.N ## Public API invariants -- The public API is the static methods of `Arr`, `Str`, `Number`, `Bytes`, `Money` and `Duration`, and `Exceptions\MissingIntlExtension`. `Assert`, `Excerpt` and `Intl` are `@internal` helpers, not part of the public API. +- The public API is the static methods of `Arr`, `Str`, `Number`, `Bytes`, `Money` and `Duration`, and `Exceptions\MissingIntlExtension`. `Assert`, `Excerpt` and `Services\Integration\IntlService` are `@internal`, not part of the public API. - Without a `$locale`, output comes from the built-in English formatter and is identical on every machine, with or without `ext-intl`. - With a `$locale`, `ext-intl` is used; without it, `MissingIntlExtension` is thrown. Invalid locales never leak `ValueError` or `IntlException`. - Rounding is half away from zero everywhere, a rounded value carries to the next unit (`999 999 B` is `1 MB`), and no formatter prints negative zero. diff --git a/src/Money.php b/src/Money.php index b1fb97b..34a152d 100644 --- a/src/Money.php +++ b/src/Money.php @@ -5,6 +5,7 @@ namespace Pharaonic\Readable; use InvalidArgumentException; +use Pharaonic\Readable\Services\Integration\IntlService; /** * Readable money amounts. Formatting only: no arithmetic and no exchange rates. @@ -44,24 +45,24 @@ public static function format( } if ($locale !== null) { - $formatter = Intl::formatter($locale, 'CURRENCY'); + $formatter = IntlService::formatter($locale, 'CURRENCY'); if ($decimals !== null) { - Intl::fractionDigits($formatter, $decimals); + IntlService::fractionDigits($formatter, $decimals); } // Like the built-in path, an amount that rounds to zero must not print as "-$0.00". Intl rounds // with its own currency digits, which can differ from ISO 4217 (IQD has 0, not 3), so compare // against Intl's own zero instead of rounding here. if ($amount < 0) { - $zero = Intl::result($formatter->formatCurrency(0, $currency), $formatter); + $zero = IntlService::result($formatter->formatCurrency(0, $currency), $formatter); - if (Intl::result($formatter->formatCurrency(abs($amount), $currency), $formatter) === $zero) { + if (IntlService::result($formatter->formatCurrency(abs($amount), $currency), $formatter) === $zero) { return $zero; } } - return Intl::result($formatter->formatCurrency($amount, $currency), $formatter); + return IntlService::result($formatter->formatCurrency($amount, $currency), $formatter); } $decimals ??= self::fractionDigits($currency); diff --git a/src/Number.php b/src/Number.php index 2862f0e..0298cd9 100644 --- a/src/Number.php +++ b/src/Number.php @@ -5,6 +5,7 @@ namespace Pharaonic\Readable; use InvalidArgumentException; +use Pharaonic\Readable\Services\Integration\IntlService; /** * Readable numbers. @@ -46,10 +47,10 @@ public static function format( $number = self::withoutNegativeZero($number, $decimals); if ($locale !== null) { - $formatter = Intl::formatter($locale, 'DECIMAL'); - Intl::fractionDigits($formatter, $decimals, $trimZeros); + $formatter = IntlService::formatter($locale, 'DECIMAL'); + IntlService::fractionDigits($formatter, $decimals, $trimZeros); - return Intl::result($formatter->format($number), $formatter); + return IntlService::result($formatter->format($number), $formatter); } $formatted = is_int($number) @@ -100,10 +101,10 @@ public static function percentage(int|float $ratio, int $decimals = 0, ?string $ Assert::decimals($decimals); if ($locale !== null) { - $formatter = Intl::formatter($locale, 'PERCENT'); - Intl::fractionDigits($formatter, $decimals); + $formatter = IntlService::formatter($locale, 'PERCENT'); + IntlService::fractionDigits($formatter, $decimals); - return Intl::result($formatter->format(self::withoutNegativeZero($ratio, $decimals + 2)), $formatter); + return IntlService::result($formatter->format(self::withoutNegativeZero($ratio, $decimals + 2)), $formatter); } $percent = $ratio * 100; @@ -123,9 +124,9 @@ public static function percentage(int|float $ratio, int $decimals = 0, ?string $ public static function ordinal(int $number, ?string $locale = null): string { if ($locale !== null) { - $formatter = Intl::formatter($locale, 'ORDINAL'); + $formatter = IntlService::formatter($locale, 'ORDINAL'); - return Intl::result($formatter->format($number), $formatter); + return IntlService::result($formatter->format($number), $formatter); } $lastTwo = abs($number % 100); @@ -150,8 +151,8 @@ public static function spell(int|float $number, ?string $locale = null): string Assert::finite($number); if ($locale !== null) { - $formatter = Intl::formatter($locale, 'SPELLOUT'); - $words = Intl::result($formatter->format($number), $formatter); + $formatter = IntlService::formatter($locale, 'SPELLOUT'); + $words = IntlService::result($formatter->format($number), $formatter); // In Arabic the conjunction "و" attaches to the following word. return preg_match('/^ar(?:[_-]|$)/i', $locale) === 1 ? str_replace('و ', 'و', $words) : $words; diff --git a/src/Intl.php b/src/Services/Integration/IntlService.php similarity index 93% rename from src/Intl.php rename to src/Services/Integration/IntlService.php index 5339056..aabc485 100644 --- a/src/Intl.php +++ b/src/Services/Integration/IntlService.php @@ -2,18 +2,19 @@ declare(strict_types=1); -namespace Pharaonic\Readable; +namespace Pharaonic\Readable\Services\Integration; use IntlException; use InvalidArgumentException; use NumberFormatter; use Pharaonic\Readable\Exceptions\MissingIntlExtension; +use Pharaonic\Readable\Excerpt; use ValueError; /** - * @internal + * @internal Integration with ext-intl's NumberFormatter. */ -final class Intl +final class IntlService { /** * Create a NumberFormatter that rounds half away from zero, like number_format().