Add is empty filter to search / api
[civicrm-core.git] / Civi / Api4 / Generic / Traits / ArrayQueryActionTrait.php
index 08db9f75fac31a31e7fc71c31cd8e3765b2acda1..88c24a78c49de6dd1b33c1be23ec5c5246455e9d 100644 (file)
@@ -14,8 +14,6 @@
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
  */
 
 
@@ -62,7 +60,7 @@ trait ArrayQueryActionTrait {
    * @return bool
    */
   private function evaluateFilters($row) {
-    $where = $this->getWhere();
+    $where = array_values($this->getWhere());
     $allConditions = in_array($where[0], ['AND', 'OR', 'NOT']) ? $where : ['AND', $where];
     return $this->walkFilters($row, $allConditions);
   }
@@ -132,6 +130,10 @@ trait ArrayQueryActionTrait {
       case 'IS NOT NULL':
         return is_null($value) == ($operator == 'IS NULL');
 
+      case 'IS EMPTY':
+      case 'IS NOT EMPTY':
+        return empty($value) == ($operator == 'IS EMPTY');
+
       case '>':
         return $value > $expected;
 
@@ -160,6 +162,15 @@ trait ArrayQueryActionTrait {
       case 'NOT IN':
         return !in_array($value, $expected);
 
+      case 'CONTAINS':
+        if (is_array($value)) {
+          return in_array($expected, $value);
+        }
+        elseif (is_string($value) || is_numeric($value)) {
+          return strpos((string) $value, (string) $expected) !== FALSE;
+        }
+        return $value == $expected;
+
       default:
         throw new NotImplementedException("Unsupported operator: '$operator' cannot be used with array data");
     }