2017 © Pedro PelĂĄez
 

library clock

Yet another clock abstraction

image

lcobucci/clock

Yet another clock abstraction

  • Thursday, March 8, 2018
  • by lcobucci
  • Repository
  • 3 Watchers
  • 43 Stars
  • 43,642 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 24 % Grown

The README.md

Clock

![Total Downloads] ![Latest Stable Version] ![Unstable Version], (*1)

![Build Status] ![Code Coverage], (*2)

Yet another clock abstraction..., (*3)

The purpose is to decouple projects from DateTimeImmutable instantiation so that we can test things properly., (*4)

Installation

Package is available on Packagist, you can install it using Composer., (*5)

composer require lcobucci/clock

Usage

Make your objects depend on the Lcobucci\Clock\Clock interface and use SystemClock or FrozenClock to retrieve the current time or a specific time (for testing), respectively:, (*6)

<?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

The Versions

08/03 2018

dev-master

9999999-dev

Yet another clock abstraction

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by LuĂ­s Cobucci

19/01 2018

1.1.0

1.1.0.0

Yet another clock abstraction

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by LuĂ­s Cobucci

26/05 2017

dev-temporarily-allow-ancient-php

dev-temporarily-allow-ancient-php

Yet another clock abstraction

  Sources   Download

MIT

The Requires

  • php ^5.6|^7.1

 

The Development Requires

by LuĂ­s Cobucci

07/05 2017

1.0.0

1.0.0.0

Yet another clock abstraction

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by LuĂ­s Cobucci