_params; } /** * Remove a Solr param. * * @param string $key * Param name to remove * @param string $value * (optional) Solr param value to be removed. If null given, remove all * params matching $key. */ public function removeParam($key, $value = NULL) { if (! $value) { unset($this->_params[$key]); } else { if (is_array($this->_params[$key])) { foreach ($this->_params[$key] as $_key => $_value) { if ($_value == $value) { unset($this->_params[$_key]); } } } else if($this->_params[$key] == $value) { unset($this->_param[$key]); } } } /** * Add a Solr param to our query * * @param string $key * Solr param name * @param array|string $value * Solr param value, likely more a string any other type. If an array is * given, this will add a set of values for the given param name. */ public function addParam($key, $value) { if (isset($this->_params[$key])) { if (is_array($value)) { // array_values() will ensure we have numeric keys and ensure the // array_merge() expected behavior. if (is_array($this->_params[$key])) { $this->_params = array_merge($this->_params[$key], array_values($value)); } else { $this->_params = array_merge(array($this->_params[$key]), array_values($value)); } } else { if (is_array($this->_params[$key])) { $this->_params[$key][] = $value; } else { $this->_params[$key] = array($this->_params[$key], $value); } } } else { // Whatever is a single value or an array, this will do the right thing. $this->_params[$key] = $value; } } /** * Merge params array with existing one. * * @param array $params = array() * New params to add * @param boolean $override = FALSE * If set to TRUE, incomming params key will override existing ones */ public function addParams($params = array(), $override = FALSE) { foreach ($params as $key => &$value) { if ($override) { $this->removeParam($key); } $this->addParam($key, $value); } } /** * Construct the object * * @param array $params = array() * Apache Solr module formated array of params */ public function __construct($params = array()) { $this->_params = $params; } } /** * Because Solr_Base_Query does not allows us to do full Solr queries, we done * our own simple implementation that fits to our needs. * * TODO: Cache the computed query strings until modifications are done. */ class SolrQuery { /** * Solr params array */ protected $_params = NULL; /** * Solr Query * * @var array * Array of SolrQueryString */ protected $_q = NULL; /* * Solr Query Filter * * @var array * Array of SolrQueryString */ protected $_fq = NULL; /* * Solr Filter query * * @var array * Array of SolrQueryString */ protected $_qf = NULL; /** * Query getter */ public function __get($name) { if ( $name == 'q') { if (empty($this->_q)) { $this->_q = new SolrQueryString(); } return $this->_q; } else if ($name == 'fq') { if (empty($this->_fq)) { $this->_fq = new SolrQueryString(); } return $this->_fq; } /* else if ($name == 'qf') { if (empty($this->_qf)) { $this->_qf = new SolrQueryFilter(); } return $this->_qf; } */ else if ($name == 'params') { return $this->_params; } else { throw new SolrQueryException("Unsupported parameter " . $name); } } /** * Query setter */ public function __set($name, $value) { if ( $name == 'q') { if (! $value instanceof SolrQueryString) { throw new SolrQueryException("'q' parameter must be a SolrQueryString instance"); } $this->_q = $value; } if ( $name == 'fq') { if (! $value instanceof SolrQueryString) { throw new SolrQueryException("'fq' parameter must be a SolrQueryString instance"); } $this->_fq = $value; } /* if ( $name == 'qf') { if (! $value instanceof SolrQueryFilter) { throw new SolrQueryException("'qf' parameter must be a SolrQueryFilter instance"); } $this->_qf = $value; } */ else { throw new SolrQueryException("Unsupported parameter"); } } /** * Rebuild params using setted query strings */ protected function _rebuildParams() { if ($str = (string) $this->_q) { $this->_params->removeParam('q'); $this->_params->addParam('q', $str); } if ($str = (string) $this->_fq) { $this->_params->removeParam('fq'); $this->_params->addParam('fq', $str); } if ($str = (string) $this->_qf) { $this->_params->removeParam('qf'); $this->_params->addParam('qf', $str); } } /** * Get params rebuilt using queries */ public function getParams() { $this->_rebuildParams(); return $this->_params->getParams(); } /** * Construct the object * * @param array $params = array() * Apache Solr module formated array of params * @param SolrQueryString $q = NULL * A SolrQueryString instance for Solr Query string construction * @param SolrQueryString $fq = NULL * A SolrQueryString instance for Solr FilterQuery string construction */ public function __construct($params = array()) { $this->_params = new SolrParams($params); } }