具有一致性和可预测 API 的 PHP 数组对象
2.0.2
2023-02-21 16:07 UTC
Requires
- php: >=8.1
- thecodingmachine/safe: ^2.4
Requires (Dev)
README
具有一致、可预测和方便 API 的 PHP 数组对象
安装
- 使用 composer 下载
composer require wernerdweight/ra
- 在项目中使用
use WernerDweight\RA\RA; // helper methods (extracted here to simplify the code below and emphasize the difference) function filterFunction(string $god): bool { return false !== strpos($god, 's'); } function mapFunction(string $godContainingTheLetterS): string { return strtoupper($godContainingTheLetterS); } function reduceFunction(string $carry, string $god): string { return $carry .= ($carry[-1] === ' ' ? '' : ', ') . $god; } // create new RA $egyptianGods = new RA(['Ra', 'Osiris', 'Anubis', 'Horus']); // use as object $godsContainingTheLetterSInUppercase = $egyptianGods ->filter('filterFunction') ->map('mapFunction') ->reverse() ->reduce('reduceFunction', 'My favourite Egyptian Gods are '); echo $godsContainingTheLetterSInUppercase . "\n"; // use as normal array $godsContainingTheLetterSInUppercase = array_reduce( array_reverse( array_map( 'mapFunction', array_filter( $egyptianGods->toArray(), 'filterFunction' ) ) ), 'reduceFunction', 'My favourite Egyptian Gods are ' ); echo $godsContainingTheLetterSInUppercase . "\n"; // RA extends Iterator, ArrayAccess and Countable foreach ($egyptianGods as $god) { echo sprintf("My favourite Egyptian God is %s\n", $god); }
API
待办事项