This package is intended to make it easier to specify time and size in configuration files more clearly.
// Before
$query->where('size', '>=', 209715200)->get();
// After
$query->where('size', '>=', Size::mb(200))->get();
// Before
$config = [
'expiration' => 4680, // 3 days and 6 hours in minutes
];
// After
$config = [
'expiration' => Time::of(days: 3, hours: 6, 'minutes'),
// or
'expiration' => Time::of(days: 3.25, 'minutes'),
// or
'expiration' => Time::days(3.25, 'minutes'),
];
// Before
class Job
{
protected int $timeout = 10800;
}
// After
class Job
{
protected int $timeout = Time::HOUR * 3;
}
- PHP 8.0 and above
Require this package with composer using the following command:
composer require bbprojectnet/unit-helpers
As simple static call:
$timeout = Time::hours(4);
Static call with output unit specified:
$timeout = Time::hours(4, 'minutes');
As method parameter:
$timeout = Time::of(days: 2);
With mixed unit types:
$timeout = Time::of(days: 2, hours: 10);
With mixed unit types and output unit specified:
$timeout = Time::of(days: 2, hours: 10, as: 'minutes');
With mixed unit types, fractions, negative values and output unit specified:
$timeout = Time::of(days: 2.4, hours: -2, minutes: 0.5, as: 'minutes');
As constant in places where method invocation is not possible:
protected int $timeout = Time::HOUR * 3;
The Unit helpers package is open-sourced software licensed under the MIT license.