承接 phpgt/typesafegetter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Build status Code quality Code coverage Current version PHP.GT/TypeSafeGetter documentation

This package defines:

  • GT\TypeSafeGetter\TypeSafeGetter
  • GT\TypeSafeGetter\NullableTypeSafeGetter
  • GT\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:

  • DateTimeInterface instances
  • 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 type
  • getString(string $name):?string
  • getInt(string $name):?int
  • getFloat(string $name):?float
  • getBool(string $name):?bool
  • getDateTime(string $name):?DateTimeInterface
  • getInstance(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

JetBrains Open Source sponsorship program

JetBrains logo.

phpgt/typesafegetter 适用场景与选型建议

phpgt/typesafegetter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.82k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 phpgt/typesafegetter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phpgt/typesafegetter 我们能提供哪些服务?
定制开发 / 二次开发

基于 phpgt/typesafegetter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26.82k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 28
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-16