尝试将混合值强制转换为所需类型,或者尝试失败

v1.0.2 2024-06-28 02:03 UTC

This package is auto-updated.

Last update: 2024-08-28 02:32:27 UTC


README

关于

混合值强制转换为所需的标量类型,或者尝试失败。

安装

composer require healthengine/coerce

用法

<?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