From 360abb926cba981e5aa2eba66129a7815004ef1a Mon Sep 17 00:00:00 2001 From: James Pepin Date: Sat, 26 Sep 2009 22:30:14 -0400 Subject: [PATCH] add constraint coupler to separate object creation from logic classes --- .../lib/Horde/Constraint/Compound/Factory.php | 21 ++++++++++ .../lib/Horde/Constraint/Compound/Factory/And.php | 17 ++++++++ .../lib/Horde/Constraint/Compound/Factory/Or.php | 17 ++++++++ .../Constraint/lib/Horde/Constraint/Coupler.php | 47 ++++++++++++++++++++++ framework/Log/lib/Horde/Log/Filter/Constraint.php | 20 +++++++-- 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 framework/Constraint/lib/Horde/Constraint/Compound/Factory.php create mode 100644 framework/Constraint/lib/Horde/Constraint/Compound/Factory/And.php create mode 100644 framework/Constraint/lib/Horde/Constraint/Compound/Factory/Or.php create mode 100644 framework/Constraint/lib/Horde/Constraint/Coupler.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 index 000000000..359b9fc3d --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Compound/Factory.php @@ -0,0 +1,21 @@ + + */ +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 index 000000000..3592d8063 --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Compound/Factory/And.php @@ -0,0 +1,17 @@ + + */ +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 index 000000000..537f2444e --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Compound/Factory/Or.php @@ -0,0 +1,17 @@ + + */ +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 index 000000000..b3e78bdb8 --- /dev/null +++ b/framework/Constraint/lib/Horde/Constraint/Coupler.php @@ -0,0 +1,47 @@ + + */ +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; + } +} diff --git a/framework/Log/lib/Horde/Log/Filter/Constraint.php b/framework/Log/lib/Horde/Log/Filter/Constraint.php index 7a645f0ca..95acf8c8c 100644 --- a/framework/Log/lib/Horde/Log/Filter/Constraint.php +++ b/framework/Log/lib/Horde/Log/Filter/Constraint.php @@ -22,7 +22,20 @@ */ 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; } -- 2.11.0