From a85902b08d8b7a55fb8c61369baf4af779a8ab25 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 22 Jul 2011 12:01:33 +0200 Subject: [PATCH] [DDC-551] Initial code for filter functionality --- lib/Doctrine/ORM/Configuration.php | 14 ++++- lib/Doctrine/ORM/EntityManager.php | 49 +++++++++++++++++ lib/Doctrine/ORM/Query/Filter/SQLFilter.php | 61 +++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 lib/Doctrine/ORM/Query/Filter/SQLFilter.php diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index a7219da8b..6e48ac19b 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -515,4 +515,16 @@ class Configuration extends \Doctrine\DBAL\Configuration } return $this->_attributes['classMetadataFactoryName']; } -} \ No newline at end of file + + public function addFilter($name, $className) + { + $this->_attributes['filters'][strtolower($name)] = $className; + } + + public function getFilterClassName($name) + { + $name = strtolower($name); + return isset($this->_attributes['filters'][$name]) ? + $this->_attributes['filters'][$name] : null; + } +} diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0379cc435..c127eefff 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -110,6 +110,13 @@ class EntityManager implements ObjectManager */ private $closed = false; + /** + * Instances of enabled filters. + * + * @var array + */ + private $enabledFilters; + /** * Creates a new EntityManager that operates on the given database connection * and uses the given Configuration and EventManager implementations. @@ -739,4 +746,46 @@ class EntityManager implements ObjectManager return new EntityManager($conn, $config, $conn->getEventManager()); } + + /** @return SQLFilter[] */ + public function getEnabledFilters() + { + return $enabledFilters; + } + + /** Throws exception if filter does not exist. No-op if the filter is alrady enabled. + * @return SQLFilter */ + public function enableFilter($name) + { + if(null === $filterClass = $this->config->getFilter($name)) { + throw new \InvalidArgumentException("Filter '" . $name . "' is does not exist."); + } + + if(!isset($this->enabledFilters[$name])) { + $this->enabledFilters[$name] = new $filterClass($this->conn); + } + + return $this->enabledFilters[$name]; + } + + /** Disable the filter, looses the state */ + public function disableFilter($name) + { + // Get the filter to return it + $filter = $this->getFilter($name); + + unset($this->enabledFilters[$name]); + + return $filter; + } + + /** throws exception if not in enabled filters */ + public function getFilter($name) + { + if(!isset($this->enabledFilters[$name])) { + throw new \InvalidArgumentException("Filter '" . $name . "' is not enabled."); + } + + return $this->enabledFilters[$name]; + } } diff --git a/lib/Doctrine/ORM/Query/Filter/SQLFilter.php b/lib/Doctrine/ORM/Query/Filter/SQLFilter.php new file mode 100644 index 000000000..454c554c1 --- /dev/null +++ b/lib/Doctrine/ORM/Query/Filter/SQLFilter.php @@ -0,0 +1,61 @@ +. + */ + +namespace Doctrine\ORM\Query\Filter; + +use Doctrine\DBAL\Connection; + +/** + * The base class that user defined filters should extend. + * + * Handles the setting and escaping of parameters. + * + * @author Alexander + * @author Benjamin Eberlei + * @abstract + */ +abstract class SQLFilter +{ + private $conn; + + private $parameters; + + final public function __construct(Connection $conn) + { + $this->conn = $conn; + } + + final function setParameter($name, $value, $type) + { + // @todo: check for a valid type? + $parameters[$name] = array('value' => $value, 'type' => $type); + } + + final function getParameter($name) + { + if(!isset($parameters[$name])) { + throw new \InvalidArgumentException("Parameter '" . $name . "' is does not exist."); + } + + // @todo: espace the parameter + return $paramaters[$name]['value']; + } + + abstract function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias); +}