2017 © Pedro Peláez
 

library fluent

image

projx-io/fluent

  • Monday, February 29, 2016
  • by projx
  • Repository
  • 0 Watchers
  • 1 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 0 % Grown

The README.md

Build Status Coverage Status, (*1)

Fluent

Installation

composer require projx-io/fluent, (*2)

Hello World

// Requires two parameters.
function greet($greeting, $name) {
    return sprintf("%s, %s!\n", $greeting, $name);
}

// Creates a stream with 'World'.
// $stream is a stream node, which is a callable value.
// This particular node is a 'with' node, and will return
// the value 'World' when the stream is executed.
// If call() is called directly on this node, then 'World'
// will be the result. Otherwise, 'World' will be passed as
// a parameter to the next node.
$stream = Fluent('World');

// Creates a stream node whose callback is 'greet',
// and has a bound parameter of 'Hello'.
// 'Hello' will be provided as the first parameter to `greet`.
// In this particular stream, the value of 'World' returned
// from the previous node will be provided as the second
// parameter to this node.
$stream = $stream->then('greet', 'Hello');

// Executes the stream then prints the returned value of
// "Hello, World!"
echo $stream->call();

Array Map Example

// adds two to a second parameter.
$offset = F::plus(2);
// results in 17
$offset->call(15);

// results in [3, 4, 5, 6, 7]
$items = F([1, 2, 3, 4, 5])
    ->map($offset)
    ->toArray();

Array Filter Example

// adds two to a parameter
$filter = F::plus(2)->moreThan(4);
// results in true, since 3+2 > 4 -> true
$filter->call(3);
// results in false, since 2+2 > 4 -> false
$filter->call(2);

// results in [3, 4, 5]
$items = F([1, 2, 3, 4, 5])
    ->filter($filter)
    ->toArray();

Object Example

$stream = F::object([
    'name' => F()->name,            // get object's name
    'generation' => F::if(          // if/then/else
        F()->age->moreThan(21),     // condition of object's age being more than 21
        F('Old Geezer!'),           // when true
        F('Just a baby!')           // when false
    ),
]);

// Results in {"name":"Steve","generation":"Old Geezer!"}
$stream->call((object)['name'=>'Steve', 'age'=>32]);

// Results in {"name":"Stacey","generation":"Just a baby!"}
$stream->call((object)['name'=>'Stacey', 'age'=>20]);

Array Example

$stream = F::map(
        F::plus(2)->times(-1)
    )[1]
    ->equalTo(-5);

// Results in true, since (3+2)*(-1) -> -5
$stream->call([2, 3, 4]);

// Results in false, since (-3+2)*(-1) -> 1
$stream->call([2, -3, 4]);

If you don't want to use a fluent stream, that's fine too., (*3)

Callback example

// Results in [4, 5, 6]
array_map(Plus::bind(2), [2, 3, 4]);

Registering Methods

Unbound

Fluent::registerMethods([
    'someMethod' => new ConstantCallbackFactory(function () {
        return ...
    }),
]);

Fluent::then(...)->someMethod();

Bound

Fluent::registerMethods([
    'someMethod' => new BindCallbackFactory(new ConstantCallbackFactory(function ($a, $b) {
        return ...
    })),
]);

Fluent::then(...)->someMethod($a, $b);

List of methods

A list of methods can be found in the Fluent class., (*4)

The Versions

29/02 2016

dev-master

9999999-dev

  Sources   Download

The Development Requires

by Terrence Howard

29/02 2016

1.10.2

1.10.2.0

  Sources   Download

The Development Requires

by Terrence Howard

29/02 2016

1.10.1

1.10.1.0

  Sources   Download

The Development Requires

by Terrence Howard

29/02 2016

1.10

1.10.0.0

  Sources   Download

The Development Requires

by Terrence Howard

29/02 2016

1.9

1.9.0.0

  Sources   Download

The Development Requires

by Terrence Howard

29/02 2016

1.8

1.8.0.0

  Sources   Download

The Development Requires

by Terrence Howard

29/02 2016

1.7.1

1.7.1.0

  Sources   Download

The Development Requires

by Terrence Howard

29/02 2016

1.7

1.7.0.0

  Sources   Download

The Development Requires

by Terrence Howard

27/02 2016

1.6

1.6.0.0

  Sources   Download

The Development Requires

by Terrence Howard

27/02 2016

1.5.1

1.5.1.0

  Sources   Download

The Development Requires

by Terrence Howard

26/02 2016

1.5

1.5.0.0

  Sources   Download

The Development Requires

by Terrence Howard

26/02 2016

1.4.1

1.4.1.0

  Sources   Download

The Development Requires

by Terrence Howard

26/02 2016

1.4

1.4.0.0

  Sources   Download

The Development Requires

by Terrence Howard

26/02 2016

1.3

1.3.0.0

  Sources   Download

The Development Requires

by Terrence Howard

26/02 2016

1.2

1.2.0.0

  Sources   Download

The Development Requires

by Terrence Howard

26/02 2016

1.1

1.1.0.0

  Sources   Download

The Development Requires

by Terrence Howard

25/02 2016

1.0

1.0.0.0

  Sources   Download

The Development Requires

by Terrence Howard