• Stars
    star
    677
  • Rank 66,694 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Yet another clock abstraction

Clock

Total Downloads Latest Stable Version Unstable Version

Build Status Code Coverage

Yet another clock abstraction...

The purpose is to decouple projects from DateTimeImmutable instantiation so that we can test things properly.

Installation

Package is available on Packagist, you can install it using Composer.

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:

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

More Repositories

1

jwt

A simple library to work with JSON Web Token and JSON Web Signature
PHP
7,247
star
2

error-handling-middleware

A PSR-15 middleware compatible with RFC 7807
PHP
58
star
3

kafka

PHP client for Kafka
PHP
57
star
4

content-negotiation-middleware

A PSR-15 middleware to handle content negotiation
PHP
46
star
5

di-builder

A library that makes easier the creation of compiled containers using Symfony DI
PHP
32
star
6

jose-parsing

A base64Url and JSON encoding/decoding implementation
PHP
26
star
7

action-mapper

Front-controller micro-framework for PHP web applications
PHP
20
star
8

social

Social authentication
PHP
15
star
9

easy-soap

Provides a easy way to create SOAP webservices
PHP
12
star
10

cache-mess

A benchmark with PSR-6 and PSR-16 providers
PHP
9
star
11

chimera-foundation

Foundation for components that help you to create well designed software
PHP
7
star
12

display-objects

Create your PHP UI components fast and easy
PHP
7
star
13

coding-standard

A modified version version of doctrine/coding-standard which I like to use for my projects
PHP
6
star
14

architecture-poc

Just playing around with some stuff
PHP
6
star
15

chimera-sample

A sample project using lcobucc/chimera-* packages
PHP
5
star
16

action-mapper-sample

PHP
5
star
17

chimera-routing

A collection of reusable PSR-15 middlewares that connects chimera-foundation to any framework
PHP
5
star
18

minicurso-tdd

Skeleton para minicurso de TDD
PHP
2
star
19

chimera-routing-expressive

Zend Expressive implementation for lcobucci/chimera-routing
PHP
2
star
20

em-data-fixtures

This is an extension of doctrine/data-fixtures to provide a subscriber and a fixture that uses the ObjectManager instead of ManagerRegistry
PHP
2
star
21

chimera-bus-tactician

Tactician implementation connecting lcobucci/chimera-foundation and lcobucci/chimera-routing
PHP
2
star
22

conference-scheduler

Just a POC of an easier way to arrange talks for conferences
JavaScript
1
star
23

behat-di-builder-extension

A behat extension that allows injecting services from a container created with lcobucci/di-builder in context
PHP
1
star
24

chimera-serialization-jms

Adapter for jms/serializer which allows you to create messages to be handled
PHP
1
star