nkopylov/php-typed-collections
最新稳定版本:v0.0.1
Composer 安装命令:
composer require nkopylov/php-typed-collections
包简介
Generatable statically typed collections for PHP entities
关键字:
README 文档
README
Generatable statically typed collections for PHP entities.
Example of the collection
<?php /** * Autogenerated. * Time: 2022-10-04 06:17:44 */ declare(strict_types=1); namespace Nkopylov\Test\PhpCollections; use JetBrains\PhpStorm\Pure; use Nkopylov\Test\PhpCollections\TestClass as CollectionEntity; /** * Autogenerated typed collection for Nkopylov\Test\PhpCollections\TestClass objects. * Check nkopylov/php-typed-collections for more information * * @implements \Iterator<mixed, CollectionEntity> * @implements \ArrayAccess<mixed, CollectionEntity> * @codeCoverageIgnore */ class TestCollection_testCollection implements \Iterator, \ArrayAccess { /** * @var CollectionEntity[] */ private array $elements = []; /** * @var callable */ private $idMapper; /** * @phpstan-pure * Creates new collection * @return static */ public static function create(): self { return new static(function() {static $counter = 0; return $counter++;}); } final public function __construct(callable $idMapper) { $this->idMapper = $idMapper; } /** * @phpstan-pure * Returns the first entity from the collection. * @return CollectionEntity|null */ public function first(): ?CollectionEntity { if ($this->count() === 0) { return null; } $this->rewind(); return $this->current(); } /** * {@inheritdoc} * @return CollectionEntity */ public function current(): CollectionEntity { $entity = current($this->elements); if ($entity === false) { throw new \LogicException('Collection is empty'); } return $entity; } /** * {@inheritdoc} */ public function next(): void { next($this->elements); } /** * {@inheritdoc} */ public function key(): mixed { return key($this->elements); } /** * {@inheritdoc} */ public function valid(): bool { return $this->key() !== null; } /** * {@inheritdoc} */ public function rewind(): void { reset($this->elements); } /** * {@inheritdoc} */ public function offsetExists($offset): bool { return isset($this->elements[$offset]); } /** * {@inheritdoc} */ public function offsetSet($offset, $value): void { if (!($value instanceof CollectionEntity)) { throw new \InvalidArgumentException(sprintf("Can't insert object of class %s to %s", get_class($value), self::class)); } $this->elements[$offset] = $value; } /** * {@inheritdoc} */ public function offsetUnset($offset): void { unset($this->elements[$offset]); } /** * {@inheritdoc} */ public function offsetGet($offset): CollectionEntity { return $this->elements[$offset]; } /** * Add entity to the collection * @param CollectionEntity $item * @return $this */ public function add(CollectionEntity $item): self { $this->elements[($this->idMapper)($item)] = $item; return $this; } /** * Add entities from array to the collection * @param iterable<CollectionEntity> $items * @return $this */ public function addArray(iterable $items): self { foreach ($items as $item) { $this->add($item); } return $this; } /** * Remove entity from collection by id * @param mixed $id * @return $this */ public function remove($id): self { unset($this->elements[$id]); return $this; } /** * Check if entity exists by id * @param mixed $id * @return bool */ public function has($id): bool { return isset($this->elements[$id]); } /** * Get entity by id. * @param mixed $id * @return CollectionEntity|null */ public function get($id): ?CollectionEntity { return $this->elements[$id] ?? null; } /** * @phpstan-pure * Map collection by given callable. Creates new collection * @param callable $mapper * @return static */ public function map(callable $mapper): self { return (new static($this->idMapper)) ->addArray(array_map($mapper, $this->elements)); } /** * @phpstan-pure * Creates a new collection mapped by key defined by given callable * @param callable $idMapper * @return static */ public function mapKeys(callable $idMapper): self { return (new static($idMapper))->addArray($this->elements); } /** * Map collection to array by given callable. * @param callable $mapper * @return array<mixed, CollectionEntity> */ public function mapToArray(callable $mapper): array { return array_map($mapper, $this->elements); } /** * Transform current collection with given function * @param callable $mapper * @return $this */ public function transform(callable $mapper): self { $this->elements = array_map($mapper, $this->elements); return $this; } /** * @phpstan-pure * Filter collection by given callback. Returns new collection * @param callable $mapper * @return static */ public function filter(callable $mapper): self { return (new static($this->idMapper))->addArray(array_filter($this->elements, $mapper) ?? []); } /** * Sort collection by given callback * @param callable $sorter * @return $this */ public function sort(callable $sorter): self { uasort($this->elements, $sorter); return $this; } /** * @phpstan-pure * Split collection on the given number of chunks. Returns array of new collections * @param int $size * @return array<int, self> */ public function chunk(int $size): array { return array_map( fn(array $chunk) => (new static($this->idMapper))->addArray($chunk), array_chunk($this->elements, $size) ); } /** * @phpstan-pure * Groups collection by some value returned by a given callable * @param callable $keyResolver * @return static[] */ public function groupBy(callable $keyResolver): array { $result = []; foreach ($this->elements as $element) { $key = $keyResolver($element); if (!isset($result[$key])) { $result[$key] = new static($this->idMapper); } $result[$key]->add($element); } return $result; } /** * Returns collection size. * @return int */ public function count(): int { return count($this->elements); } /** * Clear collection * @return $this */ public function clear(): self { $this->elements = []; return $this; } /** * @phpstan-pure * Merges current collection with a given collection. Returns new collection * @param self $collection * @return self */ public function merge(self $collection): self { return (clone $collection)->addArray($this->elements); } /** * @phpstan-pure * Slices collection. Returns new collection as a result * @param int $offset * @param int|null $limit * @return static */ public function slice(int $offset, ?int $limit = null): self { if ($offset === 0 && empty($limit)) { return $this; } $collection = new static($this->idMapper); $collection->addArray(array_slice($this->elements, $offset, $limit, true)); return $collection; } /** * Cast collection to array * @return CollectionEntity[] */ public function toArray(): array { return $this->elements; } /** * Return collection keys * @return array<mixed> */ public function keys(): array { return array_keys($this->elements); } }
Installation
Install this package as a dependency using Composer.
composer require nkopylov/php-typed-collections
Usage
vendor/bin/typed-collections generate <--class=> <--generated-class-name=> <--path=> <--namespace=> <--template=> <--parent-class=> <--interfaces=> <--traits=>
generate command generates a new statically typed collection and puts it alongside the collectable PHP class.
Command parameters:
--class- mandatory parameter. Contains full class name for the collectable entity.--generated-class-name- by default collection class name would be Collection. If you want to redefine this name - use this parameter for the short name of the class.--namespace- by default collection would be created in the same namespace as the collectable class. If you want to redefine the namespace - use this parameter for the full namespace name--path- by default collection would be placed alongside the collectable class in the file system. If you changed namespace or you need this collection to be put anywhere else - use this parameter for the new path--template- default template is already set. It contains default methods and supports\Iterableand\ArrayAccessinterfaces. Use this parameter if you want to set your own template. See src/templates/collection.template for the example.--parent-class- use this parameter if you want generated collection to extend some class.--interfaces- use this parameter if you need generated collection to implement some interfaces. Provide full name for each interface, split interfaces by comma.--traits- use this parameter if you need generated collection to use some traits. Provide list of full trait names, separated by comma.
Example
vendor/bin/typed-collections generate --class=\MyEntity
This command will generate a collection \MyEntityCollection and put it alongside \MyEntity class.
Key mapping
php-typed-collections supports three ways to set keys for collection entities:
- Autogenerated keys by int autoincrement (default)
getId()method in the collectable class. Implement\Nkopylov\PhpCollections\ObjectWithIdentifierto let generator knows, that you class supports that feature.- You can also provide your own function, that will be used for the key generation. Check
\Nkopylov\PhpCollections\Mappableinterface for more information. Also, test object\Nkopylov\Test\PhpCollections\TestObjectWithMappablewill help you.
Contributing
Contributions are welcome! Before contributing to this project, familiarize yourself with CONTRIBUTING.md.
To develop this project, you will need PHP 7.4 or greater and Composer.
After cloning this repository locally, execute the following commands:
cd /path/to/repository
composer install --dev
Now, you are ready to develop!
nkopylov/php-typed-collections 适用场景与选型建议
nkopylov/php-typed-collections 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「collections」 「php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nkopylov/php-typed-collections 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nkopylov/php-typed-collections 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nkopylov/php-typed-collections 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Doctrine Collections adapter for Rekapager pagination library
Library to mimics generic collections
The helpers allows you to easy use DATE. Change format, add/sub day, diff date, get quarter - all this is available in this library.
Kyanda API Library
Collection classes for the Monolith framework.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2022-10-04