diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:15:42 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:44:51 +0100 |
commit | a1789ddde42033f1b05cc4929491214ee6e79383 (patch) | |
tree | 63615735c4ddffaaabf2428946bb26f90899f7bf /vendor/ruflin/elastica/lib/Elastica/Filter/BoolFilter.php | |
parent | 9e06a62f265e3a2aaabecc598d4bc617e06fa32d (diff) |
Update to MediaWiki 1.26.0
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Filter/BoolFilter.php')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Filter/BoolFilter.php | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Filter/BoolFilter.php b/vendor/ruflin/elastica/lib/Elastica/Filter/BoolFilter.php new file mode 100644 index 00000000..5a488b77 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Filter/BoolFilter.php @@ -0,0 +1,133 @@ +<?php +namespace Elastica\Filter; + +use Elastica\Exception\InvalidException; + +/** + * Bool Filter. + * + * @author Nicolas Ruflin <spam@ruflin.com> + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html + */ +class BoolFilter extends AbstractFilter +{ + /** + * Must. + * + * @var array + */ + protected $_must = array(); + + /** + * Should. + * + * @var array + */ + protected $_should = array(); + + /** + * Must not. + * + * @var array + */ + protected $_mustNot = array(); + + /** + * Adds should filter. + * + * @param array|\Elastica\Filter\AbstractFilter $args Filter data + * + * @return $this + */ + public function addShould($args) + { + return $this->_addFilter('should', $args); + } + + /** + * Adds must filter. + * + * @param array|\Elastica\Filter\AbstractFilter $args Filter data + * + * @return $this + */ + public function addMust($args) + { + return $this->_addFilter('must', $args); + } + + /** + * Adds mustNot filter. + * + * @param array|\Elastica\Filter\AbstractFilter $args Filter data + * + * @return $this + */ + public function addMustNot($args) + { + return $this->_addFilter('mustNot', $args); + } + + /** + * Adds general filter based on type. + * + * @param string $type Filter type + * @param array|\Elastica\Filter\AbstractFilter $args Filter data + * + * @throws \Elastica\Exception\InvalidException + * + * @return $this + */ + protected function _addFilter($type, $args) + { + if ($args instanceof AbstractFilter) { + $args = $args->toArray(); + } elseif (!is_array($args)) { + throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Filter'); + } else { + $parsedArgs = array(); + foreach ($args as $filter) { + if ($filter instanceof AbstractFilter) { + $parsedArgs[] = $filter->toArray(); + } + } + $args = $parsedArgs; + } + + $varName = '_'.$type; + $this->{$varName}[] = $args; + + return $this; + } + + /** + * Converts bool filter to array. + * + * @see \Elastica\Filter\AbstractFilter::toArray() + * + * @return array Filter array + */ + public function toArray() + { + $args = array(); + + if (!empty($this->_must)) { + $args['bool']['must'] = $this->_must; + } + + if (!empty($this->_should)) { + $args['bool']['should'] = $this->_should; + } + + if (!empty($this->_mustNot)) { + $args['bool']['must_not'] = $this->_mustNot; + } + + if (isset($args['bool'])) { + $args['bool'] = array_merge($args['bool'], $this->getParams()); + } + + return $args; + } +} |