phpgt/typesafegetter
Composer 安装命令:
composer require phpgt/typesafegetter
包简介
An interface for objects that expose type-safe getter methods.
README 文档
README
Throughout PHP.GT repositories, wherever an object represents enclosed data, a consistent interface is used to expose the data in a type-safe manner.
This package defines:
GT\TypeSafeGetter\TypeSafeGetterGT\TypeSafeGetter\NullableTypeSafeGetterGT\TypeSafeGetter\CallbackTypeSafeGetter
The usual pattern is to implement get() yourself and use the NullableTypeSafeGetter trait for the common typed methods.
use DateTimeImmutable; use DateTimeInterface; use GT\TypeSafeGetter\NullableTypeSafeGetter; use GT\TypeSafeGetter\TypeSafeGetter; class DataStore implements TypeSafeGetter { use NullableTypeSafeGetter; public function __construct( private readonly array $data = [], ) {} public function get(string $name):mixed { return $this->data[$name] ?? null; } } $store = new DataStore([ "id" => "42", "active" => 1, "created" => "2024-05-01 09:15:00", ]); echo $store->getInt("id"); var_dump($store->getBool("active")); echo $store->getDateTime("created")?->format("Y-m-d");
getInstance() returns an existing object after checking its type:
$store = new DataStore([ "date" => new DateTimeImmutable("2024-05-01"), ]); $date = $store->getInstance("date", DateTimeInterface::class);
getDateTime() accepts:
DateTimeInterfaceinstances- Unix timestamps
- date/time strings supported by
DateTimeImmutable
The following methods are defined by this interface:
get(string $name):mixed- A non-type-safe getter, used for getting keys that are not of an inbuilt typegetString(string $name):?stringgetInt(string $name):?intgetFloat(string $name):?floatgetBool(string $name):?boolgetDateTime(string $name):?DateTimeInterfacegetInstance(string $name, class-string<T> $className):?T
Common areas you will see this interface used:
- Database rows
- User input (from the query string or posted form data)
- Session and cookie storage
- HTTP header collections
- Configuration objects
- PHP.GT's DataObject repository.
NullableTypeSafeGetter trait
A lot of repositories within PHP.GT that utilise this class were repeating the same getter code, so this trait was introduced to remove the repetition. All getter functions of the interface are implemented, introducing a protected helper function getNullableType which removes the repetition of checking null values before casting them. The getNullableType function also allows a callback to be passed as the type parameter, allowing more complex nullable types to be constructed.
CallbackTypeSafeGetter interface
This interface is for callback-driven lookups where a value may need to be fetched or computed as part of the read operation, such as cache APIs.
Unlike NullableTypeSafeGetter, there is no trait implementation in this package for the callback interface. Implementing classes define their own callback behaviour.
Proudly sponsored by
phpgt/typesafegetter 适用场景与选型建议
phpgt/typesafegetter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.82k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phpgt/typesafegetter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpgt/typesafegetter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 26.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-16