定制 pre/prop-types 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

pre/prop-types

Composer 安装命令:

composer require pre/prop-types

包简介

Prop type validators for Phpx components

README 文档

README

Support on Gitstore

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
}
  • isRequired will ensure the value is present in the accompanying properties array
  • either allows 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 type
  • PropTypes::boolean() is the same as PropTypes::bool()
  • PropTypes::integer() is the same as PropTypes::int()
  • PropTypes::double() expects double values
  • PropTypes::iterable() expects any iterable values
  • PropTypes::numeric() expects any numeric values
  • PropTypes::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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-14