-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validation rule for matching date to ID (#6)
- Loading branch information
1 parent
e0d073c
commit bc16fc8
Showing
2 changed files
with
142 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
// This file is part of package spoorsny/laravel-south-african-id. | ||
// | ||
// Package spoorsny/laravel-south-african-id is free software: you can | ||
// redistribute it and/or modify it under the terms of the GNU General Public | ||
// License as published by the Free Software Foundation, either version 3 of | ||
// the License, or (at your option) any later version. | ||
// | ||
// Package spoorsny/laravel-south-african-id is distributed in the hope that it | ||
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
// Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with | ||
// package spoorsny/laravel-south-african-id. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace Spoorsny\Laravel\Rules; | ||
|
||
use Carbon\Carbon; | ||
use Closure; | ||
use Illuminate\Contracts\Validation\DataAwareRule; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Spoorsny\ValueObjects\SouthAfricanId; | ||
|
||
/** | ||
* Rule that validates that a date string matches the date segment of a South African identity number. | ||
* | ||
* @see {@link https://laravel.com/docs/11.x/validation#using-rule-objects} | ||
* | ||
* @author Geoffrey Bernardo van Wyk <geoffrey@vanwyk.biz> | ||
* @copyright 2024 Geoffrey Bernardo van Wyk {@link https://geoffreyvanwyk.dev} | ||
* @license {@link http://www.gnu.org/copyleft/gpl.html} GNU GPL v3 or later | ||
*/ | ||
class BirthDateMatchSouthAfricanId implements DataAwareRule, ValidationRule | ||
{ | ||
/** | ||
* All of the data under validation. | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
protected array $data = []; | ||
|
||
/** | ||
* Run the validation rule. | ||
* | ||
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
if (! isset($this->data['south_african_id'])) { | ||
return; | ||
} | ||
|
||
try { | ||
$southAfricanId = new SouthAfricanId($this->data['south_african_id']); | ||
} catch (\Throwable $th) { | ||
return; | ||
} | ||
|
||
$birthDate = new Carbon($value); | ||
|
||
if ($birthDate->format('ymd') === $southAfricanId->dateSegment()) { | ||
return; | ||
} | ||
|
||
$fail('The :attribute field does not match the South African ID field.'); | ||
} | ||
|
||
/** | ||
* Set the data under validation. | ||
* | ||
* @param array<string, mixed> $data | ||
*/ | ||
public function setData(array $data): static | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
// This file is part of package spoorsny/laravel-south-african-id. | ||
// | ||
// Package spoorsny/laravel-south-african-id is free software: you can | ||
// redistribute it and/or modify it under the terms of the GNU General Public | ||
// License as published by the Free Software Foundation, either version 3 of | ||
// the License, or (at your option) any later version. | ||
// | ||
// Package spoorsny/laravel-south-african-id is distributed in the hope that it | ||
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty | ||
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
// Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License along with | ||
// package spoorsny/laravel-south-african-id. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace Spoorsny\Laravel\Tests\Unit; | ||
|
||
use Illuminate\Support\Facades\Validator; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Spoorsny\Laravel\Rules\BirthDateMatchSouthAfricanId; | ||
use Spoorsny\Laravel\Tests\TestCase; | ||
|
||
/** | ||
* Unit test for validation rule \Spoorsny\Laravel\Rules\BirthDateMatchSouthAfricanId. | ||
* | ||
* @author Geoffrey Bernardo van Wyk <geoffrey@vanwyk.biz> | ||
* @copyright 2024 Geoffrey Bernardo van Wyk {@link https://geoffreyvanwyk.dev} | ||
* @license {@link http://www.gnu.org/copyleft/gpl.html} GNU GPL v3 or later | ||
*/ | ||
#[CoversClass(BirthDateMatchSouthAfricanId::class)] | ||
class BirthDateMatchSouthAfricanIdRuleTest extends TestCase | ||
{ | ||
#[Test] | ||
public function it_passes_matching_date(): void | ||
{ | ||
$validator = Validator::make([ | ||
'south_african_id' => '240620 3710 097', | ||
'birth_date' => '1924-06-20', | ||
], [ | ||
'birth_date' => [new BirthDateMatchSouthAfricanId], | ||
]); | ||
|
||
$this->assertTrue($validator->passes()); | ||
} | ||
|
||
#[Test] | ||
public function it_fails_nonmatching_date(): void | ||
{ | ||
$validator = Validator::make([ | ||
'south_african_id' => '240620 3710 097', | ||
'birth_date' => '1925-06-20', | ||
], [ | ||
'birth_date' => [new BirthDateMatchSouthAfricanId], | ||
]); | ||
|
||
$this->assertTrue($validator->fails()); | ||
} | ||
} |