jasny/immutable
Composer 安装命令:
composer require jasny/immutable
包简介
Helper methods for immutable objects
README 文档
README
The library provides a helper methods for immutable objects.
Installation
composer require jasny/immutable
Usage
NoDynamicProperties
The Jasny\Immutable\NoDynamicProperties defines the __set method, which will throw a LogicException when
attempting to set a non-existing property.
class ImmutableFoo { use Jasny\Immutable\NoDynamicProperties; }
With
Immutable objects may have with... methods, that create an altered version of the object, while the immutable object
itself remains unchanged. An example are classes work as described in PSR-7.
The with... methods in these objects typically follow the same method.
class ImmutableFoo { public function withSomething(string $newValue) { if ($this->something === $newValue) { return $this; } $clone = clone $this; $clone->something = $newValue; return $clone; } }
The Jasny\Immutable\With trait implements a protected withProperty() and withoutProperty() method for setting and
unsetting properties on a clone of the object.
class ImmutableFoo { use Jasny\Immutable\With; protected $something; public function withSomething(string $value): self { return $this->withProperty('something', $value); } public function withoutSomething(): self { return $this->withoutProperty('something'); } }
The trait contains the withPropertyKey() and withoutPropertyKey() methods for setting and unsetting an item of an
associative array.
class ImmutableFoo { use Jasny\Immutable\With; protected array $colors = []; public function withColor(string $color, int $level): self { return $this->withPropertyKey('colors', $color, $level); } public function withoutColor(string $color): self { return $this->withoutPropertyKey('colors', $color); } }
The withPropertyItem() and withoutPropertyItem() methods work on a sequential array to add and remove an item.
class ImmutableFoo { use Jasny\Immutable\With; protected array $services = []; public function withAddedService($service): self { return $this->withPropertyItem('services', $service, true /* unique */); } public function withoutService($service): self { return $this->withoutPropertyItem('services', $service); } }
If the third argument of withPropertyItem() is set to true, the item isn't added if it's already in the array.
If the item is in the array multiple times, withoutPropertyItem() will remove them all. Strict comparison is used to
find items.
jasny/immutable 适用场景与选型建议
jasny/immutable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59.72k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jasny/immutable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jasny/immutable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 59.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 16
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-15