sandromiguel/php-type
Composer 安装命令:
composer require sandromiguel/php-type
包简介
Validate PHP field values to ensure data integrity and suppress linter alerts.
README 文档
README
PhpType is a PHP library meticulously crafted for validating field values, safeguarding data integrity, and quelling linter alerts. It goes beyond mere linting concerns by seamlessly incorporating runtime checks. These runtime checks act as a robust validation layer during code execution, ensuring that your data aligns with the specified types. This dual approach not only suppresses linter warnings but also fortifies your application's integrity, instilling an extra level of confidence in your data handling processes.
Features
- Supports the
int,string,bool, andarraydata types. - Provides a simple and easy-to-use interface.
- Is lightweight and efficient.
- Is well-documented.
Requirements
- PHP 8.1 or higher
Table of Contents
Installation
You can install this library via Composer. Run the following command:
composer require sandromiguel/php-type
Usage
This library excels in validating arrays, ensuring that data adheres to specified types and contributing to a more robust and reliable codebase. Consider the following scenarios where PhpType proves beneficial:
Scenario 1: Method Input Validation
When accepting arrays as method parameters, it's crucial to validate the input to guarantee that the data aligns with the expected types. Using PhpType in such scenarios enhances code reliability and prevents unexpected type mismatches.
Example thats shows a linter warning:
/** * @param array<string,int|string> $someArray The array. */ public static function someMethodWithWarning(array $someArray): void { $someEntity = new Entity( $someArray['someInt'], $someArray['someString'] ); echo $someEntity->getProperty1() . "\n"; echo $someEntity->getProperty2() . "\n"; }
Address the previous warning with PhpType:
/** * @param array<string,int|string> $someArray The array. */ public static function someMethodWithPhpType(array $someArray): void { $someInt = Validator::validate('someInt', $someArray['someInt'])->getIntValue(); $someString = Validator::validate('someString', $someArray['someString'])->getStringValue(); $someEntity = new Entity($someInt, $someString); echo $someEntity->getProperty1() . "\n"; echo $someEntity->getProperty2() . "\n"; }
Scenario 2: Method Output Validation
In situations where it is necessary to return values from an array, such as strings, PhpType ensures that the returned values adhere to the expected types.
use PhpType\Validator; /** * Get some text. * * @return string The text. */ public function getSomeText(): string { return Validator::validate( 'fieldName', $this->params['fieldName'] ?? null ) ->stringNotEmpty() ->getStringValue(); }
Public Methods
-
validate(string $fieldName, mixed $fieldValue): ValidatorStart the validation chain for a field. -
stringNotEmpty(): ValidatorCheck if the string is not empty. -
getArrayValue(): arrayGet the array value. -
getArrayValueOrNull(): ?arrayGet the array value or null. -
getBoolValue(): boolGet the boolean value. -
getBoolValueOrNull(): ?boolGet the boolean value or null. -
getIntValue(): intGet the integer value. -
getIntValueOrNull(): ?intGet the integer value or null. -
getStringValue(): stringGet the string value. -
getStringValueOrNull(): ?stringGet the string value or null. -
getValue(): mixedGet the mixed value.
Frequently Asked Questions (FAQ)
Q: Why use PhpType when linters like Psalm or PHPStan already exist?
A: While linters are powerful tools, PhpType serves as a lightweight solution specifically designed to handle scenarios where linter warnings arise due to type mismatches in array structures. It provides a simple and easy-to-use interface, offering an alternative approach to address such issues.
Q: Can I use PhpType in production code?
A: Yes, PhpType is intended for use in production code. It is perfectly suitable for production use without any issues.
Q: Why not use deserialization libraries like symfony/serializer for handling array data?
A: Deserialization libraries like symfony/serializer are excellent for handling complex data structures by converting them into objects. PhpType is more suited for scenarios where direct manipulation of arrays is necessary, such as dealing with legacy code or situations where immediate refactoring is not feasible.
Q: How do I handle linter warnings with PhpType?
A: PhpType helps eliminate linter warnings by providing a straightforward method for validating array values, ensuring type correctness, and suppressing alerts from linters like PHPStan. Check the documentation for examples and usage details.
Credits
- EditorConfig: IDE coding style settings.
- PHPUnit: Testing framework for PHP.
- PHP CodeSniffer: PHP coding standards checker and fixer.
- PHP Insights: Code quality and architecture analysis tool.
- PHP Metrics: PHP metrics generator.
- PHPStan: PHP static analysis tool.
- Psalm: Static analysis tool for PHP.
- Composer: Dependency management for PHP.
Contributing
Want to contribute? All contributions are welcome. Read the contributing guide.
Questions
If you have questions tweet me at @sandro_m_m or open an issue.
License
This project is licensed under the MIT License - see the LICENSE file for details
**~ sharing is caring ~**
sandromiguel/php-type 适用场景与选型建议
sandromiguel/php-type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「library」 「validation」 「type」 「field」 「php-library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sandromiguel/php-type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sandromiguel/php-type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sandromiguel/php-type 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Inbox pattern process implementation for your Laravel Applications
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-29