Code Monkey home page Code Monkey logo

prop-types-php's Introduction

PropTypes.php

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

  1. 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.

  2. 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(),
    ];

prop-types-php's People

Contributors

e1himself avatar guym4c avatar anatoliyarkhipov avatar majermi4 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.