pre/prop-types
Composer 安装命令:
composer require pre/prop-types
包简介
Prop type validators for Phpx components
README 文档
README
This is a PHP take on the ReactJS Prop Types checking. I made it because I wanted an easier way to validate the type and presence of properties in Phpx components.
Getting started
git clone git@github.com:preprocess/pre-prop-types.git
cd pre-prop-types
composer install
composer test
Defining types
We support most of the built-in types:
use App\Profile; use Pre\PropTypes; $profile = Profile::find(1); $definitions = [ "name" => PropTypes::string()->isRequired(), "age" => PropTypes::int(), "rating" => PropTypes::float(), "permissions" => PropTypes::arrayOf(PropTypes::string()), "thumbnail" => PropTypes::either( PropTypes::string(), // uri PropTypes::resource(), // file handle ), "profile" => PropTypes::objectOf(Profile::class)->isRequired(), "onMessage" => PropTypes::closure()->isRequired(), "isAdmin" => PropTypes::bool()->isRequired(), ]; $properties = [ "name" => "Joe", "profile" => $profile, "onMessage" => function($message) use ($profile) { $profile->notify($message); }, "isAdmin" => false, ]; try { PropTypes::validate($definitions, $properties); } catch (InvalidArgumentException $e) { // ...handle the error }
isRequiredwill ensure the value is present in the accompanying properties arrayeitherallows one or more types (preferably two distinct types) for comparison
There are also variations on these:
PropTypes::array()expects any array values, without a specific typePropTypes::boolean()is the same asPropTypes::bool()PropTypes::integer()is the same asPropTypes::int()PropTypes::double()expects double valuesPropTypes::iterable()expects any iterable valuesPropTypes::numeric()expects any numeric valuesPropTypes::object()expects any object values, without a specific type
Validating types
We don't automatically validate props – this must be done by the consumer. And example Phpx render method demonstrates this:
use Pre\PropTypes; use function Pre\Phpx\Html\render as renderHTML; function render($type, $props = []) { $props = (array) $props; if (class_exists($type)) { if (method_exists($type, "propTypes")) { PropTypes::validate($type::propTypes(), $props); } if (method_exists($type, "defaultProps")) { $props = array_merge($type::defaultProps(), $props); } } return renderHTML($type, $props); } render("App\\Custom\\Component");
pre/prop-types 适用场景与选型建议
pre/prop-types 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pre/prop-types 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pre/prop-types 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-14