function Inspector::assertAllMatch

Same name in other branches
  1. 8.9.x core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllMatch()
  2. 10 core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllMatch()
  3. 11.x core/lib/Drupal/Component/Assertion/Inspector.php \Drupal\Component\Assertion\Inspector::assertAllMatch()

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.

1 call to Inspector::assertAllMatch()
InspectorTest::testAssertAllMatch in core/tests/Drupal/Tests/Component/Assertion/InspectorTest.php
Tests asserting strstr() or stristr() match.

File

core/lib/Drupal/Component/Assertion/Inspector.php, line 313

Class

Inspector
Generic inspections for the assert() statement.

Namespace

Drupal\Component\Assertion

Code

public static function assertAllMatch($pattern, $traversable, $case_sensitive = FALSE) {
    if (static::assertTraversable($traversable)) {
        if ($case_sensitive) {
            foreach ($traversable as $member) {
                if (!(is_string($member) && strstr($member, $pattern))) {
                    return FALSE;
                }
            }
        }
        else {
            foreach ($traversable as $member) {
                if (!(is_string($member) && stristr($member, $pattern))) {
                    return FALSE;
                }
            }
        }
        return TRUE;
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.