apantle/fun-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

apantle/fun-php

Composer 安装命令:

composer require apantle/fun-php

包简介

Functional Programming to enhance OOP PHP developer experience

README 文档

README

Functional Programming to enhance OOP PHP developer experience

Build Status Maintainability Test Coverage

Here you have the supported functions with a minimal example, there are more examples in the tests of the package:

compose

applies a par of functions, first the one to the right, then to the returned value applies the function to the left:

Example:

$stringSquaredRoot = compose('strval', 'sqrt');

var_dump('4' === $stringSquaredRoot(16)); // bool(true) 

pipe

Returns a function that receives any number of callables, first one is passed an arbitrary number of arguments, then it passes the returned value to next one in the pipe, and so on. Next functions must be unary (only accept one argument).

Example with a simplified functional controller:

$getFoldersController = pipe(
    'validateHasUser',
    'validatePermissionOfUser',
    'getRootFolders'
);

function validateHasUser($request = null)
{
    if (!array_key_exists($request, 'user')) {
        throw new \LogicException('User parameter not received');
    }
    return $request;
}

function validatePermissionOfUser($request)
{
    if (intval($request['user']) !== 1) {
        throw new \DomainException('Only root user has permissions on this route');
    }
    return $request;
}

function getRootFolders($request) {
    return [ 'folder-' . $request['user'] ];
}

// $user key absent of request:
$getFoldersController(['path' => 'whatever']); // throws LogicException

// $user key present, non root user
$getFoldersController(['user' => 20]); // throws DomainException

// $user is root
$getFoldersController(['user' => 1]); // returns [ 'folder-1' ];

curryToUnary

Allow to partially apply any callable, returning an unary function. Most useful in combination with pipe to write point-free style algorithms.

Passing in the list of arguments to bound, the constant Apantle\FunPHP\_ the position of the argument expected can be swapped. For example, you can build a Mappable collection with array_map and the array to be mapped, and pass to this collection a different mapper every time.

Examples:

$simpleArrayReverse = curryToUnary('array_reverse');

var_dump([4,3,2,1] === $simpleArrayReverse([1,2,3,4]); // bool(true)

// custom position of argument to receive:
$map = curryToUnary('array_map', \Apantle\FunPHP\_, [1, 2, 3, 4]);

$result = $map(function ($num) { return $num * 10; });

var_dump([10, 20, 30, 40] === $result); // bool(true)

constant

Returns a function that always return the value passed, useful for placeholders or composition with constant values but to avoid hardcoding values:

$request = [
    'store_id' => filter_input(
        INPUT_POST,
        'store',
        VALIDATE_FILTER_INT
    ) 
];

$scope = constant($request['store_id']);
$scope() // always return the value received in $_POST['store']

identity

useful for functor tests, always returns the passed value

head

gets the first item of an array

var_dump(1 === head[1,2,3,4]); // bool(true)

unfold

Returns a function that takes a single value, applies an array of transformations to it, and returns a an associative array with the same keys as the array of specs, with the value mapped by those functions.

Useful to take a single input and pass it to several services, then collecting the output in a single hashmap. A perfect dual to apantle/hashmapper.

Example:

$input = 4;

var_dump(json_encode(unfold([
    'sqrt' => 'sqrt',
    'factorial' => 'gmp_fact'
])($input)));
// string(25) "{"sqrt":2,"factorial":24}"

If the function is unary, it passes only the input value.

In order to composer more complex algorithms easily, the mappers can receive a second argument, that apantle/hashmapper passes to every mapper automatically, being that the whole associative array being mapped.

Apart from the spec of transformations, it takes an optional argument that allows you to pass values through the mappers, it is recommended this argument to be an object to be passed by reference.

See the test sources for more examples.

apantle/fun-php 适用场景与选型建议

apantle/fun-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 725 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 08 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 apantle/fun-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 apantle/fun-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 apantle/fun-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-23