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.
Install via Composer:
composer require decodelabs/coercion
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']);
}
}
Coercion is licensed under the MIT License. See LICENSE for the full license text.