Security

Reference for the Security module: type and value checks used in place of plain assertions when the check must stay active even when running online in release mode. Each check validates its input, throws on failure, and returns the value on success so it can be used inline. Every function is listed with its signature, parameters and return type.

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

Classes
  • Security - type and value checks that throw on failure and return the value on success.

Security

Static check functions covering strings, numbers, arrays, ids, urls, filenames, hex hashes, class instances and more. A check validates its argument, calls Fail (which throws) when the argument is not valid, and returns the validated value otherwise.

Constants

ConstantValueDescription
Security::ASSERT_ENABLEDtrueFlag controlling whether assertions are enabled.

Any

Security::Any($value): mixed

Accept any value and return it unchanged.

NameTypeDescription
$valuemixedThe value to pass through.

Returns mixed The value unchanged.

Arr

Security::Arr(&$array, callable $check_function = null)

Check the value is an array; with a check function, apply it to each item of a zero-indexed list and return the results.

NameTypeDescription
$arraymixedThe value that must be an array.
$check_functioncallableOptional function applied to each item; keys must be sequential from zero.

Returns The array, or the array of checked items when a check function is given.

ArrOrNull

Security::ArrOrNull(&$array = null, callable $check_function = null)

Check the value is an array, or leave it when it is null.

NameTypeDescription
$arraymixedThe array to check, or null.
$check_functioncallableOptional function applied to each item.

Returns The array, or null.

ArrayDataOf

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

Check every item of an ArrayData is an instance of the named class.

NameTypeDescription
$arrayArrayDataThe collection to check.
$class_namestringThe class every item must be an instance of.

Returns The ArrayData.

ArrayDataOfOrNull

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

Check every item of an ArrayData is an instance of the named class, or leave it when it is null.

NameTypeDescription
$arrayArrayDataThe collection to check, or null.
$class_namestringThe class every item must be an instance of.

Returns The ArrayData, or null.

ArrayOf

Security::ArrayOf($class_name)

Return a check function that checks a value is an array whose every item is an instance of the named class.

NameTypeDescription
$class_namestringThe class every item must be an instance of.

Returns A closure that checks and returns the array.

ArrayOfArray

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

Check the value is an array whose every item is an array, optionally each with a fixed number of items.

NameTypeDescription
$arrayarrayThe array of arrays to check.
$count_items_in_subarraysintOptional required item count for each sub-array.

Returns The array.

ArrayOfFilePath

Security::ArrayOfFilePath(&$array, $allow_files_starting_with_dot = false, $elementCount = null)

Check the value is an array of file paths, optionally of a fixed length.

NameTypeDescription
$arrayarrayThe array of file paths to check.
$allow_files_starting_with_dotboolWhether to allow file names beginning with a dot.
$elementCountintOptional required number of elements.

Returns The array.

ArrayOfFloat

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

Check the value is an array of floats, each within an optional range, optionally of a fixed length.

NameTypeDescription
$arrayarrayThe array of floats to check.
$minfloatOptional minimum for each value.
$maxfloatOptional maximum for each value.
$elementCountintOptional required number of elements.

Returns The array.

ArrayOfID

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

Check the value is an array of ids, optionally of a fixed length.

NameTypeDescription
$arrayarrayThe array of ids to check.
$allow_zeroboolWhether zero is a valid id.
$elementCountintOptional required number of elements.

Returns The array.

ArrayOfNum

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

Check the value is an array of integers, each within an optional range, optionally of a fixed length.

NameTypeDescription
$arrayarrayThe array of numbers to check.
$minintOptional minimum for each value.
$maxintOptional maximum for each value.
$allow_numerical_stringboolWhether numeric strings are accepted as numbers.
$elementCountintOptional required number of elements.

Returns The array.

ArrayOfObject

Security::ArrayOfObject(array &$array)

Check the value is an array whose every item is an object.

NameTypeDescription
$arrayarrayThe array of objects to check.

Returns The array.

ArrayOfStr

Security::ArrayOfStr(&$array)

Check the value is an array whose every item is a string.

NameTypeDescription
$arrayarrayThe array of strings to check.

Returns The array.

ArrayOfStrOrNum

Security::ArrayOfStrOrNum(&$array)

Check the value is an array whose every item is a string or a number.

NameTypeDescription
$arrayarrayThe array of strings or numbers to check.

