Assert

Guard functions that verify a value has an expected type or form. Each static function takes a value and throws an exception when the check fails, so callers can state their assumptions up front and stop bad input from travelling further.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes
  • Assert - static guard functions that check a value and throw when it fails.

Assert

The main class: static functions that each check a value against an expected type or form and throw an exception when the check fails, listed alphabetically. When the ASSERT_ENABLED constant is false the checks return without doing anything.

Constants

ConstantValueDescription
Assert::ASSERT_ENABLEDtrueWhether assertions are enabled; when false each check returns without acting.

Arr

Assert::Arr(&$array, $check_function = null)

Assert that the value is an array, and optionally that every item passes the given check callback called with the key and value.

NameTypeDescription
&$arrayThe value to check is an array.
$check_functionAn optional callback called with each key and value; each must return true.

ArrOrNull

Assert::ArrOrNull(&$array = null, $check_function = null)

Assert that the value is null or an array.

NameTypeDescription
&$arrayThe value to check is null or an array.
$check_functionAn optional check callback.

ArrayDataOf

Assert::ArrayDataOf(ArrayData $array, $class_name)

Assert that every item in an ArrayData is an instance of the named class.

NameTypeDescription
$arrayArrayDataThe ArrayData whose items are checked.
$class_nameThe class name every item must be an instance of.

ArrayDataOfOrNull

Assert::ArrayDataOfOrNull(ArrayData $array = null, $class_name)

Assert that the ArrayData is null, or that every item is an instance of the named class.

NameTypeDescription
$arrayArrayDataThe ArrayData whose items are checked, or null.
$class_nameThe class name every item must be an instance of.

ArrayOf

Assert::ArrayOf(&$array, $class_name)

Assert that the value is an array and every item is an instance of the named class.

NameTypeDescription
&$arrayThe array whose items are checked.
$class_nameThe class name every item must be an instance of.

ArrayOfArray

Assert::ArrayOfArray(array &$array, $count_items_in_subarrays = null)

Assert that the value is an array whose every item is itself an array, optionally each with a fixed item count.

NameTypeDescription
&$arrayarrayThe array whose items must each be arrays.
$count_items_in_subarraysAn optional required item count for each sub-array.

ArrayOfFloat

Assert::ArrayOfFloat(&$array, $min = null, $max = null, $elementCount = null)

Assert that the value is an array of floats, optionally within a range and of a fixed length.

NameTypeDescription
&$arrayThe array of floats to check.
$minAn optional minimum each value must not be below.
$maxAn optional maximum each value must not exceed.
$elementCountAn optional exact number of elements.

ArrayOfID

Assert::ArrayOfID(&$array, $allow_zero = false, $elementCount = null)

Assert that the value is an array of ids, optionally allowing zero and requiring a fixed length.

NameTypeDescription
&$arrayThe array of ids to check.
$allow_zeroWhether zero is a permitted id value.
$elementCountAn optional exact number of elements.

ArrayOfNum

Assert::ArrayOfNum(&$array, $min = null, $max = null, $allow_numerical_string = false, $elementCount = null)

Assert that the value is an array of numbers, optionally within a range, allowing numeric strings, and of a fixed length.

NameTypeDescription
&$arrayThe array of numbers to check.
$minAn optional minimum each value must not be below.
$maxAn optional maximum each value must not exceed.
$allow_numerical_stringWhether numeric strings are accepted as numbers.
$elementCountAn optional exact number of elements.

ArrayOfStr

Assert::ArrayOfStr(&$array)

Assert that the value is an array and every item is a string.

NameTypeDescription
&$arrayThe array of strings to check.

ArrayOfStrOrNum

Assert::ArrayOfStrOrNum(&$array)

Assert that the value is an array and every item is a string or a number.

NameTypeDescription
&$arrayThe array of strings or numbers to check.

ArrayOfUniqueStr

Assert::ArrayOfUniqueStr(array &$array)

Assert that the value is an array of strings with no duplicate items.

NameTypeDescription
&$arrayarrayThe array of unique strings to check.

ArrayWithFields

Assert::ArrayWithFields(array &$array, $fields, $allow_extra_fields_not_in_list = false)

Assert that the array contains each of the named comma-separated fields, optionally allowing extra fields not in the list.

NameTypeDescription
&$arrayarrayThe array to check for the required fields.
$fieldsA comma-separated list of field names that must be present.
$allow_extra_fields_not_in_listWhether fields beyond the list are permitted.

Check

Assert::Check($bool, $failure_message = '')

Assert that a boolean flag is true, throwing with the given failure message when it is not.

NameTypeDescription
$boolThe boolean flag that must be true.
$failure_messageA message included in the thrown exception on failure.

