|
|
6 lat temu | |
|---|---|---|
| .. | ||
| src | 6 lat temu | |
| test | 6 lat temu | |
| .scrutinizer.yml | 6 lat temu | |
| .travis.yml | 6 lat temu | |
| LICENSE | 6 lat temu | |
| README.md | 6 lat temu | |
| composer.json | 6 lat temu | |
| phpunit.xml.dist | 6 lat temu | |
Yet another clock abstraction...
The purpose is to decouple projects from DateTimeImmutable instantiation so that
we can test things properly.
Package is available on Packagist, you can install it using Composer.
composer require lcobucci/clock
Simply create an make your objects dependent on the
interface Lcobucci\Clock\Clock and use SystemClock or
FrozenClock to retrieve the current time:
<?php
use Lcobucci\Clock\Clock;
use Lcobucci\Clock\SystemClock;
use Lcobucci\Clock\FrozenClock;
function filterData(Clock $clock, array $objects): array
{
return array_filter(
$objects,
function (stdClass $object) use ($clock): bool {
return $object->expiresAt > $clock->now();
}
);
}
// Object that will return the current time based on the given timezone
$clock = new SystemClock(new DateTimeZone('UTC'));
// Test object that always returns a fixed time object
$clock = new FrozenClock(
new DateTimeImmutable('2017-05-07 18:49:30')
);
$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