guym4c/prop-types
Composer 安装命令:
composer require guym4c/prop-types
包简介
Complete PHP port of React PropTypes (fork of prezly/prop-types)
README 文档
README
A fork of prezly/prop-types-php. Please consider using the original package
Complete PHP port of React PropTypes.
Runtime type checking for complex properties structures.
You can use prop-types to document the intended types of properties passed into your code. PropTypes will check props passed to your functions against those definitions, and throw an error if they don’t match.
Installation
composer require guym4c/prop-types
Credit
This is a fork of prezly/prop-types-php.
This package adds:
- Iterable proptype
- Callable proptype
- Number float/int shorthand proptype
- PHP7.4
Usage
PropTypes was originally exposed as part of the React core module, and is commonly used with React components. We've tried to bring the familiarity of React PropTypes into PHP. Here is an example of using PropTypes with a PHP function, which also documents the different validators provided.
You can call PropTypes::check() to validate an array of props, providing it with a props spec as below:
use Guym4c\PropTypes\Exception\PropTypeException; use Guym4c\PropTypes\PropTypes; [ // You can declare that a prop has a specific type. // By default, these are all optional. 'optionalArray' => PropTypes::array(), 'optionalBool' => PropTypes::bool(), 'optionalFunction' => PropTypes::callable(), 'optionalInteger' => PropTypes::int(), 'optionalFloat' => PropTypes::float(), 'optionalIterable' => PropTypes::iterable(), 'optionalObject' => PropTypes::object(), 'optionalString' => PropTypes::string(), // You can also declare that a prop is an instance of a class. // This uses `instanceof` operator. 'optionalDateTime' => PropTypes::instanceOf(DateTime::class), // You can ensure that your prop is limited to specific values // by treating it as an enum. 'optionalEnum' => PropTypes::oneOf(['News', 'Photos']), // An object that could be one of many types 'optionalUnion' => PropTypes::oneOfType([ PropTypes::string(), PropTypes::int(), PropTypes::instanceOf(DateTime::class), ]), // Float or int shorthand 'optionalNumber' => PropTypes::number(), // An array of a certain type 'optionalArrayOf' => PropTypes::arrayOf(PropTypes::int()), // You can chain any of the above with `isRequired` // to make sure an error is thrown if the prop isn't provided. // An object taking on a particular shape 'optionalArrayWithShape' => PropTypes::shape([ 'optionalProperty' => PropTypes::string(), 'requiredProperty' => PropTypes::int()->isRequired(), ]), // An object with errors on extra properties 'optionalObjectWithStrictShape' => PropTypes::exact([ 'optionalProperty' => PropTypes::string(), 'requiredProperty' => PropTypes::int()->isRequired(), ]), // A value of any data type (except null) 'requiredAny' => PropTypes::any()->isRequired(), // A value of any data type (including null) 'requiredNullableAny' => PropTypes::any()->isRequired()->isNullable(), // A required property that can be string or null 'requiredNullableString' => PropTypes::string()->isRequired()->isNullable(), // You can also specify a custom validator. // It should return a PropTypeException instance if the validation fails. 'customProp' => PropTypes::callback( function (array $props, string $prop_name, string $prop_full_name): ?PropTypeException { if (! preg_match('/matchme /', $props[$prop_name])) { return new PropTypeException( $prop_name, 'Invalid prop `' . $prop_full_name . '` supplied. Validation failed.' ); } return null; } ), ];
Difference from React PropTypes
-
In this package we've split required and nullable checks into different traits:
- Required means a property has to be defined in the props object
- Nullable means a property value can be set to
null
React PropTypes has less straightforward logic around required, nulls and undefined.
-
As opposed to React PropTypes we don't have a separate checker for null (
PropTypes::null()). Instead any property can become nullable by calling->isNullable()on its checker:[ 'title' => PropTypes::string()->isNullable(), ];
guym4c/prop-types 适用场景与选型建议
guym4c/prop-types 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.28k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 guym4c/prop-types 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guym4c/prop-types 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 13.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-26