Returns The array.

ArrayOfUniqueStr

Security::ArrayOfUniqueStr(array &$array)

Check the value is an array of strings with no repeated value.

NameTypeDescription
$arrayarrayThe array of strings that must all be unique.

Returns The array.

ArrayWithFields

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

Check an array contains each of a comma-separated list of keys.

NameTypeDescription
$arrayarrayThe array to check.
$fieldsstringA comma-separated list of required field names.
$allow_extra_fields_not_in_listboolWhether extra keys beyond the list are allowed.

Returns The array.

Callback

Security::Callback($callback)

Check the value is callable.

NameTypeDescription
$callbackmixedThe value that must be callable.

Returns The callback.

CallbackOrNull

Security::CallbackOrNull($callback)

Check the value is callable, or return null when it is null.

NameTypeDescription
$callbackmixedThe callable to check, or null.

Returns The callback, or null.

Check

Security::Check($condition, $failure_message = '')

Fail with the message when the boolean condition is not true.

NameTypeDescription
$conditionboolThe condition that must be true.
$failure_messagestringThe message used on failure; a backtrace is used when blank.

Returns true when the condition holds.

DatabaseName

Security::DatabaseName($name, $allow_all_numeric = false, $allow_blank = false, $allow_dots = false)

Check the value is a safe database identifier, lowercased, of letters, digits and underscores.

NameTypeDescription
$namestringThe name to check.
$allow_all_numericboolWhether an all-numeric name is allowed.
$allow_blankboolWhether an empty name is allowed.
$allow_dotsboolWhether dot-separated parts are allowed, each checked separately.

Returns The lowercased name.

DatabaseNameOrNull

Security::DatabaseNameOrNull(&$name)

Check the value is a safe database identifier, or leave it when it is null.

NameTypeDescription
$namestringThe name to check, or null.

Returns The name, or null.

Date

Security::Date(&$date)

Check the value is a valid date.

NameTypeDescription
$datemixedThe value that must be a date.

Returns The date.

DateOrNull

Security::DateOrNull(&$date)

Check the value is a valid date, or leave it when it is null.

NameTypeDescription
$datemixedThe value that must be a date, or null.

Returns The date, or null.

DictionaryOf

Security::DictionaryOf($value_type)

Return a check function that checks an array is a string-keyed map whose values are all a string or an instance of the named class.

NameTypeDescription
$value_typestringThe value type: 'string', 'str', or a class name.

Returns A closure that checks and returns the map.

DomainName

Security::DomainName(&$name)

Check the value is a valid domain name.

NameTypeDescription
$namestringThe domain name to check.

Returns The domain name.

DomainNameOrNull

Security::DomainNameOrNull(&$domain)

Check the value is a valid domain name, or leave it when it is null.

NameTypeDescription
$domainstringThe domain name to check, or null.

Returns The domain name, or null.

Each

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

Check every item of an array passes the given check function.

NameTypeDescription
$arrayarrayThe array to check.
$check_functionClosureA function returning true for a valid item.

Returns The array.

EmailAddress

Security::EmailAddress(&$email)

Check the value is a valid email address.

NameTypeDescription
$emailstringThe email address to check.

Returns The email address.

EmailAddressOrNull

Security::EmailAddressOrNull(&$email)

Check the value is a valid email address, or leave it when it is null.

NameTypeDescription
$emailstringThe email address to check, or null.

Returns The email address, or null.

Equal

Security::Equal($a, $b)

Check two values are equal, comparing by JSON when they are not identical.

NameTypeDescription
$amixedThe first value.
$bmixedThe value it must equal.

Returns The first value.

Fail

Security::Fail($msg)

Throw an exception with the given message.

NameTypeDescription
$msgstringThe failure message for the thrown exception.

Returns Nothing; always throws.

Filename

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

Check the value is a safe file name.

NameTypeDescription
$namestringThe file name to check.
$allow_files_starting_with_dotboolWhether a name beginning with a dot is allowed.

Returns The file name.

FilenameOrNull

Security::FilenameOrNull(&$name)

Check the value is a safe file name, or leave it when it is null.

NameTypeDescription
$namestringThe file name to check, or null.

Returns The file name, or null.

FilePath

Security::FilePath(&$name)

Check the value is a safe file path, normalising backslashes to forward slashes.

