承接 mezon/functional 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

mezon/functional

最新稳定版本:1.1.0

Composer 安装命令:

composer require mezon/functional

包简介

Small and fast framework for functional programming

README 文档

README

Build Status codecov

Intro

This class provides various members and tools for functional programming. It will help you to work with arrays in a very simple way.

Modes

Here we can fetch specified field from all objects of array:

$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , $obj2 , $obj3 );

// will display array( 1 , 2 ,3 )
var_dump( \Mezon\Functional\Fetcher::getFields( $Data , 'foo' ) );

We can also set fields with multyple values:

$Values = array( 1 , 2 , 3 );
$obj1 = new stdClass();
$obj2 = new stdClass();

$Data = array( $obj1 , $obj2 );

Functional::setFieldsInObjects( $Data , 'foo' , $Values );
// will display 3 objects
var_dump( $Data );

And fianlly we can sum specified fields:

$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , $obj2 , $obj3 );

// will display value 6
var_dump( Functional::sumFields( $Data , 'foo' ) );

Note that you can recursively walk along the nested arrays:

$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , array( $obj2 , $obj3 ) );

// will display value 6
var_dump(Functional::sumFields( $Data , 'foo' ));

And this code will also work:

// will display value 3
var_dump(Functional::sumFields( [
    ['foo'=>1],
    ['foo'=>2]
] , 'foo' ));

Transformations

We can also transform objects in arrays like this (the most basic and simple way):

/**
*   Transformation function multiplies 'foo' field.
*/
function  transform2x( $Object )
{
    $Object->foo *= 2;

    return( $Object );
}
$obj1 = new stdClass();
$obj1->foo = 1;

$obj2 = new stdClass();
$obj2->foo = 2;

$obj3 = new stdClass();
$obj3->foo = 3;

$Data = array( $obj1 , $obj2 , $obj3 );

Functional::transform( $Data , 'transform2x' );
// will display 3 objects
// with 2, 4 and 6 values in their 'foo' fields
var_dump( $Data );

But if you need more complex transformations, you can use class Transform. It will allow you to build entirely new array.

$data = [
	1 , 2
];

Transform::convert($data,function($item){return [10*$item, 100*$item];});

var_dump($data);

// will output
// [10=>100 , 20=>200]

And if you want to transform only elements of the array, then use Transform::convertElements

$data = [
	1 , 2
];

Transform::convertElements($data,function($item){return 10 * $item;});

var_dump($data);

// will output
// [0=>10 , 1=>20]

统计信息

  • 总下载量: 12.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-13