You are here

protected static function Condition::matchProperty in Apigee Edge 8

Creates a filter closure that matches a property.

Parameters

array $condition: Condition structure.

Return value

callable Filter function.

1 call to Condition::matchProperty()
Condition::compile in src/Entity/Query/Condition.php
Compiles this conditional clause.

File

src/Entity/Query/Condition.php, line 118

Class

Condition
Defines the condition class for the Apigee Edge entity query.

Namespace

Drupal\apigee_edge\Entity\Query

Code

protected static function matchProperty(array $condition) : callable {
  return function ($item) use ($condition) : bool {
    $value = static::getProperty($item, $condition['field']);

    // Ignore object property values.
    if (is_object($value)) {
      return FALSE;
    }

    // Exit early in case of IS NULL or IS NOT NULL, because they can also
    // deal with array values.
    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)) {
      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 in_array($value, $condition['value'], TRUE);
        case 'NOT IN':
          return !in_array($value, $condition['value'], TRUE);
        case 'STARTS_WITH':
          return mb_strpos($value, $condition['value']) === 0;
        case 'CONTAINS':
          return mb_strpos($value, $condition['value']) !== FALSE;
        case 'ENDS_WITH':
          return mb_substr($value, -mb_strlen($condition['value'])) === (string) $condition['value'];
        default:
          throw new QueryException('Invalid condition operator.');
      }
    }
    return FALSE;
  };
}