NameTypeDescription
$namestringThe file path to check.

Returns The normalised file path.

Float

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

Check the value is a float within an optional range.

NameTypeDescription
$floatfloatThe value that must be a float.
$minfloatOptional minimum.
$maxfloatOptional maximum.

Returns The float.

FloatOrNull

Security::FloatOrNull($float)

Check the value is a float, or leave it when it is null.

NameTypeDescription
$floatfloatThe value that must be a float, or null.

Returns The float, or null.

GDImage

Security::GDImage($image)

Check the value is a GD image resource.

NameTypeDescription
$imageresourceThe value that must be a GD image resource.

Returns The image resource.

Hex32

Security::Hex32(&$str)

Check the value is a 32-character lowercase hexadecimal string.

NameTypeDescription
$strstringThe hex string to check.

Returns The string.

Hex32OrNull

Security::Hex32OrNull(&$str)

Check the value is a 32-character hex string, or leave it when it is null.

NameTypeDescription
$strstringThe hex string to check, or null.

Returns The string, or null.

Hex64

Security::Hex64(&$str)

Check the value is a 64-character lowercase hexadecimal string.

NameTypeDescription
$strstringThe hex string to check.

Returns The string.

Hex64OrNull

Security::Hex64OrNull(&$str)

Check the value is a 64-character hex string, or leave it when it is null.

NameTypeDescription
$strstringThe hex string to check, or null.

Returns The string, or null.

HtmlAttributeName

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

Check the value is a valid HTML attribute name, optionally allowing a single colon separator.

NameTypeDescription
$namestringThe attribute name to check.
$allow_colonboolWhether a colon-separated name is allowed.

Returns The attribute name.

ID

Security::ID($id, $allow_zero = false)

Check the value is a non-negative integer id.

NameTypeDescription
$idintThe id to check.
$allow_zeroboolWhether zero is a valid id.

Returns The id.

IDOrNull

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

Check the value is a valid id, or leave it when it is null.

NameTypeDescription
$idintThe id to check, or null.
$allow_zeroboolWhether zero is a valid id.

Returns The id, or null.

InstanceOrNull

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

Check the value is an instance of the named class, or leave it when it is null.

NameTypeDescription
$itemobjectThe value to check, or null.
$class_namestringThe class the value must be an instance of.

Returns The item, or null.

Instance_Of

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

Check the value is an instance of the named class.

NameTypeDescription
$itemobjectThe value to check.
$class_namestringThe class the value must be an instance of.

Returns The item.

Is

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

Check the value is a non-null instance of the named class.

NameTypeDescription
$itemobjectThe value to check.
$class_namestringThe class the value must be an instance of.

Returns The item.

NotEqual

Security::NotEqual($a, $b)

Check two values are not equal, comparing by JSON.

NameTypeDescription
$amixedThe first value.
$bmixedThe value it must not equal.

Returns The first value.

NotNull

Security::NotNull($value)

Check the value is not null.

NameTypeDescription
$valuemixedThe value that must not be null.

Returns The value.

Null

Security::Null($a)

Check the value is null.

NameTypeDescription
$amixedThe value that must be null.

Returns The value.

Num

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

Check the value is an integer within an optional range, optionally accepting numeric strings.

NameTypeDescription
$numintThe value that must be an integer.
$minintOptional minimum.
$maxintOptional maximum.
$allow_numerical_stringboolWhether a numeric string is accepted and converted.

Returns The integer.

NumOrNull

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

Check the value is an integer within an optional range, or leave it when it is null.

NameTypeDescription
$numintThe value that must be an integer, or null.
$minintOptional minimum.
$maxintOptional maximum.

Returns The integer, or null.

Object

Security::Object()

Return a check function that checks a value is an object.

Returns A closure that checks and returns the object.

OneOf

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

Check a string is one of a set of allowed values, given as an array or a comma-separated string.

NameTypeDescription
$valuestringThe string that must be in the allowed set.
$allowed_valuesarray|stringThe allowed values, as an array or comma-separated string.

Returns The value.

OneOfNum

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

Check a number is one of a set of allowed values, given as an array or a comma-separated string.

NameTypeDescription
$valueintThe number that must be in the allowed set.
$allowed_valuesarray|stringThe allowed values, as an array or comma-separated string.

Returns The value.

PHPClassName

Security::PHPClassName($name)

