From f96f95299122d78a52b63a00b39b212466f154b9 Mon Sep 17 00:00:00 2001 From: Tint Naing Win Date: Thu, 18 Jul 2024 21:26:57 +0700 Subject: [PATCH] chore:support laravel11.x --- .editorconfig | 6 +- .gitattributes | 27 +++++--- CONTRIBUTING.md => .github/CONTRIBUTING.md | 0 .github/ISSUE_TEMPLATE/config.yml | 15 +++++ .github/SECURITY.md | 3 + .../workflows/fix-php-code-style-issues.yml | 32 +++++++++ .github/workflows/phpstan.yml | 38 +++++++++++ .github/workflows/run-tests.yml | 48 ++++++++++++++ .github/workflows/update-changelog.yml | 28 ++++++++ .gitignore | 6 +- README-en.md | 21 ++++-- README.md | 21 ++++-- composer.json | 23 ++++--- config/myanmar_phone.php | 4 +- phpstan-baseline.neon | 0 phpstan.neon.dist | 4 -- phpunit.xml.dist | 20 ++---- src/Contracts/MyanmarPhone.php | 65 +++---------------- .../AbstractMyanmarPhoneException.php | 5 +- src/Exceptions/FormatException.php | 4 +- src/Exceptions/InvalidNumber.php | 4 +- src/Exceptions/InvalidTelecom.php | 4 +- src/Facades/MyanPhone.php | 34 +++++----- src/Helpers/DataSource.php | 25 ++++--- src/MyanmarPhone.php | 50 ++------------ src/MyanmarPhoneServiceProvider.php | 6 +- src/Traits/Parser.php | 48 ++++++-------- src/Traits/Telecom.php | 35 +++------- src/Validations/MyanmarPhoneValidation.php | 6 +- tests/Helpers/Data.php | 9 ++- tests/Unit/PhoneNumberTest.php | 26 ++++---- 31 files changed, 350 insertions(+), 267 deletions(-) rename CONTRIBUTING.md => .github/CONTRIBUTING.md (100%) create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/SECURITY.md create mode 100644 .github/workflows/fix-php-code-style-issues.yml create mode 100644 .github/workflows/phpstan.yml create mode 100644 .github/workflows/run-tests.yml create mode 100644 .github/workflows/update-changelog.yml delete mode 100644 phpstan-baseline.neon diff --git a/.editorconfig b/.editorconfig index a7c44dd..cd8eb86 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,6 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + root = true [*] @@ -10,6 +13,3 @@ trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false - -[*.{yml,yaml}] -indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 886475c..8e0b76d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,13 +2,20 @@ # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html # Ignore all test and documentation with "export-ignore". -/.github export-ignore -/.gitattributes export-ignore -/.gitignore export-ignore -/phpunit.xml.dist export-ignore -/tests export-ignore -/.editorconfig export-ignore -/.php_cs.dist export-ignore -/psalm.xml export-ignore -/psalm.xml.dist export-ignore -/testbench.yaml export-ignore +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/art export-ignore +/docs export-ignore +/tests export-ignore +/.editorconfig export-ignore +/.php_cs.dist.php export-ignore +/.php_cs.php export-ignore +/auth.json export-ignore +/CHANGELOG.md export-ignore +/phpstan.neon.dist export-ignore +/phpstan-baseline.neon export-ignore +/phpunit.xml.dist export-ignore +/pint.json export-ignore +/README.md export-ignore +/UPGRADING.md export-ignore diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 100% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..9e00c36 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,15 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/Fawkes-Creatives/myanmar-phone/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/Fawkes-Creatives/myanmar-phone/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a security issue + url: https://github.com/Fawkes-Creatives/myanmar-phone/security/policy + about: Learn how to notify us for sensitive bugs + - name: Report a bug + url: https://github.com/Fawkes-Creatives/myanmar-phone/issues/new + about: Report a reproducable bug + diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..aa1ddde --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,3 @@ +# Security Policy + +If you discover any security related issues, please email fawkescreatives@gmail.com instead of using the issue tracker. 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..e8cb7e1 --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -0,0 +1,32 @@ +name: Check & fix styling + +on: [push] + +jobs: + php-code-styling: + runs-on: ubuntu-22.04 + + strategy: + fail-fast: true + + steps: + - name: Check out repository code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + tools: composer:v2 + coverage: none + + - name: Install composer dependencies + uses: nick-fields/retry@v2 + with: + timeout_minutes: 3 + max_attempts: 5 + command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress + + - name: Check code style + timeout-minutes: 2 + run: ./vendor/bin/pint --test --preset laravel diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..48f6ef2 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,38 @@ +name: PHPStan + +on: + push: + paths: + - '**.php' + - 'phpstan.neon.dist' + +jobs: + phpstan: + runs-on: ubuntu-22.04 + + strategy: + fail-fast: true + + name: Static Analysis + + steps: + - name: Check out repository code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + tools: composer:v2 + coverage: none + + - name: Install composer dependencies + uses: nick-fields/retry@v2 + with: + timeout_minutes: 3 + max_attempts: 5 + command: composer update --prefer-stable --prefer-dist --no-interaction --no-progress + + - name: Run PHPStan + timeout-minutes: 2 + run: ./vendor/bin/phpstan analyse --error-format=github diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..2ffc074 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,48 @@ +name: run-tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-22.04 + + strategy: + fail-fast: true + matrix: + php: [ 8.1, 8.2, 8.3 ] + laravel: [ 10, 11 ] + exclude: + - php: 8.1 + laravel: 11 + + name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + ini-values: error_reporting=E_ALL + tools: composer:v2 + 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 require "laravel/framework:^${{ matrix.laravel }}" --no-update + composer update --prefer-dist --no-interaction --no-progress + + - name: Execute tests + timeout-minutes: 2 + run: vendor/bin/pest diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..7437524 --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,28 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +jobs: + update: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v5 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md diff --git a/.gitignore b/.gitignore index 9a43686..4bdeaa3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,13 @@ +.DS_Store +.vagrant .idea .php_cs .php_cs.cache .phpunit.result.cache -build +composer.phar composer.lock coverage -docs +build phpunit.xml phpstan.neon testbench.yaml diff --git a/README-en.md b/README-en.md index bf93fbb..e184366 100644 --- a/README-en.md +++ b/README-en.md @@ -1,8 +1,8 @@

