Rework the Constraint filter to take a coupler (an And constraint or an Or
authorChuck Hagenbuch <chuck@horde.org>
Mon, 28 Sep 2009 18:54:06 +0000 (14:54 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 28 Sep 2009 18:55:09 +0000 (14:55 -0400)
constraint) as the default when adding multiple constraints to a field.

framework/Log/lib/Horde/Log/Filter/Constraint.php

index 7a645f0..67d24b7 100644 (file)
  */
 class Horde_Log_Filter_Constraint implements Horde_Log_Filter_Interface
 {
-    private $_constraints = array();
+    /**
+     * @var array
+     */
+    protected $_constraints = array();
+
+    /**
+     * @var Horde_Constraint_Coupler
+     * @default Horde_Constraint_And
+     */
+    protected $_coupler;
+
+    /**
+     * Constructor
+     *
+     * @param Horde_Constraint_Coupler $coupler The default kind of constraint
+     * to use to couple multiple constraints. Defaults to And.
+     */
+    public function __construct(Horde_Constraint_Coupler $coupler = null)
+    {
+        if (is_null($coupler)) {
+            $coupler = new Horde_Constraint_And();
+        }
+        $this->_coupler = $coupler;
+    }
 
     /**
      * Add a constraint to the filter
@@ -34,11 +57,10 @@ class Horde_Log_Filter_Constraint implements Horde_Log_Filter_Interface
      */
     public function addConstraint($field, Horde_Constraint $constraint)
     {
-        if ($this->_constraints[$field] instanceof Horde_Constraint) {
-            $this->_constraints[$field] = new Horde_Constraint_And($this->_constraints[$field], $constraint);
-        } else {
-            $this->_constraints[$field] = $constraint;
+        if (!isset($this->_constraints[$field])) {
+            $this->_constraints[$field] = clone($this->_coupler);
         }
+        $this->_constraints[$field]->addConstraint($constraint);
         return $this;
     }