Check the value is a valid PHP class name.

NameTypeDescription
$namestringThe class name to check.

Returns The class name.

PhoneNumber

Security::PhoneNumber(&$phone)

Check the value is a phone number in E.164 (+xxxxx) or 00-prefixed form.

NameTypeDescription
$phonestringThe phone number to check.

Returns The phone number.

PhoneNumberOrNull

Security::PhoneNumberOrNull(&$phone)

Check the value is a valid phone number, or leave it when it is null.

NameTypeDescription
$phonestringThe phone number to check, or null.

Returns The phone number, or null.

Sha1

Security::Sha1(&$str)

Check the value is a 40-character lowercase hexadecimal SHA-1 string.

NameTypeDescription
$strstringThe hex string to check.

Returns The string.

StdClass

Security::StdClass($value): stdClass

Check the value is a stdClass object.

NameTypeDescription
$valuemixedThe value that must be a stdClass.

Returns stdClass The object.

StdClassProperty

Security::StdClassProperty($stdClass, $propertyName): mixed

Check a stdClass has the named property and return its value.

NameTypeDescription
$stdClassstdClassThe object to read.
$propertyNamestringThe property that must exist.

Returns mixed The property value.

StorageBucketName

Security::StorageBucketName($name)

Check the value is a valid storage bucket name of lowercase letters, digits, underscores and hyphens.

NameTypeDescription
$namestringThe bucket name to check.

Returns The lowercased bucket name.

Str

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

Check the value is a string, optionally restricted to allowed characters, a maximum length, and non-blank.

NameTypeDescription
$stringstringThe value that must be a string; integers are accepted and cast.
$allowed_charsstringOptional set of characters the string may contain.
$allow_blankboolWhether an empty string is allowed.
$maxLengthintOptional maximum length.

Returns string The string.

StrContaining

Security::StrContaining($str, $substr)

Check a string contains the given substring.

NameTypeDescription
$strstringThe string to check.
$substrstringThe substring it must contain.

Returns The string.

StrEndsWith

Security::StrEndsWith($str, $ends_with)

Check a string ends with the given suffix.

NameTypeDescription
$strstringThe string to check.
$ends_withstringThe suffix it must end with.

Returns The string.

StrOrArray

Security::StrOrArray(&$value)

Check the value is a string or an array.

NameTypeDescription
$valuemixedThe value that must be a string or array.

Returns The value.

StrOrNull

Security::StrOrNull(&$string)

Check the value is a string, or leave it when it is null.

NameTypeDescription
$stringstringThe value that must be a string, or null.

Returns The string, or null.

StrOrNum

Security::StrOrNum(&$string_or_number)

Check the value is a string or a number.

NameTypeDescription
$string_or_numbermixedThe value that must be a string or number.

Returns The value.

TrueFalse

Security::TrueFalse(&$bool)

Check the value is a boolean true or false.

NameTypeDescription
$boolboolThe value that must be true or false.

Returns true or false.

TrueFalseOrNull

Security::TrueFalseOrNull($value)

Check the value is true, false or null.

NameTypeDescription
$valueboolThe value that must be true, false or null.

Returns The value.

Url

Security::Url($url)

Check the value is a valid url.

NameTypeDescription
$urlstringThe url to check.

Returns The url.

UrlName

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

Check the value is a non-empty url name segment, optionally lowercased.

NameTypeDescription
$namestringThe url name to check.
$lower_caseboolWhether the name must be lowercase.

Returns The url name.

UrlNameOrNull

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

Check the value is a valid url name, or leave it when it is null.

NameTypeDescription
$namestringThe url name to check, or null.
$lower_caseboolWhether the name must be lowercase.

Returns The url name, or null.

UrlOrNull

Security::UrlOrNull(&$url)

Check the value is a valid url, or return null when it is null.

NameTypeDescription
$urlstringThe url to check, or null.

Returns The url, or null.

UrlPath

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

Check the value is a safe url path, optionally with a query string, optionally lowercased.

NameTypeDescription
$pathstringThe url path to check.
$lower_caseboolWhether to lowercase the path.

Returns The url path.

UrlPathOrNull

Security::UrlPathOrNull(&$url_path)

Check the value is a safe url path, or leave it when it is null.

NameTypeDescription
$url_pathstringThe url path to check, or null.

Returns The url path, or null.