granam / strict-scalar
Lightweight scalar-type wrapper with strict checks
Installs: 754
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
pkg:composer/granam/strict-scalar
Requires
- php: >=5.4
- granam/exception-hierarchy: ~2.1
- granam/scalar: ~1.0
- granam/strict-object: ~1.1
Requires (Dev)
- mockery/mockery: >=0.9
- phpunit/phpunit: ~4.4
README
PHP does not provide scalar type hinting yet (planned for PHP 7.0).
For that reason, if we want to be sure about scalar type, a type-checking class is the only chance.
Warning: Non-strict scalar does not cast null - it remains null.
<?php
use Granam\Strict\Scalar\StrictScalar;
use Granam\Strict\Scalar\Exceptions\WrongParameterType;
$scalar = new StrictScalar('foo');
// foo
echo $scalar;
$nullScalar = new StrictScalar(null, false /* suppressed strictness */);
// false
echo is_scalar($nullScalar->getValue());
// true
echo is_null($nullScalar->getValue());
try {
new StrictScalar(null);
} catch (WrongParameterType $scalarException) {
// Strict scalar has to get a scalar value. Null is not a scalar.
die('Something get wrong: ' . $scalarException->getMessage());
}