承接 goksagun/collection 相关项目开发

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

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

goksagun/collection

最新稳定版本:v0.2.8

Composer 安装命令:

composer require goksagun/collection

包简介

A PHP collection library providing functionalities for managing and manipulating collections.

README 文档

README

A PHP collection library providing functionalities for managing and manipulating product collections.

Features

  • Create a collection of items
  • Add a item to the collection
  • Map over the collection to apply a function to each item
  • Filter the collection based on a predicate
  • Pluck a specific property from each item in the collection
  • Reduce the collection to a single value
  • Sort the collection based on a custom comparison function

Installation

Use Composer to install this library:

composer require goksagun/collection

Usage

Here's a basic example of how to use this library:

<?php

namespace Acme;

class Product
{
    public function __construct (
        private string $name,
        private float $price
    ) {}

    public function getName(): string
    {
        return $this->name;
    }

    public function getPrice(): float
    {
        return $this->price;
    }

}
<?php

namespace Acme;

use Acme\Product;
use Goksagun\Collection\Collection;

/**
 * @implements Collection<Product>
 */
final class ProductCollection extends Collection
{

    public function __construct(Product ...$items)
    {
        parent::__construct(...$items);
    }

}
<?php

namespace Acme;

use Acme\Product;
use Acme\ProductCollection;

$collection = new ProductCollection(
    new Product('Product 1', 100.99),
    new Product('Product 2', 200.99),
    new Product('Product 3', 300.99),
);

$total = $collection
    ->map(function (Product $product) {
        return new Product($product->getName(), $product->getPrice() * 1.18);
    })
    ->each(function (\Goksagun\Collection\Test\Fixtures\Product $product, int $index) {
        echo "Product {$index}: {$product->getName()} - {$product->getPrice()}\n";
    })
    ->filter(function (Product $product) {
        return $product->getPrice() > 300;
    })
    ->pluck('price')
    ->reduce(function (float $total, float $price) {
        return $total + $price;
    }, 0);

echo "Total: {$total}\n";

// Product 0: Product 1 - 119.1682
// Product 1: Product 2 - 237.1682
// Product 2: Product 3 - 355.1682
// Product 0: Product 3 - 355.1682
// Total: 355.1682

Testing

Run the tests with:

composer test

License

This project is licensed under the MIT License - see the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固