-
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.
Add age validation to the schema (#124)
Check and validate ages from dates in a CSV cell. This addition provides the ability to set age restrictions based on birthdays, offering controls such as minimum, maximum, and specific values for age.
- Loading branch information
Showing
8 changed files
with
205 additions
and
7 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
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
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,85 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Rules\Cell; | ||
|
||
final class ComboDateAge extends AbstractCellRuleCombo | ||
{ | ||
protected const NAME = 'age'; | ||
|
||
protected const INVALID_DATEINTERVAL_ACTUAL = -1; | ||
protected const INVALID_DATEINTERVAL_EXPECTED = -2; | ||
|
||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[ | ||
'Check an arbitrary date in a CSV cell for age (years).', | ||
'Actually it calculates the difference between the date and the current date.', | ||
'Convenient to use for age restrictions based on birthday.', | ||
'See the description of `date_*` functions for details on date formats.', | ||
], | ||
[ | ||
self::MIN => [1, 'x >= 1'], | ||
self::GREATER => [14, 'x > 14'], | ||
self::NOT => [18, 'x != 18'], | ||
self::EQ => [21, 'x == 21'], | ||
self::LESS => [99, 'x < 99'], | ||
self::MAX => [100, 'x <= 100'], | ||
], | ||
]; | ||
} | ||
|
||
protected function getActualCell(string $cellValue): float | ||
{ | ||
try { | ||
$years = self::calculateAge($cellValue); | ||
} catch (\Exception) { | ||
return self::INVALID_DATEINTERVAL_ACTUAL; | ||
} | ||
|
||
return $years; | ||
} | ||
|
||
protected function getExpected(): float | ||
{ | ||
return $this->getOptionAsInt(); | ||
} | ||
|
||
protected function getExpectedStr(): string | ||
{ | ||
return "{$this->getOptionAsInt()} years"; | ||
} | ||
|
||
protected function getCurrentStr(string $cellValue): string | ||
{ | ||
try { | ||
$years = self::calculateAge($cellValue); | ||
} catch (\Exception $exception) { | ||
return "<red>{$exception->getMessage()}</red>"; | ||
} | ||
|
||
return "parsed as \"{$years}\" years"; | ||
} | ||
|
||
private static function calculateAge(string $dateString): int | ||
{ | ||
$birthDateTime = new \DateTimeImmutable($dateString); | ||
$currentDateTime = new \DateTimeImmutable('now'); | ||
|
||
return $birthDateTime->diff($currentDateTime)->y; // Returns the total number of full years | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\PHPUnit\Rules\Cell; | ||
|
||
use JBZoo\CsvBlueprint\Rules\AbstarctRule as Combo; | ||
use JBZoo\CsvBlueprint\Rules\Cell\ComboDateAge; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRuleCombo; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
class ComboDateAgeTest extends TestAbstractCellRuleCombo | ||
{ | ||
protected string $ruleClass = ComboDateAge::class; | ||
|
||
public function testEqual(): void | ||
{ | ||
$rule = $this->create(0, Combo::EQ); | ||
|
||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('now')); | ||
|
||
isSame( | ||
'The age of the value "2020-10-02" is parsed as "3" years, ' . | ||
'which is not equal than the expected "0 years"', | ||
$rule->test('2020-10-02'), | ||
); | ||
|
||
isSame( | ||
'The age of the value "<c>qwerty</c>" is ' . | ||
'<red>Failed to parse time string (qwerty) at position 0 (q): ' . | ||
'The timezone could not be found in the database</red>, ' . | ||
'which is not equal than the expected "<green>0 years</green>"', | ||
$rule->test('qwerty', true), | ||
); | ||
} | ||
|
||
public function testMin(): void | ||
{ | ||
$rule = $this->create(21, Combo::MIN); | ||
|
||
isSame('', $rule->test('')); | ||
isSame('', $rule->test('+22 years')); | ||
isSame('', $rule->test('2100-01-01')); | ||
isSame('', $rule->test('2100-01')); | ||
|
||
isSame( | ||
'The age of the value "2020-10-02" is parsed as "3" years, ' . | ||
'which is less than the expected "21 years"', | ||
$rule->test('2020-10-02'), | ||
); | ||
|
||
isSame( | ||
'The age of the value "<c>qwerty</c>" is ' . | ||
'<red>Failed to parse time string (qwerty) at position 0 (q): ' . | ||
'The timezone could not be found in the database</red>, ' . | ||
'which is less than the expected "<green>21 years</green>"', | ||
$rule->test('qwerty', true), | ||
); | ||
} | ||
} |
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