Skip to content

decodelabs/coercion

Repository files navigation

Coercion

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.

Get news and updates on the DecodeLabs blog.


Installation

Install via Composer:

composer require decodelabs/coercion

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.

About

Simple tools for managing PHP types

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages