Search by

Try to coerce mixed values into desired types or die trying

Package info

github.com/HealthEngineAU/coerce

Issues

pkg:composer/healthengine/coerce

Statistics

Installs: 52 239

Dependents: 0

Suggesters: 0

Stars: 0

v1.2.0 2026-09-09 05:16 UTC

This package is auto-updated.

Last update: 2026-09-09 05:17:16 UTC


README

About

Coerce mixed value into a desired scalar type or die trying.

Install

composer require healthengine/coerce

Usage

<?php

declare(strict_types=1);

use Healthengine\Coerce\Coerce;
use Healthengine\Coerce\CouldNotCoerceException;
use stdClass;

Coerce::toBool(1); // true

Coerce::toBoolOrNull(null) // null

Coerce::toInt('1'); // 1

Coerce::toInt(new stdClass()); // CouldNotCoerceException

Coerce::toIntOrNull(null) // null

Coerce::toIntOrNull(new stdClass()); // CouldNotCoerceException

Coerce::toNonEmptyString('123'); // '123'

Coerce::toNonEmptyString(''); // CouldNotCoerceException

Coerce::toString(1); // '1'

Coerce::toString([]) // CouldNotCoerceException

Coerce::toStringOrNull(null); // null

Coerce::toStringOrNull([]) // CouldNotCoerceException