lcobucci / clock
又一个时钟抽象
3.2.0
2023-11-17 17:00 UTC
Requires
- php: ~8.2.0 || ~8.3.0
- psr/clock: ^1.0
Requires (Dev)
- infection/infection: ^0.27
- lcobucci/coding-standard: ^11.0.0
- phpstan/extension-installer: ^1.3.1
- phpstan/phpstan: ^1.10.25
- phpstan/phpstan-deprecation-rules: ^1.1.3
- phpstan/phpstan-phpunit: ^1.3.13
- phpstan/phpstan-strict-rules: ^1.5.1
- phpunit/phpunit: ^10.2.3
Provides
This package is auto-updated.
Last update: 2024-09-20 19:05:33 UTC
README
又一个时钟抽象...
目的是将项目从 DateTimeImmutable
实例化中解耦,以便我们可以正确地进行测试。
安装
此包可在 Packagist 上找到,您可以使用 Composer 进行安装。
composer require lcobucci/clock
用法
使您的对象依赖于 Lcobucci\Clock\Clock
接口,并分别使用 SystemClock
或 FrozenClock
获取当前时间或特定时间(用于测试)。
<?php use Lcobucci\Clock\Clock; use Lcobucci\Clock\SystemClock; use Lcobucci\Clock\FrozenClock; function filterData(Clock $clock, array $objects): array { return array_filter( $objects, static function (stdClass $object) use ($clock): bool { return $object->expiresAt > $clock->now(); } ); } // Object that will return the current time based on the given timezone // $clock = SystemClock::fromSystemTimezone(); // $clock = SystemClock::fromUTC(); $clock = new SystemClock(new DateTimeZone('America/Sao_Paulo')); // Test object that always returns a fixed time object $clock = new FrozenClock( new DateTimeImmutable('2017-05-07 18:49:30') ); // Or creating a frozen clock from the current time on UTC // $clock = FrozenClock::fromUTC(); $objects = [ (object) ['expiresAt' => new DateTimeImmutable('2017-12-31 23:59:59')], (object) ['expiresAt' => new DateTimeImmutable('2017-06-30 23:59:59')], (object) ['expiresAt' => new DateTimeImmutable('2017-01-30 23:59:59')], ]; var_dump(filterData($clock, $objects)); // last item will be filtered