/cache-duration

A readable and fluent way to generate PHP cache time.

Primary LanguagePHPMIT LicenseMIT

PHP Cache Duration

Banner

Introduction

A readable and fluent way to generate PHP cache time.

Built and written by Ajimoti Ibukun

Quick Samples

Instead of this:

$cacheDuration = 25 * 60 * 60; // twenty five hours
Redis::expire($userData, $cacheDuration);

You can do this

$cacheDuration = Duration::twentyFiveHours(); // returns 25 hours in seconds (25 * 60 * 60)
Redis::expire($userData, $cacheDuration);

// or
$cacheDuration = Duration::hours(25); // returns 25 hours in seconds (25 * 60 * 60)
Redis::expire($userData, $cacheDuration);

You can also do this:

$cacheDuration = Duration::at('first day of January 2023'); // returns the time difference between the present time and the first of january 2023 in seconds

Redis::expire($userData, $cacheDuration);

Requirements

  • PHP 8.0 or higher

Installation

You can install the package via composer:

composer require ajimoti/cache-duration --with-all-dependencies

Documentation

After installing the package via composer, import the Duration trait inside your class, then you are set.

<?php
require 'vendor/autoload.php';

use Ajimoti\CacheDuration\Duration;

var_dump(Duration::fourtyMinutes()); // returns 2400;
var_dump(Duration::tenHours()); // returns 36000;
var_dump(Duration::fiftyFourDays()); // returns 4665600;

Available methods

Method Expectations
seconds($value) Expects time in seconds
minutes($value) Expects time in minutes
hours($value) Expects time in hours
days($value) Expects time in days
at($value) Expects string, carbon instance, or DateTime instance

Dynamic calls

In addition to the methods provided above, the package uses PHP __callStatic() method to allow you make dynamic calls on the Duration trait.

For example, you want to get the number of seconds in 37 days, you can achieve this by calling a camel-case text of the number (thirtySeven in this case), plus the unit (Days in this case). That will leave us with something like this:

// The formula = camelCaseOfTheNumberInWords + Unit
Duration::thirtySevenDays(); // returns the number of seconds in 37 days

Note: The number in words MUST be in camel-case. Any other case will throw an InvalidArgumentException. Additionally, it must be followed by a title-case of the unit. The available units are Seconds, Minutes, Hours, and Days.

Usage

seconds($value)

Get time in seconds. It basically returns the same value passed into it.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::seconds(30); // returns 30

// or dynamically
$cacheDuration = Duration::thirtySeconds(); // returns 30

minutes($value)

Converts time in minutes into seconds.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::minutes(55); // returns 55 minutes in seconds (55 * 60)

// or dynamically
$cacheDuration = Duration::fiftyFiveMinutes(); // returns 55 minutes in seconds (55 * 60)

hours($value)

Converts time in hours into seconds.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::hours(7); // returns 7 hours in seconds (7 * 60 * 60)

// or dynamically
$cacheDuration = Duration::sevenHours(); // returns 7 hours in seconds (7 * 60 * 60)

days($value)

Converts time in days into seconds.

use Ajimoti\CacheDuration\Duration;

$cacheDuration = Duration::days(22); // returns 22 days in seconds (22 * 24 * 60 * 60)

// or dynamically
$cacheDuration = Duration::twentyTwoDays(); // returns 22 days in seconds (22 * 24 * 60 * 60)

at($value)

This method allows you to convert a Carbon\Carbon instance, DateTime instance or string of date into seconds.

It returns the difference in seconds between the argument passed and the current timestamp.

The date passed into this method MUST be a date in the future. When a string is passed, the text MUST be compatible with Carbon::parse() method, else an exception will be thrown

Examples

use Date;
use Carbon\Carbon;
use Ajimoti\CacheDuration\Duration;

// Carbon instance
$cacheDuration = Duration::at(Carbon::now()->addMonths(3)); // returns time in seconds between the present timestamp and three months time

// Datetime instance
$cacheDuration = Duration::at(new DateTime('2039-09-30')); // returns time in seconds between the present timestamp and the date passed (2039-09-30).

// String
$cacheDuration = Duration::at('first day of January 2023'); // returns time in seconds between the present timestamp and the first of January 2023.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.