add constraint coupler to separate object creation from logic classes
authorJames Pepin <james@jamespepin.com>
Sun, 27 Sep 2009 02:30:14 +0000 (22:30 -0400)
committerJames Pepin <james@jamespepin.com>
Sun, 27 Sep 2009 02:30:14 +0000 (22:30 -0400)
framework/Constraint/lib/Horde/Constraint/Compound/Factory.php [new file with mode: 0644]
framework/Constraint/lib/Horde/Constraint/Compound/Factory/And.php [new file with mode: 0644]
framework/Constraint/lib/Horde/Constraint/Compound/Factory/Or.php [new file with mode: 0644]
framework/Constraint/lib/Horde/Constraint/Coupler.php [new file with mode: 0644]
framework/Log/lib/Horde/Log/Filter/Constraint.php

diff --git a/framework/Constraint/lib/Horde/Constraint/Compound/Factory.php b/framework/Constraint/lib/Horde/Constraint/Compound/Factory.php
new file mode 100644 (file)
index 0000000..359b9fc
--- /dev/null
@@ -0,0 +1,21 @@
+<?
+/**
+ * interface for classes that create compound constraints
+ *
+ * @author James Pepin <james@jamespepin.com>
+ */
+interface Horde_Constraint_Compound_Factory
+{
+    /**
+     * does this factory create objects of the same type as the given object?
+     * @return boolean True if this factory creates this type of object/ false otherwise
+     */
+    public function createsConstraintType(Horde_Constraint $constraint);
+
+    /**
+     * create a compound constraint
+     *
+     * @return Horde_Constraint_Compound the created constraint
+     */
+    public function create(Horde_Constraint $a, Horde_Constraint $b);
+}
diff --git a/framework/Constraint/lib/Horde/Constraint/Compound/Factory/And.php b/framework/Constraint/lib/Horde/Constraint/Compound/Factory/And.php
new file mode 100644 (file)
index 0000000..3592d80
--- /dev/null
@@ -0,0 +1,17 @@
+<?
+/**
+ * create And constraints
+ *
+ * @author James Pepin <james@jamespepin.com>
+ */
+class Horde_Constraint_Compound_Factory_And implements Horde_Constraint_Compound_Factory
+{
+    public function create(Horde_Constraint $a, Horde_Constraint $b)
+    {
+        return new Horde_Constraint_And($a, $b);
+    }
+    public function createsConstraintType(Horde_Constraint $constraint)
+    {
+        return $constraint instanceof Horde_Constraint_And;
+    }
+}
diff --git a/framework/Constraint/lib/Horde/Constraint/Compound/Factory/Or.php b/framework/Constraint/lib/Horde/Constraint/Compound/Factory/Or.php
new file mode 100644 (file)
index 0000000..537f244
--- /dev/null
@@ -0,0 +1,17 @@
+<?
+/**
+ * create Or constraints
+ *
+ * @author James Pepin <james@jamespepin.com>
+ */
+class Horde_Constraint_Compound_Factory_Or implements Horde_Constraint_Compound_Factory
+{
+    public function create(Horde_Constraint $a, Horde_Constraint $b)
+    {
+        return new Horde_Constraint_Or($a, $b);
+    }
+    public function createsConstraintType(Horde_Constraint $constraint)
+    {
+        return $constraint instanceof Horde_Constraint_Or;
+    }
+}
diff --git a/framework/Constraint/lib/Horde/Constraint/Coupler.php b/framework/Constraint/lib/Horde/Constraint/Coupler.php
new file mode 100644 (file)
index 0000000..b3e78bd
--- /dev/null
@@ -0,0 +1,47 @@
+<?
+/**
+ * class for coupling constraints.
+ *
+ * The primary use for this interface is to allow objects to combine two
+ * constraints to form compound constraints
+ *
+ * @author James Pepin <james@jamespepin.com>
+ */
+class Horde_Constraint_Coupler
+{
+    private $_factory;
+
+    public function __construct(Horde_Constraint_Compound_Factory $factory)
+    {
+        $this->_factory = $factory;
+    }
+    /**
+     * couple two constraints together
+     *
+     * @param Horde_Constraint $husband The first constraint
+     * @param Horde_Constraint $wife The second constraint
+     * @return Horde_Constraint_Compound If one of the two arguments is an And constraint, 
+     * then we return that constraint, otherwise, we return a new constraint
+     */
+    public function couple(Horde_Constraint $husband, Horde_Constraint $wife)
+    {
+        if($this->_coupleConstraints($husband, $wife))
+        {
+            return $husband;
+        }
+        if($this->_coupleConstraints($wife, $husband))
+        {
+            return $wife;
+        }
+        return $this->_factory->create($husband, $wife);
+    }
+    private function _coupleConstraints(Horde_Constraint $a, Horde_Constraint $b)
+    {
+        if($this->_factory->createsConstraintType($a))
+        {
+            $a->addConstraint($b);
+            return true;
+        } 
+        return false;
+    }
+}
index 7a645f0..95acf8c 100644 (file)
  */
 class Horde_Log_Filter_Constraint implements Horde_Log_Filter_Interface
 {
-    private $_constraints = array();
+    protected $_constraints = array();
+    protected $_coupler;
+
+    /**
+     * create a new Constraint based filter
+     *
+     * @param Horde_Constraint_Coupler $coupler The default constraint coupler. Use an And coupler 
+     * to make addConstraint create And constraints when a second constraint is added for a field, 
+     * and use and Or coupler to make Or the default coupling of constraints
+     */
+    public function __construct(Horde_Constraint_Coupler $coupler)
+    {
+        $this->_coupler = $coupler;
+    }
 
     /**
      * Add a constraint to the filter
@@ -34,11 +47,12 @@ 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);
+        if(isset($this->_constraints[$field])) {
+            $this->_constraints[$field] = $this->_coupler->couple($this->_constraints[$field], $constraint);
         } else {
             $this->_constraints[$field] = $constraint;
         }
+
         return $this;
     }