class Inspector (View source)

Generic inspections for the assert() statement.

This is a static function collection for inspecting variable contents. All functions in this collection check a variable against an assertion about its structure.

Example call:

Methods

static bool
assertTraversable(mixed $traversable)

Asserts argument can be traversed with foreach.

static bool
assertAll(callable $callable, mixed $traversable)

Asserts callback returns TRUE for each member of a traversable.

static bool
assertAllStrings(mixed $traversable)

Asserts that all members are strings.

static bool
assertAllStringable(mixed $traversable)

Asserts all members are strings or objects with magic __toString() method.

static bool
assertStringable(mixed $string)

Asserts argument is a string or an object castable to a string.

static bool
assertAllArrays(mixed $traversable)

Asserts that all members are arrays.

static bool
assertStrictArray(mixed $array)

Asserts that the array is strict.

static bool
assertAllStrictArrays(mixed $traversable)

Asserts all members are strict arrays.

static bool
assertAllHaveKey(mixed $traversable)

Asserts all given keys exist in every member array.

static bool
assertAllIntegers(mixed $traversable)

Asserts that all members are integer values.

static bool
assertAllFloat(mixed $traversable)

Asserts that all members are float values.

static bool
assertAllCallable(mixed $traversable)

Asserts that all members are callable.

static bool
assertAllNotEmpty(mixed $traversable)

Asserts that all members are not empty.

static bool
assertAllNumeric(mixed $traversable)

Asserts all members are numeric data types or strings castable to such.

static bool
assertAllMatch(string $pattern, mixed $traversable, bool $case_sensitive = FALSE)

Asserts that all members are strings that contain the specified string.

static bool
assertAllRegularExpressionMatch(string $pattern, mixed $traversable)

Asserts that all members are strings matching a regular expression.

static bool
assertAllObjects(mixed $traversable)

Asserts that all members are objects.

Details

static bool assertTraversable(mixed $traversable)

Asserts argument can be traversed with foreach.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed with foreach.

static bool assertAll(callable $callable, mixed $traversable)

Asserts callback returns TRUE for each member of a traversable.

This is less memory intensive than using array_filter() to build a second array and then comparing the arrays. Many of the other functions in this collection alias this function passing a specific callback to make the code more readable.

Parameters

callable $callable

Callback function.

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and $callable returns TRUE on all members.

See also

http://php.net/manual/language.types.callable.php

static bool assertAllStrings(mixed $traversable)

Asserts that all members are strings.

Use this only if it is vital that the members not be objects, otherwise test with ::assertAllStringable().

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are strings.

static bool assertAllStringable(mixed $traversable)

Asserts all members are strings or objects with magic __toString() method.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are strings or objects with __toString().

static bool assertStringable(mixed $string)

Asserts argument is a string or an object castable to a string.

Use this instead of is_string() alone unless the argument being an object in any way will cause a problem.

Parameters

mixed $string

Variable to be examined

Return Value

bool

TRUE if $string is a string or an object castable to a string.

static bool assertAllArrays(mixed $traversable)

Asserts that all members are arrays.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are arrays.

static bool assertStrictArray(mixed $array)

Asserts that the array is strict.

What PHP calls arrays are more formally called maps in most other programming languages. A map is a datatype that associates values to keys. The term 'strict array' here refers to a 0-indexed array in the classic sense found in programming languages other than PHP.

Parameters

mixed $array

Variable to be examined.

Return Value

bool

TRUE if $traversable is a 0-indexed array.

See also

http://php.net/manual/language.types.array.php

static bool assertAllStrictArrays(mixed $traversable)

Asserts all members are strict arrays.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are strict arrays.

See also

::assertStrictArray

static bool assertAllHaveKey(mixed $traversable)

Asserts all given keys exist in every member array.

Drupal has several data structure arrays that require certain keys be set. You can overload this function to specify a list of required keys. All of the keys must be set for this method to return TRUE.

As an example, this assertion tests for the keys of a theme registry.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members have all keys.

static bool assertAllIntegers(mixed $traversable)

Asserts that all members are integer values.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are integers.

static bool assertAllFloat(mixed $traversable)

Asserts that all members are float values.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are floating point numbers.

static bool assertAllCallable(mixed $traversable)

Asserts that all members are callable.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are callable.

static bool assertAllNotEmpty(mixed $traversable)

Asserts that all members are not empty.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members not empty.

static bool assertAllNumeric(mixed $traversable)

Asserts all members are numeric data types or strings castable to such.

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are numeric.

static bool assertAllMatch(string $pattern, mixed $traversable, bool $case_sensitive = FALSE)

Asserts that all members are strings that contain the specified string.

This runs faster than the regular expression equivalent.

Parameters

string $pattern

The needle to find.

mixed $traversable

Variable to examine.

bool $case_sensitive

TRUE to use strstr(), FALSE to use stristr() which is case insensitive.

Return Value

bool

TRUE if $traversable can be traversed and all members are strings containing $pattern.

static bool assertAllRegularExpressionMatch(string $pattern, mixed $traversable)

Asserts that all members are strings matching a regular expression.

Parameters

string $pattern

Regular expression string to find.

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are strings matching $pattern.

static bool assertAllObjects(mixed $traversable)

Asserts that all members are objects.

When testing if a collection is composed of objects those objects should be given a common interface to implement and the test should be written to search for just that interface. While this method will allow tests for just object status or for multiple classes and interfaces this was done to allow tests to be written for existing code without altering it. Only use this method in that manner when testing code from third party vendors.

Here are some examples:

Parameters

mixed $traversable

Variable to be examined.

Return Value

bool

TRUE if $traversable can be traversed and all members are objects with at least one of the listed classes or interfaces.