bbprojectnet / unit-helpers
Simple unit helpers for configuration purposes.
Installs: 1 420
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.0.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
This package is intended to make it easier to specify time and size in configuration files more clearly.
Example 1:
// Before $query->where('size', '>=', 209715200)->get(); // After $query->where('size', '>=', Size::mb(200))->get();
Example 2:
// 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'), ];
Example 3:
// Before class Job { protected int $timeout = 10800; } // After class Job { protected int $timeout = Time::HOUR * 3; }
Requirements
- PHP 8.0 and above
Installation
Require this package with composer using the following command:
composer require bbprojectnet/unit-helpers
Usage
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;
License
The Unit helpers package is open-sourced software licensed under the MIT license.