elephox/pie

该软件包已被放弃且不再维护。作者建议使用 elephox/collection 软件包。

Elephox PIE 库。

dev-main 2022-01-11 00:09 UTC

This package is auto-updated.

Last update: 2022-01-19 01:57:35 UTC


README

这个库(或者更确切地说,模块)受到了C# LINQ 库的启发。

然而,它不是一个功能完整的 LINQ 库。由于 PHP 无法完全支持所有语法糖,它只提供了功能的一个小子集。例如,LINQ 的主要功能之一,即直接在源代码中使用类似 SQL 的语法,是不支持的,因为这需要您编译/转换代码。

不过,主要思想是提供一种更自然的方式来遍历对象集合,就像在 C# 中使用 IEnumerable 一样。

示例

<?php
declare(strict_types=1);

use Elephox\PIE\PIE;

$array = [5, 2, 1, 4, 3];
$pie = PIE::from($array);

$pie->orderBy(fn (int $item) => $item)
    ->select(function (int $item) {
      echo $item;
    });

// output: 12345


$pie->where(fn (int $item) => $item % 2 === 0)
    ->select(function (int $item) {
        echo $item;
    });

// output: 24