function Condition::match

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::match()
  2. 10 core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::match()
  3. 11.x core/lib/Drupal/Core/Config/Entity/Query/Condition.php \Drupal\Core\Config\Entity\Query\Condition::match()

Perform the actual matching.

Parameters

array $condition: The condition array as created by the condition() method.

string $value: The value to match against.

Return value

bool TRUE when matches else FALSE.

1 call to Condition::match()
Condition::matchArray in core/lib/Drupal/Core/Config/Entity/Query/Condition.php
Matches for an array representing one or more config paths.

File

core/lib/Drupal/Core/Config/Entity/Query/Condition.php, line 160

Class

Condition
Defines the condition class for the config entity query.

Namespace

Drupal\Core\Config\Entity\Query

Code

protected function match(array $condition, $value) {
    // "IS NULL" and "IS NOT NULL" conditions can also deal with array values,
    // so we return early for them to avoid problems.
    if (in_array($condition['operator'], [
        'IS NULL',
        'IS NOT NULL',
    ], TRUE)) {
        $should_be_set = $condition['operator'] === 'IS NOT NULL';
        return $should_be_set === isset($value);
    }
    if (isset($value)) {
        // We always want a case-insensitive match.
        if (!is_bool($value)) {
            $value = mb_strtolower($value);
        }
        switch ($condition['operator']) {
            case '=':
                return $value == $condition['value'];
            case '>':
                return $value > $condition['value'];
            case '<':
                return $value < $condition['value'];
            case '>=':
                return $value >= $condition['value'];
            case '<=':
                return $value <= $condition['value'];
            case '<>':
                return $value != $condition['value'];
            case 'IN':
                return array_search($value, $condition['value']) !== FALSE;
            case 'NOT IN':
                return array_search($value, $condition['value']) === FALSE;
            case 'STARTS_WITH':
                return strpos($value, $condition['value']) === 0;
            case 'CONTAINS':
                return strpos($value, $condition['value']) !== FALSE;
            case 'ENDS_WITH':
                return substr($value, -strlen($condition['value'])) === (string) $condition['value'];
            default:
                throw new QueryException('Invalid condition operator.');
        }
    }
    return FALSE;
}

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