Hurl is a data transformation framework, designed to compose complex transformations.
You will need composer require sysvyz/hurl
Hurl is designed to build algorithms as datastructures. Each algorithm is represented by a tree. A tree is represented by its root node. Trees should be stateless (and immutable) in order to reuse them.
A Node
represents a data transformation.
A Node is basically wrapper for function
s or Closure
s if you like.
Like functions Nodes have inputs(parameters) and a output.
$fromHex = Node::call(function ($data) {
return hexdec($data);
});
var_dump($fromHex('a'));
//int(10)
Nodes are transformation rules. There are several build-in php functions wrapped as Node
$explode = Node::explode('.');
var_dump($explode('a.b'));
//array(2) {
// [0]=>
// string(1) "a"
// [1]=>
// string(1) "b"
//}
Nodes can be chained to perform multiple consecutive transformations
$chain = $explode->implode('-');
var_dump($chain('a.b'));
//string(3) "a-b"
One of the most common transformation is array_map
. Node provides a convenient way of performing those operations.
Since the callback
function of array_map
is nothing else than a transformation, it's obvious to use Nodes as callbacks.
$map = $explode->map($fromHex)->implode('.');
var_dump($map('a.b'));
//string(5) "10.11"
$sort = Node::ARRAY()->sort(function ($a,$b){
return $a-$b;
});
var_dump($sort([2,5,3,4,1]));
//array(5) {
// [0]=>
// int(1)
// [1]=>
// int(2)
// [2]=>
// int(3)
// [3]=>
// int(4)
// [4]=>
// int(5)
//}