定制 intriro/clock 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

intriro/clock

最新稳定版本:2.0.0

Composer 安装命令:

composer require intriro/clock

包简介

A simple wrapper for abstracting the system date

README 文档

README

A simple clock abstraction library for PHP.

Motivation

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.

With the Clock interface you are able to switch implementations and therefore have control over the returned DateTime instances.

Installation

Install it using Composer.

composer require intriro/clock

Implementation

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.

With a Clock interface you can always swap implementations and get DateTimeImmutable instances which don't rely on the system time.

The library ships with a Clock interface and three implementations.

SystemClock gives you access to the system date and time. It just returns new DateTimeImmutable('now') and it's the default implementation.

ShiftedSystemClock returns the system date and time that is shifted for a provided interval.

FixedClock returns a predefined fixed date and time.

Example

class Event
{
    /**
     * @var DateTimeImmutable
     */
    private $date;

    public function hasPassed(Clock $clock): bool
    {
        if ($this->clock->now() > $this->date) {
            return true;
        }
        
        return false;
    }
}

统计信息

  • 总下载量: 8.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-20