dev-master
9999999-dev http://hoa-project.net/The Hoa\Heap library.
BSD-3-Clause
The Requires
The Development Requires
library iterator heap
Wallogit.com
2017 © Pedro Peláez
The Hoa\Heap library.
, (*1)
Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds., (*3)
This library provides a set of advanced Heap can support Scalar, Array, Object or Closure as item and not only Integer, as ordinal does. The order of heap depends of priority parameter., (*5)
Hoa\Heap\Min and Hoa\Heap\Max class interpret priority by comparing items numerically.
But you are free to implement your own class if you want a different sort algorithm., (*6)
The default iteration process do not dequeue the Heap as common usage.
You must use Generator methods top or pop for iterate on with remove item from heap., (*7)
With Composer, to include this library into your
dependencies, you need to require
hoa/heap:, (*8)
$ composer require hoa/heap '~0.0'
For more installation procedures, please read the Source page., (*9)
Considering the library has been installed with Composer, the following commands will run the test suites:, (*10)
$ composer install $ vendor/bin/hoa test:run
For more information, please consult the contributor guide., (*11)
As a quick overview, we propose to see a simple use case with
a Phone number, This phone number must be sent to three methods
in a strict order, Check, Transform, Format., (*12)
Let's assume we don't have access to iteration process. But we can sort in which orders our methods must be called for respect our process., (*13)
In first, we will create our callbacks process., (*14)
require_once dirname(dirname(__DIR__)) . '/vendor/autoload.php';
// First method used to check if phone number is correct.
$check = function($phone) {
if (1 !== preg_match('/^\+?[0-9]+$/', $phone)) {
throw new \Exception('Phone number not conform.');
}
return $phone;
};
// Second method used to convert number into object.
$transform = function($phone) {
return (object)[
'prefix' => '+33',
'country' => 'France',
'number' => $phone,
];
};
// Third method used to display formatted number.
$format = function(\StdClass $phone) {
return $phone->prefix
. ' '
. wordwrap($phone->number, 3, ' ', true)
;
};
Creation of our Heap with minimum priority Ascending ( lower called first )., (*15)
$heap = new \Hoa\Heap\Min();
// Insert the callback method with the priority argument used for order Heap.
$heap->insert($transform, 20);
$heap->insert($check, 10);
$heap->insert($format, 30);
// Show the number of item in Heap.
var_dump(
$heap->count()
);
/**
* Will output:
* int(3)
*/
Then we can iterate on our Heap with assurance of correct call order.
Spread your number into closure and have process mutation expected., (*16)
// Phone number as expected by first callback.
$number = '123001234';
foreach ($heap as $closure) {
try {
// Mutation of number by closure, execute in the priority order expected.
$number = $closure($number);
} catch (\Exception $e) {
break;
}
}
// Finally, we can display our formatted number.
var_dump($number);
/**
* Will output:
* string(15) "+33 123 001 234"
*/
The hack book of
Hoa\heap contains
detailed information about how to use this library and how it works., (*17)
To generate the documentation locally, execute the following commands:, (*18)
$ composer require --dev hoa/devtools $ vendor/bin/hoa devtools:documentation --open
More documentation can be found on the project's website: hoa-project.net., (*19)
There are mainly two ways to get help:, (*20)
#hoaproject
IRC channel,Do you want to contribute? Thanks! A detailed contributor guide explains everything you need to know., (*21)
Hoa is under the New BSD License (BSD-3-Clause). Please, see
LICENSE for details., (*22)
There are no related project registered, Let us know by opening issue if you use it and want be listed!, (*23)
The Hoa\Heap library.
BSD-3-Clause
library iterator heap