shahmal1yev/gcollection

本包最新版本(1.0.7)没有提供许可信息。

GenericCollection 是一个通用的 PHP 库,提供类型安全的集合类来管理各种数据类型。通过直观的方法和强类型约束简化数据管理。

1.0.7 2024-09-30 17:35 UTC

This package is auto-updated.

Last update: 2024-09-30 17:38:44 UTC


README

GitHub tag (latest by date) License: MIT GitHub last commit GitHub issues GitHub stars GitHub forks GitHub contributors

GenericCollection 是一个支持各种原始类型的 PHP 库,用于处理集合。它允许进行灵活的类型验证和集合操作。

特性

  • 灵活的类型处理:支持多种原始类型,如 intstringfloatbool 等。
  • 类型验证:提供方法验证集合中的项目是否符合指定的类型。
  • 可扩展性:易于扩展以支持自定义类型和验证逻辑。

依赖关系

{
  "require": {
    "php": ">=7.4"
  },
  "require-dev": {
    "phpunit/phpunit": "9.6.20"
  }
}

安装

您可以通过 Composer 安装 GenericCollection。如果您还没有安装 Composer,您可以从 getcomposer.org 获取。

运行以下命令安装 GenericCollection

composer require shahmal1yev/gcollection

用法

以下是一个使用 GenericCollection 的基本示例

<?php

require 'vendor/autoload.php';

use GenericCollection\GenericCollection;
use GenericCollection\Types\Primitive\StringType;

class Foo
{
    public function __toString(): string
    {
        return "Foo as string";
    }
}

// Create a new collection with Foo validation
$collection = new GenericCollection(Foo::class);

// Add items to the collection
$collection[] = new Foo;
$collection->add(1, new Foo);

// Retrieve items from the collection
echo $collection[0];
echo $collection->get(1); // Outputs: Foo as string

// Validate an item
var_dump($collection->validate(new Foo)); // Outputs: bool(true)
<?php

require 'vendor/autoload.php';

use GenericCollection\GenericCollection;
use GenericCollection\Types\Primitive\StringType;

// Create a new collection with StringType validation
$collection = new GenericCollection(new StringType());

// Add items to the collection
$collection[] = "Hello";
$collection->add(1, "World");

// Retrieve items from the collection
echo $collection[1];
echo $collection->get(0); // Outputs: Hello

// Validate an item
var_dump($collection->validate("Test")); // Outputs: bool(true)

支持的原始类型

GenericCollection 包含以下原始类型

  • IntType:验证整数值。
  • StringType:验证字符串值。
  • FloatType:验证浮点值。
  • BoolType:验证布尔值。
  • ObjectType:验证对象值。
  • CallableType:验证可调用值。
  • ResourceType:验证资源值。
  • IterableType:验证可迭代值。

贡献

欢迎贡献!请按照以下步骤向项目贡献

  1. 仓库分支:通过在 GitHub 上创建分支来创建个人仓库副本。
  2. 创建分支:为您的更改创建一个新的分支。
  3. 进行更改:实现您的更改或修复问题。
  4. 提交拉取请求:推送您的更改并提交拉取请求以进行审查。

运行测试

要运行测试,请确保您已安装 PHPUnit。您可以通过 Composer 安装它。

composer require --dev phpunit/phpunit:9.6.20

使用以下命令运行测试

vendor/bin/phpunit ./tests

许可证

本项目采用 MIT 许可证。更多信息请参阅 LICENSE

联系

如有任何疑问或反馈,请通过 LinkedIn 联系我们。