--- /dev/null
+<?
+/**
+ * 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);
+}
--- /dev/null
+<?
+/**
+ * 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;
+ }
+}
--- /dev/null
+<?
+/**
+ * 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;
+ }
+}
--- /dev/null
+<?
+/**
+ * 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;
+ }
+}
*/
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
*/
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;
}