DatabaseName

Assert::DatabaseName($name, $allow_all_numeric = false, $allow_blank = false)

Assert that the value is a valid database name of lower-case letters, digits and underscores, not starting with a digit.

NameTypeDescription
$nameThe database name to check.
$allow_all_numericWhether an all-numeric name is permitted.
$allow_blankWhether an empty name is permitted.

DatabaseNameOrNull

Assert::DatabaseNameOrNull(&$name)

Assert that the value is null or a valid database name.

NameTypeDescription
&$nameThe database name to check, or null.

Date

Assert::Date(&$date)

Assert that the value is a date.

NameTypeDescription
&$dateThe value to check is a date.

DateOrNull

Assert::DateOrNull(&$date)

Assert that the value is null or a date.

NameTypeDescription
&$dateThe value to check is null or a date.

DomainName

Assert::DomainName(&$name)

Assert that the value is a valid domain name.

NameTypeDescription
&$nameThe domain name to check.

DomainNameOrNull

Assert::DomainNameOrNull(&$domain)

Assert that the value is null or a valid domain name.

NameTypeDescription
&$domainThe domain name to check, or null.

Each

Assert::Each(array &$array, Closure $check_function)

Assert that the value is an array and every item passes the given check closure.

NameTypeDescription
&$arrayarrayThe array whose items are checked.
$check_functionClosureA closure called with each item that must return true.

EmailAddress

Assert::EmailAddress(string &$email)

Assert that the value is a valid email address.

NameTypeDescription
&$emailstringThe email address to check.

FilePath

Assert::FilePath(&$name)

Assert that the value is a string that matches a normalised file path.

NameTypeDescription
&$nameThe file path to check.

Filename

Assert::Filename(&$name, $allow_files_starting_with_dot = false)

Assert that the value is a valid filename, optionally allowing names that start with a dot.

NameTypeDescription
&$nameThe filename to check.
$allow_files_starting_with_dotWhether a leading dot is permitted.

FilenameOrNull

Assert::FilenameOrNull(&$name)

Assert that the value is null or a valid filename.

NameTypeDescription
&$nameThe filename to check, or null.

Float

Assert::Float($float, $min = null, $max = null)

Assert that the value is a float, optionally within a minimum and maximum range.

NameTypeDescription
$floatThe value to check is a float.
$minAn optional minimum the value must not be below.
$maxAn optional maximum the value must not exceed.

FloatOrNull

Assert::FloatOrNull($float)

Assert that the value is null or a float.

NameTypeDescription
$floatThe value to check is null or a float.

GDImage

Assert::GDImage($image)

Assert that the value is a GD image resource.

NameTypeDescription
$imageThe value to check is a GD image resource.

HtmlAttributeName

Assert::HtmlAttributeName($name, $allow_colon = false)

Assert that the value is a valid HTML attribute name, optionally allowing a single well-formed colon.

NameTypeDescription
$nameThe HTML attribute name to check.
$allow_colonWhether a colon separating two non-empty parts is permitted.

ID

Assert::ID(&$id, $allow_zero = false)

Assert that the value is a non-negative id number, optionally allowing zero.

NameTypeDescription
&$idThe id to check.
$allow_zeroWhether zero is a permitted id value.

IDOrNull

Assert::IDOrNull(&$id, $allow_zero = false)

Assert that the value is null or a valid id.

NameTypeDescription
&$idThe id to check, or null.
$allow_zeroWhether zero is a permitted id value.

Instance_Of

Assert::Instance_Of(&$item, $class_name)

Assert that the value is an instance of the named class.

NameTypeDescription
&$itemThe value to check.
$class_nameThe class name the value must be an instance of.

InstanceOrNull

Assert::InstanceOrNull(&$item, $class_name)

Assert that the value is null or an instance of the named class.

NameTypeDescription
&$itemThe value to check, or null.
$class_nameThe class name the value must be an instance of.

Is

Assert::Is(&$item, $class_name)

Assert that the value is an instance of the named class, where the class name is a valid database name.

NameTypeDescription
&$itemThe value to check.
$class_nameThe class name the value must be an instance of.

NotNull

Assert::NotNull($value)

Assert that the value is not null.

NameTypeDescription
$valueThe value that must not be null.

Num

Assert::Num($num, $min = null, $max = null, $allow_numerical_string = false)

Assert that the value is an integer, optionally within a range and optionally allowing a numeric string.

NameTypeDescription
$numThe value to check is an integer.
$minAn optional minimum the value must not be below.
$maxAn optional maximum the value must not exceed.
$allow_numerical_stringWhether a numeric string is accepted and converted to an integer.

NumOrNull

Assert::NumOrNull(&$num, $min = null, $max = null)

