Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/attitude/prop-types-php

A very lean implementation of React Prop Types library in PHP
https://github.com/attitude/prop-types-php

Last synced: about 6 hours ago
JSON representation

A very lean implementation of React Prop Types library in PHP

Awesome Lists containing this project

README

        

# React PropTypes implemented in PHP

A lean implementation inspired by the [React Prop Types](https://reactjs.org/docs/typechecking-with-proptypes.html) library and [Flow](https://flow.org) done for PHP. All the types are overridable (displays warnings you can turn off) and you can even define/register your own types for better reuse (inspired by the Flow types).

All registered types are accessible as static methods of the `PropTypes` class.

## Install using composer:

```json
{
"name": "you/example-project",
"repositories": [
{
"type": "vcs",
"url": "https://github.com/attitude/prop-types-php"
}
],
"require": {
"attitude/prop-types-php": "dev-main"
}
}
```

## Use in your code

```php
PropTypes::array()->isRequired,
'bool' => PropTypes::bool(),
'structure' => PropTypes::shape([
'deep' => PropTypes::array()->isRequired,
'enum' => PropTypes::oneOf(['1', 2, 'three']),
'oneOf' => PropTypes::oneOfType([
PropTypes::array(),
PropTypes::string(),
PropTypes::bool(),
])->isRequired,
'arrayOf' => PropTypes::arrayOf(PropTypes::string())->isRequired,
'objectOf' => PropTypes::objectOf(PropTypes::string())->isRequired,
'requiredAny' => PropTypes::any()->isRequired,
'instanceOf' => PropTypes::instanceOf('StdClass')->isRequired,
]),
]));

function SomeComponent($props) {
// Check the props
PropTypes::SomeComponentType()->assert($props);

// rest of the component code...
}

// Run your code:
SomeComponent([
'names' => ['Martin'],
'bool' => true,
'structure' => [
'deep' => ['Adamko'],
'enum' => 2,
'oneOf' => '',
'arrayOf' => ['2'],
'objectOf' => (object)['2'],
'requiredAny' => '',
'instanceOf' => (object) [],
],
'extraKey' => '123', // <<< This should throw an error
]);

```

## Options

Constant | type | default | Description
----------------------------------|---------|---------|------------
`PROP_TYPES_STRICT_OBJECTS_SHAPE` | boolean | false | Whether Shape accepts both `StdClass` and `array` or strictly just `StdClass` instance
`PROP_TYPES_WARNINGS_ENABLED` | boolean | true | Show or hide warnings

## API

- #### static method `PropTypes::register()`
Used for registering a new type. See `/src/bootstrap.php` for more examples.
##### Arguments:
- *string* `$typeName` - name for the type, e.g. `'Props'`
- *callable | TypeInterface* `$typeFactoryOrType` - a `TypeInterface` of a factory function that returns a `TypeInterface`
- #### static method `PropTypes::__callStatic()`
Overloading method used to get any registered type Some of the types require arguments. Consult the [React Prop Types](https://reactjs.org/docs/typechecking-with-proptypes.html) documentation.

```php
// Get registered type instance:
$type = `PropTypes::PreviouslyRegisteredTypeName()`;
// Validates props variable against the registered type:
$type->assert ($props);
```

> This project is predecessor of Duck Types for PHP — your asserts turn into readable and short one-liners with Flow-flavoured syntax annotations.

---

Not implemented types:

- symbol
- node
- element
- elementType