Wallogit.com
2017 © Pedro Peláez
A simple wrapper for abstracting the system date
A simple clock abstraction library for PHP., (*1)
The problem with DateTime instances is that they are not testable and mockable. They always rely on the system date and time. Therefore you should never directly create DateTime instances., (*2)
With the Clock interface you are able to switch implementations and therefore have control over the returned DateTime instances., (*3)
Install it using Composer., (*4)
composer require intriro/clock
The Clock object provides access to the current date and time. Basically it is just a object which returns a
DateTimeImmutable instance that represents the current date and time., (*5)
With a Clock interface you can always swap implementations and get DateTimeImmutable instances which don't rely on the system time., (*6)
The library ships with a Clock interface and three implementations., (*7)
SystemClock gives you access to the system date and time. It just returns new DateTimeImmutable('now') and it's the
default implementation., (*8)
ShiftedSystemClock returns the system date and time that is shifted for a provided interval., (*9)
FixedClock returns a predefined fixed date and time., (*10)
class Event { /** * @var DateTimeImmutable */ private $date; public function hasPassed(Clock $clock): bool { if ($this->clock->now() > $this->date) { return true; } return false; } }