Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from zentlix/feature/spiral-validator
Adding PhoneNumberChecker
- Loading branch information
Showing
10 changed files
with
227 additions
and
16 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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
/.github export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/tests export-ignore | ||
/.editorconfig export-ignore | ||
/.php_cs.dist.php export-ignore | ||
/psalm.xml export-ignore | ||
/psalm.xml.dist export-ignore | ||
/UPGRADING.md 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.php export-ignore | ||
/.php-cs-fixer.dist.php export-ignore | ||
/psalm.xml export-ignore | ||
/psalm.xml.dist export-ignore | ||
/UPGRADING.md export-ignore |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
# Changelog | ||
|
||
## 1.1.0 - 2023-02-01 | ||
- **Other Features** | ||
- Added `Spiral\PhoneNumber\Validator\Checker\PhoneNumberChecker` for the | ||
[Spiral Validator](https://github.com/spiral/validator) package. | ||
|
||
## 1.0.0 - 2023-01-08 | ||
|
||
- initial release | ||
- initial release |
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
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\PhoneNumber\Validator\Checker; | ||
|
||
use libphonenumber\PhoneNumber; | ||
use libphonenumber\PhoneNumberUtil; | ||
use Spiral\PhoneNumber\Config\PhoneNumberConfig; | ||
use Spiral\Validator\AbstractChecker; | ||
|
||
final class PhoneNumberChecker extends AbstractChecker | ||
{ | ||
public function __construct( | ||
private readonly PhoneNumberConfig $config | ||
) { | ||
} | ||
|
||
public const MESSAGES = [ | ||
'isValid' => '[[This value is not a valid phone number.]]', | ||
]; | ||
|
||
public function isValid(string $number): bool | ||
{ | ||
$phoneUtil = PhoneNumberUtil::getInstance(); | ||
|
||
try { | ||
/** @var PhoneNumber $phoneNumber */ | ||
$phoneNumber = $phoneUtil->parse($number, $this->config->getDefaultRegion()); | ||
|
||
return $phoneUtil->isValidNumber($phoneNumber); | ||
} catch (\Throwable) { | ||
return false; | ||
} | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\PhoneNumber\Tests\App\Request; | ||
|
||
use Spiral\Filters\Attribute\Input\Post; | ||
use Spiral\Filters\Model\Filter; | ||
use Spiral\Filters\Model\FilterDefinitionInterface; | ||
use Spiral\Filters\Model\HasFilterDefinition; | ||
use Spiral\Validator\FilterDefinition; | ||
|
||
final class OtherPhoneRequest extends Filter implements HasFilterDefinition | ||
{ | ||
#[Post] | ||
public string $phone; | ||
|
||
#[Post(key: 'phone_with_custom_error')] | ||
public string $phoneWithCustomError; | ||
|
||
public function filterDefinition(): FilterDefinitionInterface | ||
{ | ||
return new FilterDefinition([ | ||
'phone' => ['phone'], | ||
'phoneWithCustomError' => [ | ||
['phone', 'error' => 'Custom error.'] | ||
] | ||
]); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
tests/src/Functional/Validator/Checker/PhoneNumberCheckerTest.php
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\PhoneNumber\Tests\Functional\Validator\Constraints; | ||
|
||
use Psr\Http\Message\ServerRequestFactoryInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Spiral\Filters\Exception\ValidationException; | ||
use Spiral\PhoneNumber\Tests\App\Request\OtherPhoneRequest; | ||
use Spiral\PhoneNumber\Tests\Functional\TestCase; | ||
|
||
final class PhoneNumberCheckerTest extends TestCase | ||
{ | ||
public function testSuccessValidation(): void | ||
{ | ||
$this->getContainer()->bind( | ||
ServerRequestInterface::class, | ||
$this->createRequest(['phone' => '+1 650 253 0000', 'phone_with_custom_error' => '+16502530000']) | ||
); | ||
|
||
$filter = $this->getContainer()->get(OtherPhoneRequest::class); | ||
|
||
$this->assertSame('+1 650 253 0000', $filter->phone); | ||
$this->assertSame('+16502530000', $filter->phoneWithCustomError); | ||
} | ||
|
||
public function testFailValidation(): void | ||
{ | ||
$this->getContainer()->bind( | ||
ServerRequestInterface::class, | ||
$this->createRequest(['phone' => 'foo', 'phone_with_custom_error' => 'bar']) | ||
); | ||
|
||
$exception = null; | ||
try { | ||
$this->getContainer()->get(OtherPhoneRequest::class); | ||
} catch (ValidationException $e) { | ||
$exception = $e; | ||
} | ||
|
||
$this->assertInstanceOf(ValidationException::class, $exception); | ||
$this->assertSame('This value is not a valid phone number.', $exception->errors['phone']); | ||
$this->assertSame('Custom error.', $exception->errors['phone_with_custom_error']); | ||
} | ||
|
||
private function createRequest(array $data): ServerRequestInterface | ||
{ | ||
$factory = $this->getContainer()->get(ServerRequestFactoryInterface::class); | ||
|
||
return $factory->createServerRequest('POST', '/foo')->withParsedBody($data); | ||
} | ||
} |