tandiljuan/collection

PHP 库,用于创建自定义集合

1.0.0 2019-01-06 19:12 UTC

This package is auto-updated.

Last update: 2024-09-07 07:40:36 UTC


README

PHP 库,用于创建自定义集合。

使用方法

定义一个自定义集合,继承库的抽象集合并定义项目和偏移类型

<?php

class MyCustomCollection extends \Tandiljuan\Collection\AbstractCollection
{
    /**
     * {@inheritdoc}
     */
    protected $itemType = '\DateTime';

    /**
     * {@inheritdoc}
     */
    protected $offsetType = 'integer';
}

创建一个只允许您定义的项目和偏移类型的集合

<?php

$collection = new MyCustomCollection();

try {
    $collection[] = 'error';
} catch (\Tandiljuan\Collection\Exception\InvalidItemType $e) {
    echo "Invalid item type\n";
}

try {
    $collection['a'] = new \DateTime();
} catch (\Tandiljuan\Collection\Exception\InvalidOffsetType $e) {
    echo "Invalid offset type\n";
}

$collection[] = new DateTime('2001-01-01 01:01:01');
$collection[] = new DateTime('2002-02-02 02:02:02');
$collection[] = new DateTime('2003-03-03 03:03:03');

使用 类型声明 需要 MyCustomCollection 参数

<?php

function printItems(MyCustomCollection $parameter)
{
    foreach ($parameter as $item) {
        echo $item->format('r')."\n";
    }
}

printItems($collection);

// The output will be
// Mon, 01 Jan 2001 01:01:01 +0000
// Sat, 02 Feb 2002 02:02:02 +0000
// Mon, 03 Mar 2003 03:03:03 +0000

开发

贡献必须遵循 Symfony 编码标准

建议阅读以下文档

run bash 脚本是 PHP docker 容器的包装器。它将启动容器并在其中运行给定的命令。容器应包含所有必需的开发工具。如果缺少任何工具,请通知项目维护者。

要在 composer.json 文件中添加新包,请使用 composer require

./run composer require [--dev] [vendor/package:version]

要安装 composer.json 文件中定义的包,请使用 composer install

./run composer install

要检查 PHP 代码是否满足编码标准,请使用 php-cs-fixer

# Check if a file needs to be fixed
./run php-cs-fixer fix --dry-run --diff path/to/file.php
# Fix a file
./run php-cs-fixer fix path/to/file.php

要运行 PHP REPL 交互式外壳,请使用 phppsysh

# Using PHP
./run php -a
# Using psysh
./run psysh

测试

引导 Codeception 测试环境。这必须只做一次。

$ # Bootstrap codeception without the standard suites
$ ./run codecept bootstrap --empty --namespace 'Tandiljuan\Collection\Tests'

创建一个 codeception 测试套件。这必须对每个套件只做一次。

$ # Create `unit` suit (for unit testing)
$ ./run codecept generate:suite unit

在给定的套件下创建一个 Cest 类。可以根据需要创建尽可能多的 Cest 类。

$ # Create a new cest `Dummy` under the `unit` suit
$ ./run codecept generate:cest unit Dummy

编写测试后,可以使用 run 命令执行它们

$ ./run codecept run