<?php
use Assert\Assertion;
class Foo
{
public function intOnly($value)
{
Assertion::integer($value);
return $value;
}
public function floatOnly($value)
{
Assertion::float($value);
return $value;
}
public function sum($collection)
{
Assertion::isTraversable($collection);
$result = 0;
foreach ($collection as $value) {
Assertion::integer($value);
$result += $value;
}
return $result;
}
}
$foo = new Foo();
try {
$foo->intOnly('1');
} catch (Exception $e) {
dump($e->getMessage());
}
try {
$foo->floatOnly(1);
} catch (Exception $e) {
dump($e->getMessage());
}
try {
$foo->sum([1, 1, '1']);
} catch (Exception $e) {
dump($e->getMessage());
}
try {
$foo->sum(4);
} catch (Exception $e) {
dump($e->getMessage());
}
^ "Value "1" is not an integer."
^ "Value "1" is not a float."
^ "Value "1" is not an integer."
^ "Value "4" is not an array and does not implement Traversable."