decodelabs/coercion

Simple tools for managing PHP types

v0.1.3 2022-03-14 23:30 UTC

This package is auto-updated.

Last update: 2022-08-15 00:44:05 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

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.

Installation

Install via Composer:

composer require decodelabs/coercion

PHP version

Please note, the final v1 releases of all Decode Labs libraries will target PHP8 or above.

Current support for earlier versions of PHP will be phased out in the coming months.

Usage

Pass any mixed value to the available coerce methods to ensure input types in constructors:

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 for the full license text.