PHP code example of fabstract / assert
1. Go to this page and download the library: Download fabstract/assert 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/ */
fabstract / assert example snippets
function add($x, $y){
Assert::isNumeric($x); // throws exception if $x is not a number
Assert::isNumeric($y); // throws exception if $y is not a number
return $x + $y;
}
function addToArray($key, $value){
Assert::isValidArrayIndex($key, 'key');
$this->array[$key] = $value;
}
Assert::isObject(new stdClass(), 'variable name'); // no exception
Assert::isObject(1, 'variable name'); // exception!
Assert::isNotNull(5, 'variable name'); // no exception
Assert::isNotNull(null, 'variable name'); // exception!
Assert::isEqualTo(1, 1, 'variable name'); // no exception
Assert::isEqualTo(null, null, 'variable name'); // no exception
Assert::isEqualTo(1, 1.0, 'variable name'); // exception!
Assert::isEqualTo(1, '1', 'variable name'); // exception!
Assert::isEqualTo(1, 1.0, 'variable name'); // no exception
Assert::isEqualTo(1, '1', 'variable name'); // no exception
Assert::isEqualTo(1, 1, 'variable name'); // exception!
Assert::isEqualTo(null, null, 'variable name'); // exception!
class SomeClass {}
interface SomeInterface {}
Assert::isTypeExists('someclass', 'variable name'); // no exception
Assert::isTypeExists('someinterface', 'variable name'); // no exception
Assert::isTypeExists('someclass2', 'variable name'); // exception!
class SomeClass {}
interface SomeInterface {}
Assert::isClassExists('someclass', 'variable name'); // no exception
Assert::isClassExists('someinterface', 'variable name'); // exception!
Assert::isClassExists('someclass2', 'variable name'); // exception!
class SomeClass {}
interface SomeInterface {}
Assert::isInterfaceExists('someinterface', 'variable name'); // no exception
Assert::isInterfaceExists('someclass', 'variable name'); // exception!
trait SomeTrait {
public function somePublicTraitMethod() {}
private function somePrivateTraitMethod() {}
}
trait SomeOtherTrait {
public function someOtherPublicTraitMethod() {}
}
class SomeClass {
use SomeTrait;
public function somePublicMethod(){}
private function somePrivateMethod(){}
}
Assert::isMethodExists('someclass', 'somePublicMethod', 'variable name'); // no exception
Assert::isMethodExists('someclass', 'somePrivateMethod', 'variable name'); // no exception
Assert::isMethodExists('someclass', 'somePublicTraitMethod', 'variable name'); // no exception
Assert::isMethodExists('someclass', 'somePrivateTraitMethod', 'variable name'); // no exception
Assert::isMethodExists('someclass', 'someOtherPublicTraitMethod', 'variable name'); // exception!
Assert::isMethodExists('someclass', 'someMethodThatDoesNotExist', 'variable name'); // exception!
$instance = new SomeClass();
Assert::isMethodExists($instance, 'somePublicMethod', 'variable name'); // no exception
Assert::isMethodExists($instance, 'somePrivateMethod', 'variable name'); // no exception
Assert::isMethodExists($instance, 'somePublicTraitMethod', 'variable name'); // no exception
Assert::isMethodExists($instance, 'somePrivateTraitMethod', 'variable name'); // no exception
Assert::isMethodExists($instance, 'someOtherPublicTraitMethod', 'variable name'); // exception!
Assert::isMethodExists($instance, 'someMethodThatDoesNotExist', 'variable name'); // exception!
$allowed_value_list = ['1', '2', '3'];
Assert::isInArray('1', $allowed_value_list, true, 'variable name'); // no exception
Assert::isInArray('1', $allowed_value_list, false, 'variable name'); // no exception
Assert::isInArray(1, $allowed_value_list, false, 'variable name'); // no exception
Assert::isInArray(1, $allowed_value_list, true, 'variable name'); // exception!
Assert::isCallable(function(){}, 'variable name'); // no exception
Assert::isCallable('str_replace', 'variable_name'); // no exception
class SomeClass {
public function somePublicMethod(){}
public static function somePublicStaticMethod(){}
protected function someProtectedMethod(){}
public function somePrivateMethod(){}
}
Assert::isCallable(['someclass', 'somePublicMethod'], 'variable name'); // no exception
Assert::isCallable(['someclass', 'somePublicStaticMethod'], 'variable name'); // no exception
Assert::isCallable(['someclass', 'someProtectedMethod'], 'variable name'); // exception!
Assert::isCallable(['someclass', 'somePrivateMethod'], 'variable name'); // exception!
$instance = new SomeClass();
Assert::isCallable([$instance, 'somePublicMethod'], 'variable name'); // no exception
Assert::isCallable([$instance, 'somePublicStaticMethod'], 'variable name'); // no exception
Assert::isCallable([$instance, 'someProtectedMethod'], 'variable name'); // exception!
Assert::isCallable([$instance, 'somePrivateMethod'], 'variable name'); // exception!
Assert::isString('some string', 'variable name'); // no exception
Assert::isString('', 'variable name'); // no exception
Assert::isString(new stdClass(), 'variable name'); // exception!
Assert::isString(null, 'variable name'); // exception!
Assert::isBoolean(true, 'variable name'); // no exception
Assert::isBoolean(false, 'variable name'); // no exception
Assert::isBoolean('true', 'variable name'); // exception!
Assert::isBoolean(1, 'variable name'); // exception!
Assert::isInt(1, 'variable name'); // no exception
Assert::isInt(0, 'variable name'); // no exception
Assert::isInt('1', 'variable name'); // exception!
Assert::isStringOrInt(0, 'variable name'); // no exception
Assert::isStringOrInt('0', 'variable name'); // no exception
Assert::isStringOrInt(null, 'variable name'); // exception!
Assert::isStringOrInt([], 'variable name'); // exception!
Assert::isIntOrFloat(0, 'variable name'); // no exception
Assert::isIntOrFloat(0.5, 'variable name'); // no exception
Assert::isIntOrFloat('0.5', 'variable name'); // exception!
Assert::isIntOrFloat('1', 'variable name'); // exception!
Assert::isFloat(0.5, 'variable name'); // no exception
Assert::isFloat('0.5', 'variable name'); // exception!
Assert::isValidArrayIndex(1, 'variable name'); // no exception
Assert::isValidArrayIndex('some string', 'variable name'); // no exception
Assert::isValidArrayIndex(null, 'variable name'); // no exception
Assert::isValidArrayIndex(0.5, 'variable name'); // no exception
Assert::isValidArrayIndex(-1, 'variable name'); // no exception
Assert::isValidArrayIndex(true, 'variable name'); // no exception
Assert::isValidArrayIndex(false, 'variable name'); // no exception
Assert::isValidArrayIndex([], 'variable name'); // exception!
Assert::isValidArrayIndex(new stdClass(), 'variable name'); // exception!
Assert::isArray([], 'variable name'); // no exception
Assert::isArray(1, 'variable name'); // exception!
Assert::isNumeric(1, 'variable name'); // no exception
Assert::isNumeric(1.5, 'variable name'); // no exception
Assert::isNumeric('1.5', 'variable name'); // no exception
Assert::isNumeric(INF, 'variable name'); // no exception
Assert::isNumeric([], 'variable name'); // exception!
Assert::isNumeric(true, 'variable name'); // exception!
class SomeClass {}
Assert::isType(new SomeClass(), SomeClass::class, 'variable name'); // no exception
Assert::isType(null, SomeClass::class, 'variable name'); // exception!
class SomeClass {}
Assert::isOneOfTypes(new SomeClass(), [SomeClass::class], 'variable_name'); // no exception
Assert::isOneOfTypes(new stdClass(), [SomeClass::class], 'variable_name'); // exception!
interface SomeInterface {}
class SomeClass implements SomeInterface {}
Assert::isImplements(new SomeClass(), 'someinterface'); // no exception
Assert::isImplements('someclass', 'someinterface'); // no exception
Assert::isImplements(new stdClass(), 'someinterface'); // exception!
Assert::isImplements(new stdClass(), 'someclass'); // exception!
class SomeClass {}
class ChildClass extends SomeClass {}
Assert::isChildOf('childclass', 'someclass', 'variable name'); // no exception
Assert::isChildOf(new ChildClass(), 'someclass', 'variable name'); // no exception
Assert::isChildOf(new ChildClass(), SomeClass::class, 'variable name'); // no exception
Assert::isChildOf(new SomeClass(), SomeClass::class, 'variable name'); // exception!
interface SomeInterface {}
interface ChildInterface {}
class SomeClass implements SomeInterface {}
Assert::isChildOf('someclass', 'someinterface', 'variable name'); // no exception
Assert::isChildOf('childinterface', 'someinterface', 'variable name'); // no exception
Assert::isChildOf('someinterface', 'someinterface', 'variable name'); // exception!
Assert::isNotEmptyString('some string', false, 'variable name'); // no exception
Assert::isNotEmptyString(' ', true, 'variable name'); // no exception
Assert::isNotEmptyString(' ', false, 'variable name'); // exception!
Assert::isNotEmptyString('', true, 'variable name'); // exception!
Assert::isNotEmptyString('', false, 'variable name'); // exception!
Assert::isNotEmptyString(0, false, 'variable name'); // exception!
Assert::startsWith('string', 's', 'variable name'); // no exception
Assert::startsWith('string', 'str', 'variable name'); // no exception
Assert::startsWith(' string', ' ', 'variable name'); // no exception
Assert::startsWith('string', '', 'variable name'); // no exception (empty string never throws)
Assert::startsWith('string', 'a', 'variable name'); // exception!
Assert::startsWith('string', 'S', 'variable name'); // exception!
Assert::isRegexMatches('string', '/[w]+/', 'variable name'); // no exception
Assert::isRegexMatches('string', '/[d]+/', 'variable name'); // exception!
Assert::isRegexMatches('string', 'string', 'variable name'); // exception! (invalid regex pattern)
Assert::isRegexPattern('/regex/', 'variable name'); // no exception
Assert::isRegexPattern('/\w/', 'variable name'); // no exception
Assert::isRegexPattern('//', 'variable name'); // no exception
Assert::isRegexPattern('string', 'variable name'); // exception!
Assert::isInStringArray('string', ['abcd', 'string']); // no exception
Assert::isInStringArray('', ['']); // no exception
Assert::isInStringArray('abcd', ['string']); // exception!
Assert::isNotEmptyArray(['1'], 'variable name'); // no exception
Assert::isNotEmptyArray([], 'variable name'); // exception!
class SomeClass() {}
Assert::isArrayOfType([new SomeClass()], 'someclass', 'variable name'); // no exception
Assert::isArrayOfType([], 'someclass', 'variable name'); // no exception
Assert::isArrayOfType([new SomeClass(), 'string'], 'someclass', 'variable name'); // exception!
Assert::isArrayOfType([], 'variable name'); // no exception
Assert::isArrayOfType(['a', 'b', 'c'], 'variable name'); // no exception
Assert::isArrayOfType(['string', null], 'someclass', 'variable name'); // exception!
Assert::isSequentialArray(['a', 'z', 99], false, 'variable name'); // no exception
Assert::isSequentialArray(['a', 'z', 99], true, 'variable name'); // no exception
Assert::isSequentialArray([], true, 'variable name'); // no exception
Assert::isSequentialArray(['a' => 0, 'b' => 1], true, 'variable name'); // exception!
Assert::isPositiveInt(1, 'variable name'); // no exception
Assert::isPositiveInt(-1, 'variable name'); // exception!
Assert::isPositiveInt(INF, 'variable name'); // exception!
Assert::isNotNegativeInt(1, 'variable name'); // no exception
Assert::isNotNegativeInt(0, 'variable name'); // no exception
Assert::isPositiveInt(-1, 'variable name'); // exception!
Assert::isPositiveInt(INF, 'variable name'); // exception!
Assert::isPositiveInt('string', 'variable name'); // exception!
Assert::isPositiveNumber(1, false, 'variable name'); // no exception
Assert::isPositiveNumber(1, true, 'variable name'); // no exception
Assert::isPositiveNumber('1', true, 'variable name'); // no exception
Assert::isPositiveNumber('1', false, 'variable name'); // exception!
Assert::isPositiveNumber(-1, false, 'variable name'); // exception!
Assert::isNotNegativeNumber(1, false, 'variable name'); // no exception
Assert::isNotNegativeNumber(1, true, 'variable name'); // no exception
Assert::isNotNegativeNumber('1', true, 'variable name'); // no exception
Assert::isNotNegativeNumber(0, false, 'variable name'); // no exception
Assert::isNotNegativeNumber('0', true, 'variable name'); // no exception
Assert::isNotNegativeNumber('0', false, 'variable name'); // exception!
Assert::isNotNegativeNumber('1', false, 'variable name'); // exception!
Assert::isNotNegativeNumber(-1, false, 'variable name'); // exception!
Assert::isNotNegativeNumber('string', false, 'variable name'); // exception!
function divideIntegers($dividend, $divisor) {
Assert::isInt($dividend);
Assert::isInt($divisor);
return intdiv($dividend, $divisor);
}
divideIntegers(5, '2');
function divideIntegers($dividend, $divisor) {
Assert::isInt($dividend, 'dividend'); // <- name is provided
Assert::isInt($divisor, 'divisor'); // <- name is provided
return intdiv($dividend, $divisor);
}
divideIntegers(5, '2');
static::throwException($name, $expected, $given);
class MyCustomAssertionException extends \Exception implements AssertionExceptionInterface
{
}
class MyCustomAssert extends \Fabstract\Component\Assert\Assert
{
protected static function generateException($name, $expected, $given)
{
$exception = parent::generateException($name, $expected, $given);
return new MyCustomAssertionException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
}
function divideIntegers($dividend, $divisor) {
MyCustomAssert::isInt($dividend, 'dividend');
MyCustomAssert::isInt($divisor, 'divisor');
return intdiv($dividend, $divisor);
}
divideIntegers(5, '2');
try {
$app->run();
} catch (LoggerLibraryAssertionException $exception) {
// do something related to logger library
} catch (DateTimeLibraryAssertionException $exception) {
// do something related to datetime library
} catch (AssertionExceptionInterface $exception) {
// none of above
}