guym4c/prop-types

React PropTypes 的完整 PHP 版本(prezly/prop-types 的分支)

v1.0.1 2020-04-26 23:35 UTC

This package is auto-updated.

Last update: 2024-09-27 09:39:37 UTC


README

这是 prezly/prop-types-php 的分支。请考虑使用原始包

React PropTypes 的完整 PHP 版本。

对复杂属性结构的运行时类型检查。

您可以使用 prop-types 来记录传递给代码的属性预期类型。PropTypes 将检查传递给函数的 props 与这些定义进行比较,如果不匹配,则抛出错误。

安装

composer require guym4c/prop-types

信用

这是 prezly/prop-types-php 的分支。

此包增加了

  • 可迭代 prop 类型
  • 可调用 prop 类型
  • 数字浮点/整数简写 prop 类型
  • PHP7.4

使用方法

PropTypes 最初作为 React 核心模块的一部分公开,通常与 React 组件一起使用。我们试图将 React PropTypes 的熟悉度引入 PHP。以下是一个使用 PropTypes 与 PHP 函数的示例,它还记录了提供的不同验证器。

您可以通过调用 PropTypes::check() 来验证 props 数组,并为其提供以下 props 规范

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;
        }
    ),
];

与 React PropTypes 的区别

  1. 在这个包中,我们将 requirednullable 检查分成了不同的 trait

    • Required 表示属性必须在 props 对象中定义
    • Nullable 表示属性值可以设置为 null

    React PropTypes 在 required、nulls 和 undefined 方面逻辑不那么直接。

  2. 与 React PropTypes 不同,我们没有单独的 null 检查器(PropTypes::null())。相反,任何属性都可以通过在其检查器上调用 ->isNullable() 变为可空的

    [
       'title' => PropTypes::string()->isNullable(),
    ];