-
Notifications
You must be signed in to change notification settings - Fork 2
Add support for date-time string format #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92da800
Support date-time string format
alkallio 93e3d85
Use ValidateResult
alkallio ff69ad6
Added more test cases
alkallio 3e24f42
Fix error messages for formatMinimum and formatMaximum
alkallio f05efc5
Remove unneeded checks
alkallio 47fcb67
Added test with JSON defined validator of type 'string' and format 'd…
alkallio abfe182
Strong types in new test
alkallio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 +1,7 @@ | ||
## 0.11.0 | ||
### Added | ||
- Support for date-time format in type string | ||
|
||
## 0.10.3 | ||
### Fixed | ||
- IntegerValidator unnecessary casting to float causing overflow | ||
- IntegerValidator unnecessary casting to float causing overflow |
This file contains hidden or 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,100 @@ | ||
<?php | ||
|
||
namespace Phramework\Validate\Formats; | ||
|
||
use Phramework\Exceptions\IncorrectParametersException; | ||
use Phramework\Validate\ValidateResult; | ||
|
||
/** | ||
* Class DateTime | ||
* | ||
* @author Alex Kalliontzis <alkallio@gmail.com> | ||
* @internal Helper class for internal use | ||
* @since 0.11.0 | ||
*/ | ||
class DateTime | ||
{ | ||
const REGEX = '/^(?<year>\d{4})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12][0-9]|3[01])' . | ||
'T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)' . | ||
'(Z|(\+|-)([01][0-9]|2[0-3]):([0-5][0-9]))$/i'; | ||
|
||
public function validateFormat( | ||
string $data, | ||
string $type, | ||
\stdClass $formatProperties | ||
): ValidateResult { | ||
$matches = []; | ||
if (!preg_match(self::REGEX, $data, $matches)) { | ||
return new ValidateResult( | ||
$data, | ||
false, | ||
new IncorrectParametersException([ | ||
[ | ||
'type' => $type, | ||
'failure' => 'date-time', | ||
], | ||
]) | ||
); | ||
} | ||
|
||
//Check if date is valid. This is needed because DateTime | ||
//will parse an invalid date like 2019-02-31 | ||
if (!checkdate((int)$matches ['month'], (int) $matches['day'], (int) $matches['year'])) { | ||
return new ValidateResult( | ||
$data, | ||
false, | ||
new IncorrectParametersException([ | ||
[ | ||
'type' => $type, | ||
'failure' => 'date-time', | ||
], | ||
]) | ||
); | ||
} | ||
|
||
$date = (new \DateTime($data))->getTimestamp(); | ||
|
||
if ($formatProperties->formatMinimum !== null) { | ||
$formatMinimum = (new \DateTime( | ||
$formatProperties->formatMinimum | ||
))->getTimestamp(); | ||
|
||
if ($date < $formatMinimum) { | ||
return new ValidateResult( | ||
$data, | ||
false, | ||
new IncorrectParametersException([ | ||
[ | ||
'type' => $type, | ||
'failure' => 'formatMinimum', | ||
] | ||
]) | ||
); | ||
} | ||
} | ||
|
||
if ($formatProperties->formatMaximum !== null) { | ||
$formatMaximum = (new \DateTime( | ||
$formatProperties->formatMaximum | ||
))->getTimestamp(); | ||
|
||
if ($date > $formatMaximum) { | ||
return new ValidateResult( | ||
$data, | ||
false, | ||
new IncorrectParametersException([ | ||
[ | ||
'type' => $type, | ||
'failure' => 'formatMaximum', | ||
] | ||
]) | ||
); | ||
} | ||
} | ||
|
||
return new ValidateResult( | ||
$data, | ||
true | ||
); | ||
} | ||
} |
This file contains hidden or 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,44 @@ | ||
<?php | ||
|
||
namespace Phramework\Validate\Formats; | ||
|
||
use Phramework\Validate\IFormatValidator; | ||
use Phramework\Validate\ValidateResult; | ||
|
||
/** | ||
* Validates that a property of type 'string' and format | ||
* 'date-time' is an RFC3339 formatted string | ||
* | ||
* @author Alex Kalliontzis <alkallio@gmail.com> | ||
* @since 0.11.0 | ||
*/ | ||
class StringFormatValidatorValidator implements IFormatValidator | ||
{ | ||
protected static $type = 'string'; | ||
|
||
public function validateFormat( | ||
string $data, | ||
string $format, | ||
\stdClass $formatProperties = null | ||
): ValidateResult { | ||
$validateResult = new ValidateResult( | ||
$data, | ||
true | ||
); | ||
|
||
switch ($format) { | ||
case 'date-time': | ||
//Todo Create a TDO for formatProperties | ||
$validateResult = (new DateTime()) | ||
->validateFormat( | ||
$data, | ||
self::$type, | ||
$formatProperties | ||
); | ||
|
||
break; | ||
} | ||
|
||
return $validateResult; | ||
} | ||
} |
This file contains hidden or 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,18 @@ | ||
<?php | ||
|
||
namespace Phramework\Validate; | ||
|
||
/** | ||
* Interface IFormatValidator | ||
* | ||
* @author Alex Kalliontzis <alkallio@gmail.com> | ||
* @since 0.11.0 | ||
*/ | ||
interface IFormatValidator | ||
{ | ||
public function validateFormat( | ||
string $data, | ||
string $format, | ||
\stdClass $formatProperties = null | ||
): ValidateResult; | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.