Laravel Myanmar Phone

[![Latest Version on Packagist](https://img.shields.io/packagist/v/fawkescreatives/myanmar-phone.svg)](https://packagist.org/packages/fawkescreatives/myanmar-phone) -[![Laravel 8.x](https://img.shields.io/badge/Laravel-8.x-red.svg)](https://laravel.com/docs/8.x) -[![Laravel 9.x](https://img.shields.io/badge/Laravel-9.x-red.svg)](https://laravel.com) +[![Laravel 10.x](https://img.shields.io/badge/Laravel-10.x-red.svg)](https://laravel.com/docs/10.x) +[![Laravel 11.x](https://img.shields.io/badge/Laravel-11.x-red.svg)](https://laravel.com/docs/11.x) [![Total Downloads](https://poser.pugx.org/fawkescreatives/myanmar-phone/downloads)](https://packagist.org/packages/fawkescreatives/myanmar-phone) [![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://packagist.org/packages/fawkescreatives/myanmar-phone) @@ -103,6 +103,19 @@ You can run the tests with: composer test ``` -### License +## Changelog -The MIT License (MIT). Please see [License File](https://github.com/Fawkes-Creatives/myanmar-phone/blob/main/LICENSE.md) for more information. +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. + + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/README.md b/README.md index 1edabdc..55ce331 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@

Laravel Myanmar Phone

[![Latest Version on Packagist](https://img.shields.io/packagist/v/fawkescreatives/myanmar-phone.svg)](https://packagist.org/packages/fawkescreatives/myanmar-phone) -[![Laravel 8.x](https://img.shields.io/badge/Laravel-8.x-red.svg)](https://laravel.com/docs/8.x) -[![Laravel 9.x](https://img.shields.io/badge/Laravel-9.x-red.svg)](https://laravel.com) +[![Laravel 10.x](https://img.shields.io/badge/Laravel-10.x-red.svg)](https://laravel.com/docs/10.x) +[![Laravel 11.x](https://img.shields.io/badge/Laravel-11.x-red.svg)](https://laravel.com/docs/11.x) [![Total Downloads](https://poser.pugx.org/fawkescreatives/myanmar-phone/downloads)](https://packagist.org/packages/fawkescreatives/myanmar-phone) [![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://packagist.org/packages/fawkescreatives/myanmar-phone) @@ -118,7 +118,18 @@ You can run the tests with: composer test ``` -### License +## Changelog -The MIT License (MIT). Please see [License File](https://github.com/Fawkes-Creatives/myanmar-phone/blob/main/LICENSE.md) -for more information. +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json index 26db99a..89f8735 100644 --- a/composer.json +++ b/composer.json @@ -20,16 +20,16 @@ } ], "require": { - "php": "^7.3|^8.0", - "illuminate/support": "^8.0|^9.0", + "php": "^8.1", + "illuminate/contracts": "^10.0|^11.0", "giggsey/libphonenumber-for-php": "^8.0" }, "require-dev": { - "nunomaduro/collision": "^5.10|^6.1", - "nunomaduro/larastan": "^1.0|^2.0", - "orchestra/testbench": "^6.22|^7.0", - "pestphp/pest": "^1.21", - "pestphp/pest-plugin-laravel": "^1.1", + "laravel/pint": "^1.10", + "larastan/larastan": "^2.0", + "orchestra/testbench": "^8.11|^9.0", + "pestphp/pest": "^2.0", + "pestphp/pest-plugin-laravel": "^2.0", "phpstan/extension-installer": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0" @@ -47,10 +47,15 @@ "scripts": { "analyse": "vendor/bin/phpstan analyse", "test": "vendor/bin/pest", - "test-coverage": "vendor/bin/pest --coverage" + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true + } }, "extra": { "laravel": { diff --git a/config/myanmar_phone.php b/config/myanmar_phone.php index f882f97..8afd8b1 100644 --- a/config/myanmar_phone.php +++ b/config/myanmar_phone.php @@ -15,5 +15,5 @@ | RFC3966 => 3 (eg.., tel:+95-9-123-456-789) | */ - 'default_format' => 0, // 0 || 1 || 2 || 3 -]; \ No newline at end of file + 'default_format' => 0, // 0 || 1 || 2 || 3 +]; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon deleted file mode 100644 index e69de29..0000000 diff --git a/phpstan.neon.dist b/phpstan.neon.dist index e005ac7..6a9db1d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,6 +1,3 @@ -includes: - - phpstan-baseline.neon - parameters: level: 4 paths: @@ -9,5 +6,4 @@ parameters: tmpDir: build/phpstan checkOctaneCompatibility: true checkModelProperties: true - checkMissingIterableValueType: false diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a817321..4e8aaf8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,36 +3,26 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" backupGlobals="false" - backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" - convertErrorsToExceptions="true" - convertNoticesToExceptions="true" - convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" + stopOnFailure="true" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" - verbose="true" > - - tests + + ./tests/ - + ./src - - - - - - + diff --git a/src/Contracts/MyanmarPhone.php b/src/Contracts/MyanmarPhone.php index d8a8da8..5abad1d 100644 --- a/src/Contracts/MyanmarPhone.php +++ b/src/Contracts/MyanmarPhone.php @@ -11,80 +11,35 @@ interface MyanmarPhone { /** - * @param string $number - * @return MyanmarPhone * @throws InvalidNumber * @throws NumberParseException */ public function make(string $number): ?self; /** - * @param string|int|null $format - * @return string + * @param string|int|null $format */ public function format($format = null): string; - /** - * @return string - */ public function formatE164(): string; - /** - * @return string - */ public function formatInternational(): string; - /** - * @param string|null $separator - * @return string - */ - public function formatRFC3966(string $separator = null): string; + public function formatRFC3966(?string $separator = null): string; - /** - * @param string|null $separator - * @return string - */ - public function formatNational(string $separator = null): string; + public function formatNational(?string $separator = null): string; - /** - * @param string|null $number - * @return string - */ - public function operator(string $number = null): string; + public function operator(?string $number = null): string; - /** - * @param string|null $number - * @return string - */ - public function telecom(string $number = null): string; + public function telecom(?string $number = null): string; - /** - * @param string|null $number - * @return bool - */ - public function isTelenor(string $number = null): bool; + public function isTelenor(?string $number = null): bool; - /** - * @param string|null $number - * @return bool - */ - public function isOoredoo(string $number = null): bool; + public function isOoredoo(?string $number = null): bool; - /** - * @param string|null $number - * @return bool - */ - public function isMpt(string $number = null): bool; + public function isMpt(?string $number = null): bool; - /** - * @param string|null $number - * @return bool - */ - public function isMyTel(string $number = null): bool; + public function isMyTel(?string $number = null): bool; - /** - * @param string|null $number - * @return bool - */ - public function isMec(string $number = null): bool; + public function isMec(?string $number = null): bool; } diff --git a/src/Exceptions/AbstractMyanmarPhoneException.php b/src/Exceptions/AbstractMyanmarPhoneException.php index 623fdae..c41199d 100644 --- a/src/Exceptions/AbstractMyanmarPhoneException.php +++ b/src/Exceptions/AbstractMyanmarPhoneException.php @@ -7,7 +7,4 @@ use Exception; -abstract class AbstractMyanmarPhoneException extends Exception -{ - -} \ No newline at end of file +abstract class AbstractMyanmarPhoneException extends Exception {} diff --git a/src/Exceptions/FormatException.php b/src/Exceptions/FormatException.php index 309507d..dccaf76 100644 --- a/src/Exceptions/FormatException.php +++ b/src/Exceptions/FormatException.php @@ -9,5 +9,5 @@ class FormatException extends AbstractMyanmarPhoneException { protected $code = 400; - protected $message = "Invalid format type"; -} \ No newline at end of file + protected $message = 'Invalid format type'; +} diff --git a/src/Exceptions/InvalidNumber.php b/src/Exceptions/InvalidNumber.php index 28d5078..3ff0908 100644 --- a/src/Exceptions/InvalidNumber.php +++ b/src/Exceptions/InvalidNumber.php @@ -9,5 +9,5 @@ class InvalidNumber extends AbstractMyanmarPhoneException { protected $code = 400; - protected $message = "Invalid phone number"; -} \ No newline at end of file + protected $message = 'Invalid phone number'; +} diff --git a/src/Exceptions/InvalidTelecom.php b/src/Exceptions/InvalidTelecom.php index 58fd7d6..bb97be9 100644 --- a/src/Exceptions/InvalidTelecom.php +++ b/src/Exceptions/InvalidTelecom.php @@ -9,5 +9,5 @@ class InvalidTelecom extends AbstractMyanmarPhoneException { protected $code = 400; - protected $message = "Invalid Telecom"; -} \ No newline at end of file + protected $message = 'Invalid Telecom'; +} diff --git a/src/Facades/MyanPhone.php b/src/Facades/MyanPhone.php index e6f1e80..5451c99 100644 --- a/src/Facades/MyanPhone.php +++ b/src/Facades/MyanPhone.php @@ -7,28 +7,28 @@ use Illuminate\Support\Facades\Facade; use MyanmarPhone\Contracts\MyanmarPhone; +use MyanmarPhone\Helpers\DataSource; /** - * @method make(string $number)) - * @method getPhoneInstance() - * @method getDataSource() - * @method setStrPhoneNumber(string $number) - * @method getStrPhoneNumber() - * @method getCountryCode() - * @method getPhoneNumber(bool $leadingZero = true) - * @method format($format = null) - * @method setSeparator($separator = null) - * @method getSeparator() - * @method response(string $number) - * @method formatE164() - * @method formatInternational() - * @method formatRFC3966(string $separator = null) - * @method formatNational(string $separator = null) - * @method check() + * @method static \MyanmarPhone\MyanmarPhone make(string $number)) + * @method static \libphonenumber\PhoneNumber getPhoneInstance() + * @method static DataSource getDataSource() + * @method static \MyanmarPhone\MyanmarPhone setStrPhoneNumber(string $number) + * @method static string|null getStrPhoneNumber() + * @method static string|null getCountryCode() + * @method static string getPhoneNumber(bool $leadingZero = true) + * @method static string format($format = null) + * @method static \MyanmarPhone\MyanmarPhone setSeparator($separator = null) + * @method static string|null getSeparator() + * @method static string|null response(string $number) + * @method static string formatE164() + * @method static string formatInternational() + * @method static string formatRFC3966(string $separator = null) + * @method static string formatNational(string $separator = null) + * @method static bool check() * * @see \MyanmarPhone\MyanmarPhone */ - class MyanPhone extends Facade { protected static function getFacadeAccessor(): string diff --git a/src/Helpers/DataSource.php b/src/Helpers/DataSource.php index 7c3530c..a740609 100644 --- a/src/Helpers/DataSource.php +++ b/src/Helpers/DataSource.php @@ -14,49 +14,48 @@ class DataSource * Regular expression pattern of variable. Check for Network Providers. */ const MEC_PATTERN_FOR_MATCHING = '/^(09|\+?959)3\d{7}$/'; + const MPT_PATTERN_FOR_MATCHING = '/^(09|\+?959)(2[0-4]\d{5}|5[0-6]\d{5}|8[13-7]\d{5}|4[1379]\d{6}|73\d{6}|91\d{6}|25\d{7}|26[0-5]\d{6}|40[0-4]\d{6}|42\d{7}|44[0-589]\d{6}|45\d{7}|87\d{7}|89[6789]\d{6})$/'; + const TELENOR_PATTERN_FOR_MATCHING = '/^(09|\+?959)7\d{8}$/'; + const OOREDOO_PATTERN_FOR_MATCHING = '/^(09|\+?959)9\d{8}$/'; + const MYTEL_PATTERN_FOR_MATCHING = '/^(09|\+?959)6\d{8}$/'; const MPT = 'MPT'; + const MEC = 'MEC'; + const TELENOR = 'TELENOR'; + const OOREDOO = 'OOREDOO'; + const MYTEL = 'MYTEL'; protected $regionCode = 'MM'; + protected $prefixCode = '95'; + protected $normalizeMaxLength = 11; + protected $pureNumberMinLength = 7; - /** - * @return string - */ public function getRegionCode(): string { return $this->regionCode; } - /** - * @return string - */ public function getPrefixCode(): string { return $this->prefixCode; } - /** - * @return int - */ public function getNormalizeMaxLength(): int { return $this->normalizeMaxLength; } - /** - * @return int - */ public function getPureNumberMinLength(): int { return $this->pureNumberMinLength; @@ -69,4 +68,4 @@ public function getDefaultFormat() { return config('myanmar_phone.default_format'); } -} \ No newline at end of file +} diff --git a/src/MyanmarPhone.php b/src/MyanmarPhone.php index e6ef82b..47b5c19 100644 --- a/src/MyanmarPhone.php +++ b/src/MyanmarPhone.php @@ -11,10 +11,10 @@ use libphonenumber\PhoneNumber; use libphonenumber\PhoneNumberFormat; use libphonenumber\PhoneNumberUtil; +use MyanmarPhone\Contracts\MyanmarPhone as MyanmarPhoneContract; use MyanmarPhone\Exceptions\InvalidNumber; use MyanmarPhone\Helpers\DataSource; use MyanmarPhone\Traits\Parser; -use MyanmarPhone\Contracts\MyanmarPhone as MyanmarPhoneContract; use MyanmarPhone\Traits\Telecom; class MyanmarPhone implements MyanmarPhoneContract @@ -56,12 +56,10 @@ public function __construct(DataSource $dataSource) } /** - * @param string $number - * @return MyanmarPhoneContract * @throws InvalidNumber * @throws NumberParseException */ - public function make(string $number): MyanmarPhoneContract + public function make(string $number): self { $number = $this->normalize($number); @@ -71,53 +69,33 @@ public function make(string $number): MyanmarPhoneContract return $this; } - /** - * @return PhoneNumber - */ public function getPhoneInstance(): PhoneNumber { return $this->phoneInstance; } - /** - * @return DataSource - */ public function getDataSource(): DataSource { return $this->dataSource; } - /** - * @param string $number - * @return MyanmarPhone - */ - public function setStrPhoneNumber(string $number): MyanmarPhone + public function setStrPhoneNumber(string $number): self { $this->strPhoneNumber = $number; return $this; } - /** - * @return string - */ public function getStrPhoneNumber(): ?string { return $this->strPhoneNumber; } - /** - * @return null|string - */ public function getCountryCode(): ?string { return (string) $this->getPhoneInstance()->getCountryCode(); } - /** - * @param bool $leadingZero - * @return string - */ public function getPhoneNumber(bool $leadingZero = true): string { $number = $this->getStrPhoneNumber(); @@ -135,7 +113,7 @@ public function getPhoneNumber(bool $leadingZero = true): string /** * @param string|int|null $format - * @return string + * * @throws Exception */ public function format($format = null): string @@ -165,21 +143,14 @@ public function setSeparator($separator = null): self return $this; } - /** - * @return string|null - */ public function getSeparator(): ?string { return $this->separator; } - /** - * @param string $number - * @return null|string - */ protected function response(string $number): ?string { - if (!is_null($this->getSeparator())) { + if (! is_null($this->getSeparator())) { $number = $this->normalizeWhiteSpaceAndDash($number, $this->getSeparator()); } @@ -187,7 +158,6 @@ protected function response(string $number): ?string } /** - * @return string * @throws Exception */ public function formatE164(): string @@ -196,7 +166,6 @@ public function formatE164(): string } /** - * @return string * @throws Exception */ public function formatInternational(): string @@ -205,11 +174,9 @@ public function formatInternational(): string } /** - * @param string|null $separator - * @return string * @throws Exception */ - public function formatRFC3966(string $separator = null): string + public function formatRFC3966(?string $separator = null): string { return $this ->setSeparator($separator) @@ -217,11 +184,9 @@ public function formatRFC3966(string $separator = null): string } /** - * @param string|null $separator - * @return string * @throws Exception */ - public function formatNational(string $separator = null): string + public function formatNational(?string $separator = null): string { return $this ->setSeparator($separator) @@ -229,7 +194,6 @@ public function formatNational(string $separator = null): string } /** - * @return bool * @throws Exception */ public function check(): bool diff --git a/src/MyanmarPhoneServiceProvider.php b/src/MyanmarPhoneServiceProvider.php index b15777e..466c29c 100644 --- a/src/MyanmarPhoneServiceProvider.php +++ b/src/MyanmarPhoneServiceProvider.php @@ -19,16 +19,16 @@ public function boot() { if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__ . './../config/myanmar_phone.php' => $this->app->configPath('myanmar_phone.php'), + __DIR__.'./../config/myanmar_phone.php' => $this->app->configPath('myanmar_phone.php'), ]); } - $this->app['validator']->extendDependent('myanmar_phone', MyanmarPhoneValidation::class . '@validate'); + $this->app['validator']->extendDependent('myanmar_phone', MyanmarPhoneValidation::class.'@validate'); } public function register() { - $this->mergeConfigFrom(__DIR__ . '/../config/myanmar_phone.php', 'myanmar_phone'); + $this->mergeConfigFrom(__DIR__.'/../config/myanmar_phone.php', 'myanmar_phone'); $this->app->bind(MyanmarPhoneContract::class, function ($app) { return $app->make(MyanmarPhone::class); diff --git a/src/Traits/Parser.php b/src/Traits/Parser.php index a3dae2e..abba776 100644 --- a/src/Traits/Parser.php +++ b/src/Traits/Parser.php @@ -13,8 +13,9 @@ trait Parser { /** - * @param int|string $format + * @param int|string $format * @return int|string + * * @throws FormatException */ public function parseFormat($format) @@ -22,20 +23,19 @@ public function parseFormat($format) if (in_array($format, static::libphonenumberFormats())) { return $format; } - + throw new FormatException(); } - + /** * Return phone number formats with libphonenumber - * @return array */ protected static function libphonenumberFormats(): array { - return with(new ReflectionClass(PhoneNumberFormat::class)) + return (new ReflectionClass(PhoneNumberFormat::class)) ->getConstants(); } - + /** * @throws InvalidNumber */ @@ -44,76 +44,66 @@ public function normalize(string $number): string if (mb_strlen($number) < $this->getDataSource()->getPureNumberMinLength()) { throw new InvalidNumber(); } - + $number = $this->normalizeDigits($number); $number = $this->normalizeLeadingZero($number); $number = $this->normalizeAreaCode($number); - + return $this->normalizeCountryCode($number); } - + /** * Get only digits - * @param string $number - * @return string */ public function normalizeDigits(string $number): string { return preg_replace('/[^0-9]/', '', trim($number)); } - + /** * Remove leading zero - * @param string $number - * @return string */ public function normalizeLeadingZero(string $number): string { - return ltrim($number, "0"); + return ltrim($number, '0'); } - + public function normalizeAreaCode($number): string { return sprintf('%9d', $number); } - + /** * Remove country code - * @param string $number - * @return string */ public function normalizeCountryCode(string $number): string { $max = $this->getDataSource()->getNormalizeMaxLength(); $prefixCode = $this->getDataSource()->getPrefixCode(); - + do { $number = preg_replace("/^\+?$prefixCode/", '', $number); } while (mb_strlen($number) > $max); - + return $number; } - + /** - * @param string $number * @param string $replacement - * @return string|null */ public function normalizeWhiteSpaceAndDash(string $number, $replacement = ''): ?string { return preg_replace('/[- )(]/', $replacement, trim($number)); } - + /** - * @param string $number - * @return string * @throws InvalidNumber */ public function parseStrPhoneNumber(string $number): string { $number = $this->normalize($number); $this->setStrPhoneNumber($number); - + return $this->getPhoneNumber(true); } -} \ No newline at end of file +} diff --git a/src/Traits/Telecom.php b/src/Traits/Telecom.php index 552f04a..5182050 100644 --- a/src/Traits/Telecom.php +++ b/src/Traits/Telecom.php @@ -12,12 +12,9 @@ trait Telecom { /** - * @param string|null $number - * @param string $pattern - * @return bool * @throws Exception */ - protected function isOperator(string $number = null, string $pattern): bool + protected function isOperator(?string $number, string $pattern): bool { if (is_null($number)) { $number = $this->formatE164(); @@ -25,66 +22,54 @@ protected function isOperator(string $number = null, string $pattern): bool $number = $this->parseStrPhoneNumber($number); } - return (bool)preg_match($pattern, $number); + return (bool) preg_match($pattern, $number); } /** - * @param string|null $number - * @return bool * @throws Exception */ - public function isTelenor(string $number = null): bool + public function isTelenor(?string $number = null): bool { return $this->isOperator($number, DataSource::TELENOR_PATTERN_FOR_MATCHING); } /** - * @param string|null $number - * @return bool * @throws Exception */ - public function isOoredoo(string $number = null): bool + public function isOoredoo(?string $number = null): bool { return $this->isOperator($number, DataSource::OOREDOO_PATTERN_FOR_MATCHING); } /** - * @param string|null $number - * @return bool * @throws Exception */ - public function isMpt(string $number = null): bool + public function isMpt(?string $number = null): bool { return $this->isOperator($number, DataSource::MPT_PATTERN_FOR_MATCHING); } /** - * @param string|null $number - * @return bool * @throws Exception */ - public function isMyTel(string $number = null): bool + public function isMyTel(?string $number = null): bool { return $this->isOperator($number, DataSource::MYTEL_PATTERN_FOR_MATCHING); } /** - * @param string|null $number - * @return bool * @throws Exception */ - public function isMec(string $number = null): bool + public function isMec(?string $number = null): bool { return $this->isOperator($number, DataSource::MEC_PATTERN_FOR_MATCHING); } /** - * @param string|null $number - * @return string * @throws InvalidTelecom * @throws Exception */ - public function telecom(string $number = null): string + public function telecom(?string $number = null): string { if ($this->isMpt($number)) { return DataSource::MPT; @@ -106,11 +91,9 @@ public function telecom(string $number = null): string } /** - * @param string|null $number - * @return string * @throws InvalidTelecom */ - public function operator(string $number = null): string + public function operator(?string $number = null): string { return $this->telecom($number); } diff --git a/src/Validations/MyanmarPhoneValidation.php b/src/Validations/MyanmarPhoneValidation.php index 6383ba0..83feeae 100644 --- a/src/Validations/MyanmarPhoneValidation.php +++ b/src/Validations/MyanmarPhoneValidation.php @@ -18,12 +18,12 @@ class MyanmarPhoneValidation * @var Application|mixed|MyanmarPhone */ protected $myanmarPhone; - + public function __construct() { $this->myanmarPhone = app(MyanmarPhone::class); } - + /** * @throws BindingResolutionException * @throws NumberParseException @@ -34,4 +34,4 @@ public function validate($attribute, $value, array $parameters, $validator): boo { return $this->myanmarPhone->make($value)->check(); } -} \ No newline at end of file +} diff --git a/tests/Helpers/Data.php b/tests/Helpers/Data.php index 4da0c8e..99ca0b1 100644 --- a/tests/Helpers/Data.php +++ b/tests/Helpers/Data.php @@ -8,13 +8,20 @@ class Data { const NATIONAL_NUMBER = '9252345678'; + const INTERNATIONAL_NUMBER = '+959252345678'; + const LOCAL_NUMBER = '09252345678'; + const INVALID_NUMBER = '09123456789'; const OOREDOO_NUMBER = '09978412345'; + const MPT_NUMBER = '09252345678'; + const TELENOR_NUMBER = '09789123456'; + const MEC_NUMBER = '0933123456'; + const MYTEL_NUMBER = '09671234567'; -} \ No newline at end of file +} diff --git a/tests/Unit/PhoneNumberTest.php b/tests/Unit/PhoneNumberTest.php index 7d37c5d..d69bd7c 100644 --- a/tests/Unit/PhoneNumberTest.php +++ b/tests/Unit/PhoneNumberTest.php @@ -16,30 +16,30 @@ config()->set('myanmar_phone.default_format', PhoneNumberFormat::E164); $phone = MyanPhone::make(Data::LOCAL_NUMBER); $result = $phone->format(); - $this->assertStringStartsWith("+95", $result); + $this->assertStringStartsWith('+95', $result); }); it('is config default format is international', function () { config()->set('myanmar_phone.default_format', PhoneNumberFormat::INTERNATIONAL); $phone = MyanPhone::make(Data::NATIONAL_NUMBER); $result = $phone->format(); - $this->assertStringContainsString("+95 ", $result); + $this->assertStringContainsString('+95 ', $result); }); it('is config default format is national', function () { config()->set('myanmar_phone.default_format', PhoneNumberFormat::NATIONAL); $phone = MyanPhone::make(Data::LOCAL_NUMBER); $result = $phone->format(); - $this->assertStringStartsWith("09", $result); - $this->assertStringContainsString(" ", $result); + $this->assertStringStartsWith('09', $result); + $this->assertStringContainsString(' ', $result); }); it('is config default format is rfc3966', function () { config()->set('myanmar_phone.default_format', PhoneNumberFormat::RFC3966); $phone = MyanPhone::make(Data::LOCAL_NUMBER); $result = $phone->format(); - $this->assertStringStartsWith("tel", $result); - $this->assertStringContainsString("-", $result); + $this->assertStringStartsWith('tel', $result); + $this->assertStringContainsString('-', $result); }); it('can be format', function () { @@ -49,29 +49,29 @@ it('can be formatE164', function () { $result = MyanPhone::make(Data::LOCAL_NUMBER)->formatE164(); - $this->assertStringStartsWith("+95", $result); + $this->assertStringStartsWith('+95', $result); }); it('can be formatInternational', function () { $result = MyanPhone::make(Data::LOCAL_NUMBER)->formatInternational(); - $this->assertStringContainsString("+95 ", $result); + $this->assertStringContainsString('+95 ', $result); }); it('can be formatRFC3966', function () { $result = MyanPhone::make(Data::LOCAL_NUMBER)->formatRFC3966(); - $this->assertStringStartsWith("tel", $result); - $this->assertStringContainsString("-", $result); + $this->assertStringStartsWith('tel', $result); + $this->assertStringContainsString('-', $result); $result = MyanPhone::make(Data::LOCAL_NUMBER)->formatRFC3966(''); - $this->assertStringNotContainsString("-", $result); + $this->assertStringNotContainsString('-', $result); }); it('can be formatNational', function () { $result = MyanPhone::make(Data::LOCAL_NUMBER)->formatNational(); - $this->assertStringStartsWith("09 ", $result); + $this->assertStringStartsWith('09 ', $result); $result = MyanPhone::make(Data::LOCAL_NUMBER)->formatNational('_'); - $this->assertStringContainsString("_", $result); + $this->assertStringContainsString('_', $result); }); it('can get operator', function () {