PHP code example of healthengine / coerce

1. Go to this page and download the library: Download healthengine/coerce library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

healthengine / coerce example snippets




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