alecrabbit/php-accessories

PHP Accessories: Classes etc.

Fund package maintenance!
Patreon

0.14.0 2019-10-10 09:15 UTC

README

PHP Version Build Status Appveyor Status Scrutinizer Code Quality Code Coverage Total Downloads

Latest Stable Version Latest Stable Version Latest Unstable Version

License Average time to resolve an issue Percentage of issues still open

Installation

composer require alecrabbit/php-accessories

Usage

for details see examples

Features

Caller::class

Gets a caller Class::method() or function() or Undefined

$caller = Caller::get() // object(AlecRabbit\Accessories\Caller\CallerData)

Note: CallerData::class can be casted to string

if($wrongArguments) {
 throw new \RuntimeException(Caller::get() . ' provided wrong arguments'); 
}

You can set your custom formatter for string casting:

$formatter = new CustomFormatter($options);
Caller::setFormatter($formatter);

Note: CustomFormatter::class should implement CallerDataFormatterInterface

Circular::class

Helper class to get values in a circle

$c = new Circular([1, 2, 3]);
$value = $c(); // int(1) invoke 
$value = $c->value(); // int(2) get value by method
... 
$c(); // int(3)
...
$c(); // int(1)

Note: Circular::__construct can accept array, Rewindable or callable which returns \Generator

Rewindable::class

Rewindable generator helper class

$r = new Rewindable($genFunc); // $genFunc is a callable and returns \Generator
iterator_to_array($r);
$r->rewind();
G::class

Contains generator functions

$r = G::range(1, 3);  // object(Generator)
R::class

Contains rewindable generator functions

$r = R::range(1, 3); // object(AlecRabbit\Accessories\Rewindable)
iterator_to_array($r);
$r->rewind();
Pretty::class

String formatter, e.g. percent, bytes and time(seconds, microseconds, nanoseconds)

Pretty::bytes(10584760, 'mb'); // string(7) "10.09MB"
Pretty::time(0.214); // string(5) "214ms"
Pretty::percent(0.214);  // string(6) "21.40%"

Pretty::seconds(0.214); // string(5) "214ms"

Pretty::milliseconds(214); // string(5) "214ms"

Pretty::useconds(3212); // string(5) "3.2ms"
Pretty::useconds(12); // string(5) "12μs"
// alias for useconds
Pretty::microseconds(12); // string(5) "12μs"

Pretty::nanoseconds(10485); // string(7) "10.5μs"
Pretty::nanoseconds(105); // string(7) "105ns"

Note: time formatting methods of Pretty::class named by units they are accepting

MemoryUsage::class

Helper class to get memory usage

$report = MemoryUsage::reportStatic('mb');
echo $report . PHP_EOL;
// Memory: 0.75MB(32.73MB) Real: 2.00MB(34.00MB)

You can set your custom formatter for string casting:

$formatter = new CustomFormatter($options);
MemoryUsage::setFormatter($formatter);

Note: CustomFormatter::class should implement MemoryUsageReportFormatterInterface

Note: Parameter $options has no effect on MemoryUsageReportFormatter