Created
January 8, 2014 14:28
-
-
Save dmj/8317558 to your computer and use it in GitHub Desktop.
VF2 QueryChain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * This file is part of HABfind. | |
| * | |
| * HABfind is free software: you can redistribute it and/or modify | |
| * it under the terms of the GNU General Public License as published by | |
| * the Free Software Foundation, either version 3 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * HABfind is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with HABfind. If not, see <http://www.gnu.org/licenses/>. | |
| * | |
| * @author David Maus <[email protected]> | |
| * @copyright Copyright (c) 2013 by Herzog August Bibliothek Wolfenbüttel | |
| * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 | |
| */ | |
| namespace HABfind\Service\Search\Query; | |
| use VuFindSearch\Query\AbstractQuery; | |
| use SplObjectStorage; | |
| /** | |
| * A chain of queries concatenated by boolean operators. | |
| * | |
| * @author David Maus <[email protected]> | |
| * @copyright Copyright (c) 2013 by Herzog August Bibliothek Wolfenbüttel | |
| * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 | |
| */ | |
| class QueryChain extends AbstractQuery | |
| { | |
| /** | |
| * Default operator. | |
| * | |
| * @var string | |
| */ | |
| private static $defaultOperator = 'AND'; | |
| /** | |
| * Query chain. | |
| * | |
| * @var SplObjectStorage | |
| */ | |
| private $chain; | |
| /** | |
| * Constructor. | |
| * | |
| * @return void | |
| */ | |
| public function __construct () | |
| { | |
| $this->chain = new SplObjectStorage(); | |
| } | |
| /** | |
| * Add chain element. | |
| * | |
| * @param QueryChainElement $element | |
| * @return void | |
| */ | |
| public function addQueryChainElement (QueryChainElement $element) | |
| { | |
| $this->chain->attach($element->getQuery(), $element->getOperator() ?: self::$defaultOperator); | |
| } | |
| /** | |
| * Return chain. | |
| * | |
| * Returns a SplObjectStorage containg the queries and the | |
| * associated trailing operator. | |
| * | |
| * @return SplObjectStorage | |
| */ | |
| public function getQueryChain () | |
| { | |
| return clone($this->chain); | |
| } | |
| /// AbstractQuery | |
| /** | |
| * {@inheritDoc} | |
| */ | |
| public function containsTerm ($needle) | |
| { | |
| foreach ($this->chain as $query) { | |
| if ($query->containsTerm($needle)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| /** | |
| * {@inheritDoc} | |
| */ | |
| public function getAllTerms () | |
| { | |
| $terms = array(); | |
| foreach ($this->chain as $query) { | |
| $terms []= $query->getAllTerms(); | |
| } | |
| return implode(' ', $terms); | |
| } | |
| /** | |
| * {@inheritDoc} | |
| */ | |
| public function replaceTerm ($from, $to) | |
| { | |
| foreach ($this->chain as $query) { | |
| $query->replaceTerm($form, $to); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * This file is part of HABfind. | |
| * | |
| * HABfind is free software: you can redistribute it and/or modify | |
| * it under the terms of the GNU General Public License as published by | |
| * the Free Software Foundation, either version 3 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * HABfind is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with HABfind. If not, see <http://www.gnu.org/licenses/>. | |
| * | |
| * @author David Maus <[email protected]> | |
| * @copyright Copyright (c) 2013 by Herzog August Bibliothek Wolfenbüttel | |
| * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 | |
| */ | |
| namespace HABfind\Service\Search\Query; | |
| use InvalidArgumentException; | |
| /** | |
| * A query chain element. | |
| * | |
| * @author David Maus <[email protected]> | |
| * @copyright Copyright (c) 2013 by Herzog August Bibliothek Wolfenbüttel | |
| * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 | |
| */ | |
| class QueryChainElement | |
| { | |
| /** | |
| * Valid operator. | |
| * | |
| * @var array | |
| */ | |
| private static $operators = array('AND', 'OR', 'NOT'); | |
| /** | |
| * Query. | |
| * | |
| * @var AbstractQuery $query | |
| */ | |
| private $query; | |
| /** | |
| * Operator. | |
| * | |
| * @var string | |
| */ | |
| private $operator; | |
| /** | |
| * Constructor. | |
| * | |
| * @return void | |
| */ | |
| public function __construct () | |
| {} | |
| /** | |
| * Return query. | |
| * | |
| * @return Query | |
| */ | |
| public function getQuery () | |
| { | |
| if (!$this->query) { | |
| $this->query = new Query(); | |
| } | |
| return $this->query; | |
| } | |
| /** | |
| * Return operator. | |
| * | |
| * @return string | |
| */ | |
| public function getOperator () | |
| { | |
| return $this->operator; | |
| } | |
| /** | |
| * Set operator. | |
| * | |
| * @throws InvalidArgumentException Invalid operator | |
| * | |
| * @param string $operator | |
| * @return void | |
| */ | |
| public function setOperator ($operator) | |
| { | |
| $operator = strtoupper($operator); | |
| if (!in_array($operator, self::$operators)) { | |
| throw new InvalidArgumentException(sprintf('Invalid query chain operator: %s', $operator)); | |
| } | |
| $this->operator = $operator; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment