petrenkoanton/php-collection

自定义集合实现

v1.1.2 2024-01-26 19:55 UTC

This package is auto-updated.

Last update: 2024-09-26 21:34:55 UTC


README

PHP Version Latest Version on Packagist Total Downloads License

PHP Composer Coverage Status type-coverage psalm-level Build Status

安装 | 功能 | 使用 | 开发者 | 许可 | 相关项目

安装

要求

  • php 8.0 或更高版本

Composer

composer require petrenkoanton/php-collection

功能

公共方法

集合

异常

主库异常是 CollectionException

使用

<?php

declare(strict_types=1);

use Collection\Arrayable;
use Collection\Collectable;
use Collection\Collection;

// All collection items must implements `Collection\Collectable` interface
interface EntityInterface extends Collectable
{
}

class Entity implements Arrayable, EntityInterface
{
    public function __construct(private int $id)
    {
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function toArray(): array
    {
        return ['id' => $this->id];
    }
}

class EntityInterfaceCollection extends Collection
{
    public function __construct(EntityInterface ...$items)
    {
        parent::__construct(...$items); // Mandatory call of the parent constructor
    }
}

$firstEntity = new Entity(1);
$secondEntity = new Entity(2);

$collection = new EntityInterfaceCollection($firstEntity);
$collection->add($secondEntity);

$firstEntityId = $collection->first()->getId(); // 1

$count = $collection->count(); // 2

$collectionAsArray = $collection->toArray() // [['id' => 1], ['id' => 2]];

开发者

要求

实用工具

设置

初始化

创建 ./docker/.env

make init 

使用不同版本的 PHP 构建容器

php 8.0

make up80

php 8.1

make up81

php 8.2

make up82

php 8.3

make up83

在构建具有其他 PHP 版本的容器之前,还需要运行此命令。这将删除网络和之前创建的容器。

make down

其他命令

进入容器

make inside

检查 PHP 版本

make php-v

检查包版本

make v

运行测试和代码检查器

运行 PHPUnit 测试并生成覆盖率报告

make test-c 

运行 Psalm

make psalm

运行 PHP_CodeSniffer

make phpcs

或者从容器内部运行一揽子命令

composer check-all

许可

php-collection 库是开源软件,许可协议为 MIT 许可

相关项目