Assert that the value is null or an integer, optionally within a range.

NameTypeDescription
&$numThe value to check is null or an integer.
$minAn optional minimum the value must not be below.
$maxAn optional maximum the value must not exceed.

Obj

Assert::Obj(&$value)

Assert that the value is an object.

NameTypeDescription
&$valueThe value to check is an object.

ObjOrNull

Assert::ObjOrNull(&$value)

Assert that the value is null or an object.

NameTypeDescription
&$valueThe value to check is null or an object.

OneOf

Assert::OneOf(&$value, $allowed_values)

Assert that the string value is one of the allowed values, given as an array or a comma-separated string.

NameTypeDescription
&$valueThe string value that must appear in the allowed list.
$allowed_valuesAn array or comma-separated string of allowed values.

OneOfNum

Assert::OneOfNum(&$value, $allowed_values)

Assert that the numeric value is one of the allowed values, given as an array or a comma-separated string.

NameTypeDescription
&$valueThe numeric value that must appear in the allowed list.
$allowed_valuesAn array or comma-separated string of allowed values.

OptionalIs

Assert::OptionalIs(&$item, $class_name)

Assert that the value is null, or an instance of the named class.

NameTypeDescription
&$itemThe value to check, or null.
$class_nameThe class name the value must be an instance of.

PHPClassName

Assert::PHPClassName($name)

Assert that the value is a valid PHP class name, checked as a database name.

NameTypeDescription
$nameThe class name to check.

Sha1

Assert::Sha1(&$str)

Assert that the value is a 40-character lower-case hexadecimal SHA1 string.

NameTypeDescription
&$strThe SHA1 string to check.

Str

Assert::Str(&$string, $allowed_chars = null, $allow_blank = true, $maxLength = null)

Assert that the value is a string (or an integer), optionally restricted to a set of allowed characters, optionally non-blank, and optionally within a maximum length.

NameTypeDescription
&$stringThe value to check is a string.
$allowed_charsAn optional set of characters the string may contain.
$allow_blankWhether an empty string is permitted.
$maxLengthAn optional maximum length.

StrContaining

Assert::StrContaining($str, $substr)

Assert that the string contains the given substring.

NameTypeDescription
$strThe string to search within.
$substrThe substring that must be present.

StrEndsWith

Assert::StrEndsWith($str, $ends_with)

Assert that the string ends with the given suffix.

NameTypeDescription
$strThe string to check.
$ends_withThe suffix the string must end with.

StrOrArray

Assert::StrOrArray(&$value)

Assert that the value is a string or an array.

NameTypeDescription
&$valueThe value to check is a string or an array.

StrOrNull

Assert::StrOrNull(&$string)

Assert that the value is null or a string.

NameTypeDescription
&$stringThe value to check is null or a string.

StrOrNum

Assert::StrOrNum(&$string_or_number)

Assert that the value is a string or a number.

NameTypeDescription
&$string_or_numberThe value to check is a string or a number.

ThrowException

Assert::ThrowException($message = 'EXCEPTION (UNSPECIFIED ERROR)')

Throw an exception with the given message.

NameTypeDescription
$messageThe message for the thrown exception.

TrueFalse

Assert::TrueFalse(&$bool)

Assert that the value is exactly true or false.

NameTypeDescription
&$boolThe value to check is a boolean.

TrueFalseOrNull

Assert::TrueFalseOrNull($value)

Assert that the value is null, true or false.

NameTypeDescription
$valueThe value to check is null or a boolean.

Url

Assert::Url(&$url)

Assert that the value is a valid URL.

NameTypeDescription
&$urlThe URL to check.

UrlName

Assert::UrlName(&$name, $lower_case = false)

Assert that the value is a non-empty valid URL name, optionally required to be lower case.

NameTypeDescription
&$nameThe URL name to check.
$lower_caseWhether the name must be lower case.

UrlNameOrNull

Assert::UrlNameOrNull(&$name, $lower_case = false)

Assert that the value is null or a valid URL name.

NameTypeDescription
&$nameThe URL name to check, or null.
$lower_caseWhether the name must be lower case.

UrlOrNull

Assert::UrlOrNull(&$url)

Assert that the value is null or a valid URL.

NameTypeDescription
&$urlThe URL to check, or null.

UrlPath

Assert::UrlPath(&$path, $lower_case = false)

Assert that the value is a valid URL path, optionally required to be lower case.

NameTypeDescription
&$pathThe URL path to check.
$lower_caseWhether the path must be lower case.

UrlPathOrNull

Assert::UrlPathOrNull(&$url_path)

Assert that the value is null or a valid URL path.

NameTypeDescription
&$url_pathThe URL path to check, or null.