Skip to content
Snippets Groups Projects
Commit f59b8445 authored by David Maus's avatar David Maus
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 1939 additions and 0 deletions
:project-name "HAB Solr"
# GNU Emacs
*~
.*~
\#*
.\#*
ChangeLog
# Composer
/vendor/
composer.lock
{
"name": "hab/solr",
"autoload": {
"psr-0": {
"HAB": "src/"
}
}
}
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Command;
/**
* Interface of a Solr command.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
interface CommandInterface
{
/**
* Return handler.
*
* @return string
*/
public function getHandler ();
/**
* Return parameters.
*
* @return array
*/
public function getParameters ();
/**
* Set Solr response.
*
* @param string $response
* @return void
*/
public function setResponse ($response);
/**
* Return command result.
*
* @return mixed
*/
public function getResult ();
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Command;
use HAB\Solr\ParamBag;
use GuzzleHttp\Client;
/**
* Solr command invoker.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class Invoker
{
/**
* Base Url.
*
* @var string
*/
private $baseUrl;
/**
* Client.
*
* @var Client
*/
private $client;
/**
* Parameter provider.
*
* @var ParamBag
*/
private $parameters;
public function __construct ($baseUrl, Client $client)
{
$this->baseUrl = $baseUrl;
$this->client = $client;
}
/**
* Invoke command.
*
* @param CommandInterface $command
* @param ParamBag $parameters
* @return mixed
*/
public function invoke (CommandInterface $command, ParamBag $parameters = null)
{
$params = $this->getParameters();
if ($parameters) {
$params->mergeWith($parameters);
}
$query = $params->mergeWith($command->getParameters())->request();
$url = sprintf('%s/%s?%s', $this->baseUrl, $command->getHandler(), implode('&', $query));
$response = $this->getClient()->request('GET', $url);
$command->setResponse($response->getBody());
return $command->getResult();
}
/**
* Return client.
*
* @return Client
*/
public function getClient ()
{
return $this->client;
}
/**
* Return parameters.
*
* @return ParamBag
*/
public function getParameters ()
{
if (is_null($this->parameters)) {
$this->setParameters(new ParamBag());
}
return $this->parameters;
}
/**
* Set parameters.
*
* @param ParamBag $parameters
* @return
*/
public function setParameters (ParamBag $parameters)
{
$this->parameters = $parameters;
}
/**
* Encode key-value-pair.
*
* @param string $name
* @param string $value
* @return string
*/
private static function encode ($name, $value)
{
return sprintf('%s=%s', urlencode($name), urlencode($value));
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Command;
use HAB\Solr\ParamBag;
/**
* Interface of a Solr query builder.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
interface QueryBuilderInterface
{
/**
* Return Solr query parameters.
*
* @param array|\ArrayAccess $query
* @return ParamBag
*/
public function buildQuery ($query);
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Command;
use HAB\Solr\Response\Json\RecordCollection;
use HAB\Solr\ParamBag;
use ArrayObject;
use LogicException;
use RuntimeException;
/**
* Search command.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class Search implements CommandInterface
{
/**
* Handler.
*
* @var string
*/
private $handler;
/**
* Query.
*
* @var ArrayObject
*/
private $query;
/**
* Query builder.
*
* @var QueryBuilderInterface
*/
private $builder;
public function __construct ($handler, QueryBuilderInterface $builder)
{
$this->handler = $handler;
$this->builder = $builder;
}
/**
* {@inheritDoc}
*/
public function getHandler ()
{
return $this->handler;
}
/**
* {@inheritDoc}
*/
public function getParameters ()
{
return $this->builder->buildQuery($this->query);
}
/**
* {@inheritDoc}
*/
public function setResponse ($response)
{
$result = json_decode($response, true);
if (!is_array($result)) {
throw new RuntimeException('Invalid response from upstream server');
}
$this->result = new RecordCollection($result);
}
/**
* {@inheritDoc}
*/
public function getResult ()
{
if (!$this->result instanceof RecordCollection) {
throw new LogicException('Command was not executed');
}
return $this->result;
}
public function setQuery (ArrayObject $query)
{
$this->query = $query;
}
public function getQuery ()
{
if (is_null($this->query)) {
$this->setQuery(new ArrayObject());
}
return $this->query;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* An abstract base class for facet adapters.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
abstract class AbstractFacetAdapter implements FacetAdapterInterface
{
/**
* The facet value container.
*
* @var Value\Container\ContainerInterface
*/
private $container;
/**
* Facet name.
*
* @var string
*/
private $name;
/**
* Facet label, if any.
*
* @var string
*/
private $label;
/**
* The request value mapper.
*
* @var ValueMapper\RequestValueMapperInterface
*/
private $mapper;
/**
* Container sort function.
*
* @var callable
*/
private $sort;
/**
* Application state.
*
* @var ParameterBag
*/
private $state;
/**
* Set sort function.
*
* The sort function is called with the facet value container as
* sole argument.
*
* @param callable $sort
* @return void
*/
public function setSortFunction ($sort)
{
$this->sort = $sort;
}
/**
* Set facet label.
*
* @param string $label
* @return void
*/
public function setLabel ($label)
{
$this->label = $label;
}
/**
* Return facet label.
*
* @return string|null
*/
public function getLabel ()
{
return $this->label ?: $this->getName();
}
/**
* Return facet name.
*
* @return string
*/
public function getName ()
{
if ($this->name == '') {
$this->setName(spl_object_hash($this));
}
return $this->name;
}
/**
* Set the facet name.
*
* @param string $name
* @return void
*/
public function setName ($name)
{
$this->name = $name;
}
/**
* Return facet value container.
*
* @return Value\Container\ContainerInterface
*/
public function getFacetValueContainer ()
{
if (!$this->container) {
$this->setFacetValueContainer(new Value\Container\FacetValueList());
}
if ($this->sort) {
call_user_func($this->sort, $this->container);
}
return $this->container;
}
/**
* Set facet value container.
*
* @param Value\Container\ContainerInterface $container
* @return void
*/
public function setFacetValueContainer (Value\Container\ContainerInterface $container)
{
$this->container = $container;
}
/**
* {@inheritDoc}
*/
public function getRequestValueMapper ()
{
if (!$this->mapper) {
$this->setRequestValueMapper(new ValueMapper\RequestValueIdentityMapper());
}
return $this->mapper;
}
/**
* Set the request value mapper.
*
* @param ValueMapper\RequestValueMapperInterface $mapper
* @return void
*/
public function setRequestValueMapper (ValueMapper\RequestValueMapperInterface $mapper)
{
$this->mapper = $mapper;
}
/**
* {@inheritDoc}
*/
public function setComponentState (ParameterBag $state)
{
$params = $state->get('f');
if ($params && is_array($params) && isset($params[$this->getName()])) {
$this->setFilterValues($params[$this->getName()]);
}
$this->state = $state;
}
public function getGlobalComponentState ()
{
if ($this->state) {
return $this->state->all();
}
return array();
}
/**
* {@inheritDoc}
*/
public function getComponentState ()
{
return array('f' => array($this->getName() => $this->getFilterValues()));
}
/**
* Set filter values from request.
*
* @param mixed $values
* @return void
*/
abstract public function setFilterValues ($values);
/**
* Return filter values for request.
*
* @return mixed
*/
abstract public function getFilterValues ();
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet;
use HAB\Solr\Response\Json\RecordCollection;
/**
* Default implementation of a {@see FacetAdapterInterface}.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class DefaultFacetAdapter extends AbstractFacetAdapter
{
/**
* Decorated facet.
*
* @var FacetImplInterface
*/
private $facet;
/**
* Facet value label factory.
*
* @var LabelFactory\LabelFactoryInterface
*/
private $labelFactory;
/**
* Constructor.
*
* @param FacetImplInterface $facet
* @param boolean $sortByLabel
* @return void
*/
public function __construct (FacetImplInterface $facet, $sortByLabel = false)
{
$this->facet = $facet;
if ($sortByLabel) {
$this->setSortFunction(
function (Value\Container\ContainerInterface $container) {
$container->sortByLabel();
}
);
}
}
/**
* {@inheritDoc}
*/
public function setFilterValues ($values)
{
$this->facet->setSelected(
$this->getRequestValueMapper()->mapFromRequest($values)
);
}
/**
* {@inheritDoc}
*/
public function getFilterValues ()
{
return $this->getRequestValueMapper()->mapToRequest($this->facet->getSelected());
}
/**
* {@inheritDoc}
*/
public function getSearchParameters ()
{
return $this->facet->getSearchParameters();
}
/**
* {@inheritDoc}
*/
public function setRecordCollection (RecordCollection $response)
{
$this->facet->setRecordCollection($response);
$container = $this->getFacetValueContainer();
$factory = $this->getFacetValueLabelFactory();
$selected = $this->facet->getSelected();
$values = $this->facet->getFacetCounts();
$mapper = $this->getRequestValueMapper();
$name = $this->getName();
foreach ($values as $value) {
$query = $selected;
if ($value->isSelected()) {
foreach (array_keys($query, $value->getValue()) as $key) {
unset($query[$key]);
}
} else {
$query []= $value->getValue();
}
$params = $this->getGlobalComponentState();
if ($query) {
$params['f'][$name] = $mapper->mapToRequest($query);
} else {
unset($params['f'][$name]);
}
$value->setQuery($params);
$value->setLabel($factory->createLabel($value->getValue()));
$container->addFacetValue($value);
}
}
/**
* Return facet value label factory.
*
* @return LabelFactory\LabelFactoryInterface
*/
public function getFacetValueLabelFactory ()
{
if (!$this->labelFactory) {
$this->setFacetValueLabelFactory(new LabelFactory\DefaultLabelFactory());
}
return $this->labelFactory;
}
/**
* Set facet value label factory.
*
* @param LabelFactory\LabelFactoryInterface $factory
* @return void
*/
public function setFacetValueLabelFactory (LabelFactory\LabelFactoryInterface $factory)
{
$this->labelFactory = $factory;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet;
/**
* Interface of a facet adapter.
*
* A facet adapter mitigates between a backend-specific facet
* implementation and the rest of the application.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
interface FacetAdapterInterface
{
/**
* Return adapter name.
*
* @return string
*/
public function getName ();
/**
* Return adapter label.
*
* @return string
*/
public function getLabel ();
/**
* Return facet value container.
*
* @return Value\Container\ContainerInterface
*/
public function getFacetValueContainer ();
/**
* Return the request value mapper.
*
* @return ValueMapper\ValueMapperInterface
*/
public function getRequestValueMapper ();
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet;
use HAB\Solr\Response\Json\RecordCollection;
use HAB\Solr\Response\Json\RecordCollectionConsumerInterface;
use HAB\Solr\ParameterProviderInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Countable;
use ArrayIterator;
use IteratorAggregate;
/**
* A FacetCollection aggregates FacetAdapter objects.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class FacetCollection implements Countable, IteratorAggregate, ParameterProviderInterface, RecordCollectionConsumerInterface
{
/**
* Aggregated facets.
*
* @var array
*/
private $facets;
/**
* Constructor.
*
* @return void
*/
public function __construct ()
{
$this->facets = array();
}
/**
* Add a facet.
*
* @param FacetAdapterInterface $facet
* @return void
*/
public function addFacet (FacetAdapterInterface $facet)
{
$name = $facet->getName();
$this->facets[$name] = $facet;
}
/**
* Return facet by name.
*
* @return FacetDecorator|null
*/
public function getFacet ($name)
{
return isset($this->facets[$name]) ? $this->facets[$name] : null;
}
/**
* Return true if collection aggregates facet with name.
*
* @param string $name
* @return boolean
*/
public function hasFacet ($name)
{
return isset($this->facets[$name]);
}
/**
* {@inheritDoc}
*/
public function getIterator ()
{
return new ArrayIterator($this->facets);
}
/**
* {@inheritDoc}
*/
public function setComponentState (ParameterBag $state)
{
foreach ($this->facets as $facet) {
$facet->setComponentState($state);
}
}
/**
* {@inheritDoc}
*/
public function getComponentState ()
{
$params = array();
foreach ($this->facets as $facet) {
$params = array_merge_recursive($params, $facet->getComponentState());
}
return $params;
}
/**
* {@inheritDoc}
*/
public function getSearchParameters ()
{
$params = array();
foreach ($this->facets as $facet) {
$params = array_merge_recursive($params, $facet->getSearchParameters());
}
return $params;
}
/**
* {@inheritDoc}
*/
public function setRecordCollection (RecordCollection $response)
{
foreach ($this->facets as $name => $facet) {
$facet->setRecordCollection($response);
}
}
/**
* Return number of aggregated facets.
*
* @return integer
*/
public function count ()
{
return count($this->facets);
}
/**
* Return aggregated facet count.
*
* @return integer
*/
public function getAggregatedCount ()
{
$sum = 0;
foreach ($this->facets as $facet) {
$sum += $facet->getFacetValueContainer()->getAggregatedCount();
}
return $sum;
}
}
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet;
/**
* Interface of a backend-specific facet implementations.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
interface FacetImplInterface
{
/**
* Return facet counts.
*
* Returns an array with {@see Value\FacetValue} objects.
*
* @return Value\FacetValue[]
*/
public function getFacetCounts ();
/**
* Set selected facet values.
*
* @param string[] $selected
*/
public function setSelected (array $selected);
/**
* Return array of selected facet values.
*
* @return array
*/
public function getSelected ();
/**
* __clone
*
* @return void
*/
public function __clone ();
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\Impl;
use HAB\Solr\Facet\FacetImplInterface;
use HAB\Solr\Facet\Value\FacetValue;
use HAB\Solr\Response\Json\RecordCollection;
use InvalidArgumentException;
/**
* A simple SOLR field facet.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class FieldFacet implements FacetImplInterface
{
/**
* Field to facet on.
*
* @var string
*/
private $field;
/**
* Selected values.
*
* @var string[]
*/
private $selected;
/**
* Filter query operator.
*
* @var string
*/
private $fqOperator;
/**
* Filter query tag, if any.
*
* @var string
*/
private $fqTag;
/**
* Is multiselect enabled?
*
* @var boolean
*/
private $multiselect;
/**
* Facet options.
*
* @var string[]
*/
private $options;
/**
* Local parameters.
*
* @var LocalParams
*/
private $localParams;
/**
* Facet counts.
*
* @var array
*/
private $counts;
/**
* Constructor.
*
* @param string $field
* @param array $options
* @return void
*/
public function __construct ($field, array $options = array())
{
if (empty($field)) {
throw new InvalidArgumentException('Facet field must not be empty');
}
$this->selected = array();
$this->field = $field;
$this->setFacetOptions($options);
}
/**
* {@inheritDoc}
*/
public function getSearchParameters ()
{
$params = $this->getFacetOptions();
$params['facet'] = 'true';
$params['facet.field'] = sprintf("%s%s", (string)$this->getLocalParams(), $this->field);
if ($fq = $this->getFilterQuery()) {
$params['fq'] = $fq;
}
return $params;
}
/**
* {@inheritDoc}
*/
public function setRecordCollection (RecordCollection $response)
{
$facets = $response->getFacets();
$fields = $facets->getFieldFacets();
if (isset($fields[$this->field])) {
$this->setFacetCounts($fields[$this->field]);
}
}
/**
* Set facet counts.
*
* The parameter is expected to be an array or Traversable that
* maps index values to value counts.
*
* @param array|Traversable $counts
* @return void
*/
public function setFacetCounts ($counts)
{
$this->counts = array();
foreach ($counts as $value => $count) {
$this->counts []= new FacetValue($value, $count, $this->isSelected($value));
}
}
/**
* {@inheritDoc}
*/
public function getFacetCounts ()
{
if ($this->counts === null) {
$this->setFacetCounts(array());
}
return $this->counts;
}
/**
* Return local params.
*
* @return LocalParams
*/
public function getLocalParams ()
{
if (empty($this->localParams)) {
$this->localParams = new LocalParams();
}
return $this->localParams;
}
/**
* {@inheritDoc}
*/
public function setSelected (array $selected)
{
$this->selected = $selected;
}
/**
* Return selected values.
*
* @return string[]
*/
public function getSelected ()
{
if ($this->selected === null) {
$this->setSelected(array());
}
return $this->selected;
}
/**
* Is the value selected?
*
* @param string $value
* @return boolean
*/
public function isSelected ($value)
{
return in_array($value, $this->selected);
}
/**
* Set boolean operator for filter query.
*
* @throws InvalidArgumentException Boolean operator neither 'or' nor 'and'
*
* @param string $operator
* @return void
*/
public function setFilterQueryOperator ($operator)
{
$operator = trim(strtoupper($operator));
if ($operator !== 'AND' && $operator !== 'OR') {
throw new InvalidArgumentException(
sprintf(
"Invalid filter query operator -- expected 'OR' or 'AND', got %s', $operator",
$operator
)
);
}
$this->fqOperator = $operator;
}
/**
* Return filter query operator.
*
* @return string
*/
public function getFilterQueryOperator ()
{
if (empty($this->fqOperator)) {
$this->setFilterQueryOperator('AND');
}
return $this->fqOperator;
}
/**
* Return filter query tag.
*
* @return string
*/
public function getFilterQueryTag ()
{
if (empty($this->fqTag)) {
$this->fqTag = sprintf('%s.%s', $this->field, spl_object_hash($this));
}
return $this->fqTag;
}
/**
* Is multiselected enabled?
*
* @return boolean
*/
public function isMultiselect ()
{
return $this->multiselect;
}
/**
* Enable or disable multiselect.
*
* When multiselect is enabled that facet's filter query is
* excluded from calculating the facet counts.
*
* @param boolean $enable
* @return void
*/
public function enableMultiselect ($enable = true)
{
$local = $this->getLocalParams();
if ($enable) {
$local->set('ex', $this->getFilterQueryTag());
} else {
$local->remove('ex');
}
$this->multiselect = (boolean)$enable;
}
/**
* Return the filter query.
*
* @return string
*/
public function getFilterQuery ()
{
if (!empty($this->selected)) {
$local = new LocalParams();
$local->add('q.op', $this->getFilterQueryOperator());
if ($this->isMultiselect()) {
$local->add('tag', $this->getFilterQueryTag());
}
return sprintf('%s%s:(%s)', (string)$local, $this->field, implode(' ', array_map(array($this, 'escape'), $this->selected)));
}
}
/**
* Set facet options.
*
* Facet options are encoded using SOLR's field specific syntax
* option, e.g. as f.FIELD.option.
*
* @param string[] $options
* @return void
*/
public function setFacetOptions (array $options)
{
$this->options = array();
$field = $this->field;
foreach ($options as $key => $value) {
$this->options["f.{$field}.{$key}"] = $value;
}
}
/**
* Return facet options.
*
* @return array
*/
public function getFacetOptions ()
{
if ($this->options === null) {
$this->setFacetOptions(array());
}
return $this->options;
}
/**
* Escape values for filter query request.
*
* Encloses each value in double-quotes.
*
* @param string $value
* @return string
*/
private function escape ($value)
{
return '"' . addcslashes($value, '"') . '"';
}
/**
* {@inheritDoc}
*/
public function __clone ()
{
if ($this->localParams) {
$this->localParams = clone($this->localParams);
}
$this->fqTag = null;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\Impl;
use HAB\Solr\ParamBag;
/**
* Representing SOLR local params.
*
* @see https://wiki.apache.org/solr/LocalParams
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class LocalParams extends ParamBag
{
/**
* Return local param string.
*
* @return string
*/
public function __toString ()
{
if (!empty($this->params)) {
$params = array();
foreach ($this->params as $key => $values) {
foreach ($values as $value) {
if (strpos($value, ' ') !== false) {
$value = '"' . addcslashes($value, '"') . '"';
}
$params []= "{$key}={$value}";
}
}
return sprintf('{!%s}', implode(' ', $params));
}
return '';
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\LabelFactory;
/**
* The default implementation simply returns the value.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class DefaultLabelFactory implements LabelFactoryInterface
{
/**
* {@inheritDoc}
*/
public function createLabel ($value)
{
return $value;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\LabelFactory;
/**
* A LabelFactory creates a label for a facet value.
*
* The function self::createLabel() is called with the actual value
* rather then a FacetValue to enable usage of the LabelFactory in
* cirumstances where no call to the backend is required.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
interface LabelFactoryInterface
{
/**
* Return label for value.
*
* @param string $value
* @return string
*/
public function createLabel ($value);
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\LabelFactory;
/**
* Uses a map of values to labels.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class MapLabelFactory implements LabelFactoryInterface
{
/**
* Translation map.
*
* @var array
*/
private $map;
/**
* Constructor.
*
* @param array $map
* @return void
*/
public function __construct (array $map)
{
$this->map = $map;
}
/**
* {@inheritDoc}
*/
public function createLabel ($value)
{
if ($value && isset($this->map[$value])) {
return $this->map[$value];
}
return $value;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\LabelFactory;
/**
* Obtain a label by performing a search/replace operation on the value.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2013-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class RegExpLabelFactory implements LabelFactoryInterface
{
/**
* Pattern.
*
* @var string
*/
private $pattern;
/**
* Replace template.
*
* @var string
*/
private $template;
/**
* Constructor.
*
* @param string $pattern
* @param string $template
* @return void
*/
public function __construct ($pattern, $template)
{
$this->pattern = $pattern;
$this->template = $template;
}
/**
* {@inheritDoc}
*/
public function createLabel ($value)
{
if (preg_match($this->pattern, $value)) {
return preg_replace($this->pattern, $this->template, $value);
}
return $value;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\Value\Container;
use HAB\Solr\Facet\Value\FacetValue;
use Countable;
use Iterator;
/**
* Interface of a facet value container.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
interface ContainerInterface extends Countable
{
/**
* Return iterator to iterate over contained facet values.
*
* @return Iterator
*/
public function getIterator ();
/**
* Return iterator to iterate over selected facet values only.
*
* @return Iterator
*/
public function getSelectedValueIterator ();
/**
* Are there selected values?
*
* @return boolean
*/
public function hasSelectedValue ();
/**
* Add facet value.
*
* @param FacetValue $value
* @return void
*/
public function addFacetValue (FacetValue $value);
/**
* Sort facet values by facet value label.
*
* @param boolean $reverse Sort in reverse order
* @return void
*/
public function sortByLabel ($reverse = false);
/**
* Sort facet values by facet value count.
*
* @param boolean $reverse Sort in reverse order
* @return void
*/
public function sortByCount ($reverse = false);
}
\ No newline at end of file
<?php
/**
* This file is part of HAB Solr.
*
* HAB Solr 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.
*
* HAB Solr 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 HAB Solr. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\Solr\Facet\Value\Container;
use HAB\Solr\Facet\Value\FacetValue;
use ArrayIterator;
use IteratorIterator;
/**
* A flat list of facet values.
*
* @author David Maus <maus@hab.de>
* @copyright Copyright (c) 2014-2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class FacetValueList implements ContainerInterface
{
/**
* Facet values.
*
* @var array
*/
private $values;
/**
* Does the list contain a selected value?
*
* @var boolean
*/
private $hasSelectedValue = false;
/**
* Constructor.
*
* @return void
*/
public function __construct ()
{
$this->values = array();
}
/**
* {@inheritDoc}
*/
public function getIterator ()
{
return new IteratorIterator(new ArrayIterator($this->values));
}
/**
* {@inheritDoc}
*/
public function getSelectedValueIterator ()
{
return new SelectedFacetValueFilterIterator($this->getIterator());
}
/**
* {@inheritDoc}
*/
public function hasSelectedValue ()
{
return $this->hasSelectedValue;
}
/**
* {@inheritDoc}
*/
public function addFacetValue (FacetValue $value)
{
if ($value->isSelected()) {
$this->hasSelectedValue = true;
}
if (!in_array($value, $this->values, true)) {
$this->values []= $value;
}
}
/**
* {@inheritDoc}
*/
public function sortByLabel ($reverse = false)
{
usort($this->values, array('HAB Solr\Search\Facet\Value\FacetValue', 'compareByLabel'));
if ($reverse) {
$this->values = array_reverse($this->values);
}
}
/**
* {@inheritDoc}
*/
public function sortByCount ($reverse = false)
{
usort($this->values, array('HAB Solr\Search\Facet\Value\FacetValue', 'compareByCount'));
if ($reverse) {
$this->values = array_reverse($this->values);
}
}
/**
* {@inheritDoc}
*/
public function count ()
{
return count($this->values);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment