chrisharrison/date-validator
Composer 安装命令:
composer require chrisharrison/date-validator
包简介
Historical date validator.
README 文档
README
Installation
I've added this to Packagist, so you can use:
composer require chrisharrison/date-validator
Alternatively, you can clone the repository and run:
composer install
Running tests
Run tests with:
vendor/bin/phpunit
Usage
You can inject the date validator like this:
final class SomeOtherFunctionality
{
private $dateValidator;
public function __construct(DateValidator $dateValidator)
{
$this->dateValidator = $dateValidator;
}
public function doSomethingWithDate(string $date)
{
if ($this->dateValidator->validate()->isValid()) {
return 'A valid historical date';
}
}
}
This package comes with a default implementation. The DefaultDateValidator requires a Clock in order for it to compare dates. A DefaultDateValidator should be initialised within an IoC container. It can be constructed like this:
$dateValidator = new DefaultDateValidator(new SystemClock);
The validate() method returns a DateValidatorResponse value object with the following methods:
isValid(): boolWhether the date is a valid historical date or not.getMessage(): DateValidatorResponseMessageReturns an enum with the following options:INVALID_DATEThe input date is not a valid date (e.g. 32/13/1994)NOT_A_DATE_IN_THE_PASTThe input date is valid but it's not in the past (e.g. 01/01/3033)INVALID_FORMATThe input is not a date at all (e.g. 'jibberish')- If the date is valid then the message is a null version of
DateValidatorResponseMessage
This package also provides a singleton which can be used like this:
use ChrisHarrison\DateValidator\Singleton\DateValidator;
DateValidator::validateHistoricalDate('03/12/1999');
The singleton uses a DefaultDateValidator internally and validateHistoricalDate() works the same way as above.
My strategy
You can see the way that I approached this task my looking at my commit history.
Note on really historical dates
The solution I've written handles dates back to the year 1 CE. It doesn't handle dates BCE. And bizarrely thinks that there is a year zero. If I was to devote more time to this - and there was a business case for it - I'd investigate that. [OK I couldn't resist looking it up. It seems that PHP is using some implementation of ISO 8601 and not the Gregorian calendar]
Note on PHP date formats
The stipulation that the input date format should be DD/MM/YYYY reminded me of an interesting quirk of certain PHP functions which I discussed in a Stackoverflow answer many years ago. Just a mildly interesting titbit 😄
chrisharrison/date-validator 适用场景与选型建议
chrisharrison/date-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 chrisharrison/date-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chrisharrison/date-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: UNLICENSED
- 更新时间: 2019-09-06