定制 projx-io/fluent 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

projx-io/fluent

Composer 安装命令:

composer require projx-io/fluent

包简介

README 文档

README

Build Status Coverage Status

Fluent

Installation

composer require projx-io/fluent

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.

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.

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-02-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固