jascha030/type-collections
最新稳定版本:1.0.0
Composer 安装命令:
composer require jascha030/type-collections
包简介
README 文档
README
A small set of interfaces and an abstract implementation, extending the Collection class from illuminate/collections, used to create collections with predefined content types.
Getting started
Prerequisites
- PHP
>=8.0.2
Having the phpstan/phpstan composer package installed globally is not required, but useful.
Installation
composer require Jascha030/type-collections
Usage
You can create a "typed" collection class by extending Jascha030\TypeCollection\TypeCollectionAbstract, this will require you to implement the getType method.
<?php declare(strict_types=1); namespace Jascha030\TypeCollections; /** * @template T * * @extends \Jascha030\TypeCollections\ValidatedCollectionInterface<T> */ interface TypeCollectionInterface extends ValidatedCollectionInterface { /** * @return class-string<T>|string */ public function getType(): string; }
This package also contains the simplest implementation for these interfaces which takes the accepted type as first constructor parameter.
<?php declare(strict_types=1); use Jascha030\TypeCollections\TypeCollection; use Jascha030\TypeCollections\Tests\Fixtures\ExampleModel; $items = [ new ExampleModel(1), new ExampleModel(2), ]; $collection = new TypeCollection(ExampleModel::class, $items);
PHPStan annotations
This package uses phpstan to annotate your class for better code completion.
You can do this by annotating your class using the @implements PHPDoc tag like: @implements TypeCollectionAbstract<YOUR_CLASS_NAME>.
You can see how this is done for the ExampleCollection class used for unit testing this package below.
<?php declare(strict_types=1); namespace Jascha030\TypeCollections\Tests\Fixtures; use Jascha030\TypeCollections\TypeCollectionAbstract; /** * @implements TypeCollectionAbstract<ExampleModel> */ final class ExampleCollection extends TypeCollectionAbstract { // ... }
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-25