granam/strict-scalar

This package is abandoned and no longer maintained. The author suggests using the granam/scalar package instead.

Lightweight scalar-type wrapper with strict checks

2.1.2 2015-09-16 11:22 UTC

This package is not auto-updated.

Last update: 2016-03-16 21:14:14 UTC


README

Build Status

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());
}