Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/decodelabs/coercion
Simple tools for managing PHP types
https://github.com/decodelabs/coercion
coercion php types
Last synced: 1 day ago
JSON representation
Simple tools for managing PHP types
- Host: GitHub
- URL: https://github.com/decodelabs/coercion
- Owner: decodelabs
- License: mit
- Created: 2022-03-09T13:24:16.000Z (almost 3 years ago)
- Default Branch: develop
- Last Pushed: 2024-04-29T08:55:13.000Z (9 months ago)
- Last Synced: 2024-04-30T02:04:01.270Z (8 months ago)
- Topics: coercion, php, types
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Coercion
[![PHP from Packagist](https://img.shields.io/packagist/php-v/decodelabs/coercion?style=flat)](https://packagist.org/packages/decodelabs/coercion)
[![Latest Version](https://img.shields.io/packagist/v/decodelabs/coercion.svg?style=flat)](https://packagist.org/packages/decodelabs/coercion)
[![Total Downloads](https://img.shields.io/packagist/dt/decodelabs/coercion.svg?style=flat)](https://packagist.org/packages/decodelabs/coercion)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/decodelabs/coercion/integrate.yml?branch=develop)](https://github.com/decodelabs/coercion/actions/workflows/integrate.yml)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true&style=flat)](https://github.com/phpstan/phpstan)
[![License](https://img.shields.io/packagist/l/decodelabs/coercion?style=flat)](https://packagist.org/packages/decodelabs/coercion)### Simple tools for managing PHP types
Coercion offers simple tools to help neatly handle coercion of mixed parameters, especially useful when dealing with higher level static analysis test which require strict type handling.
_Get news and updates on the [DecodeLabs blog](https://blog.decodelabs.com)._
---
## Installation
Install via Composer:
```bash
composer require decodelabs/coercion
```## Usage
Pass any mixed value to the available coerce methods to ensure input types in constructors:
```php
use DecodeLabs\Coercion;class MyClass {
protected string $string;
protected ?string $optionalString;
protected int $int;public function __construct(array $params) {
$this->string = Coercion::toString($params['maybeString']);
$this->optionalString = Coercion::toStringOrNull($params['maybeString']);
$this->int = Coercion::toInt($params['maybeInt']);
}
}
```## Licensing
Coercion is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.