Skip to content

Instantly share code, notes, and snippets.

@dmj
Last active December 21, 2015 08:58
Show Gist options
  • Save dmj/6281336 to your computer and use it in GitHub Desktop.
Save dmj/6281336 to your computer and use it in GitHub Desktop.
<?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\Application\Backend\Search;
use Zend\EventManager\SharedListenerAggregateInterface;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\EventManager\EventInterface;
use SplObjectStorage;
/**
* Base class for parameter provider listeners.
*
* @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
*/
abstract class AbstractParameterProviderListener implements SharedListenerAggregateInterface
{
/**
* Attached listeners.
*
* @var SplObjectStorage
*/
private $listeners;
/**
* Backends to listen for.
*
* @var array
*/
private $backends;
/**
* Operations to listen for.
*
* @var array
*/
private $operations;
/**
* Constructor.
*
* @return void
*/
public function __construct ()
{
$this->listeners = new SplObjectStorage();
$this->backends = array();
$this->operations = array();
}
/**
* Return array with search parameters.
*
* @return array
*/
abstract protected function getSearchParameters ();
/**
* Configure listener.
*
* @param true|string|array $backend
* @param true|string|array $operation
* @return void
*/
public function configureFor ($backend, $operation)
{
$this->backends = ($backend === true) ? $backend : (array)$backend;
$this->operations = ($operation === true) ? $operation : (array)$operation;
}
/**
* {@inheritDoc}
*/
public function attachShared (SharedEventManagerInterface $events)
{
$listener = $events->attach('VuFind\Search', 'pre', array($this, 'onSearchPre'));
$this->addListener($events, $listener);
}
/**
* {@inheritDoc}
*/
public function detachShared (SharedEventManagerInterface $events)
{
if ($this->listeners->contains($events)) {
$listeners = $this->listeners->offsetGet($events);
foreach ($listeners as $listener) {
$events->detach('VuFind\Search', $listener);
}
$this->listeners->detach($events);
}
}
/**
* Add a listeners.
*
* @param SharedEventManagerInterface $evens
* @param mixed $listener
* @return void
*/
protected function addListener (SharedEventManagerInterface $events, $listener)
{
if (!$this->listeners->contains($events)) {
$this->listeners->offsetSet($events, new SplObjectStorage());
}
$listeners = $this->listeners->offsetGet($events);
if (is_array($listener)) {
array_map(array($listeners, 'attach'), $listeners);
} else {
$listeners->attach($listener);
}
}
/**
* .search.pre
*
* @param EventInterface $event
* @return EventInterface
*/
public function onSearchPre (EventInterface $event)
{
$backend = $event->getParam('backend');
$operation = $event->getParam('context');
if ($this->isConfiguredFor($backend, $operation)) {
$params = $event->getParam('params');
foreach ($this->getSearchParameters() as $key => $value) {
$params->add($key, $value);
}
}
return $event;
}
/**
* Return TRUE if provider is configured for backend and operation.
*
* @param string $backend
* @param string $operation
* @return boolean
*/
public function isConfiguredFor ($backend, $operation)
{
return (($this->backends === true || in_array($backend, $this->backends, true)) &&
($this->operations === true || in_array($operation, $this->operations, true)));
}
}
<?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\Application\Model\Facet;
use HABfind\Application\Component\ComponentInterface;
use HABfind\Application\Backend\Search\AbstractParameterProviderListener;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\EventManager\EventInterface;
use Zend\Stdlib\Parameters;
use IteratorAggregate;
use ArrayIterator;
/**
* A collection of 0..n facets.
*
* @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 FacetCollection extends AbstractParameterProviderListener implements IteratorAggregate, ComponentInterface
{
/**
* Attached facets.
*
* @var SplObjectStorage
*/
private $facets;
/**
* Component state difference.
*
* @var array
*/
private $state;
/**
* Constructor.
*
* @return void
*/
public function __construct ()
{
parent::__construct();
$this->facets = array();
}
/**
* {@inheritDoc}
*/
public function attachShared (SharedEventManagerInterface $events)
{
parent::attachShared($events);
$listener = $events->attach('VuFind\Search', 'post', array($this, 'onSearchPost'));
$this->addListener($events, $listener);
}
/**
* Add a facet.
*
* @throws InvalidArgumentException Name already in use
*
* @param string $name
* @return void
*/
public function addFacet (FacetInterface $facet)
{
$name = $facet->getFacetName();
if (isset($this->facets[$name])) {
throw new InvalidArgumentException(sprintf('Facet name already in use: %s', $name));
}
$this->facets[$name] = $facet;
}
/**
* Return faced by name.
*
* @param string $name
* @return FacetInterface|null
*/
public function getFacet ($name)
{
if (isset($this->facets[$name])) {
return $this->facets[$name];
}
return null;
}
/**
* {@inheritDoc}
*/
public function setComponentState (Parameters $state)
{
foreach ($this->facets as $facet) {
$facet->setComponentState($state);
}
$this->state = $state->getArrayCopy();
unset($this->state['f']);
}
/**
* {@inheritDoc}
*/
public function getComponentState ()
{
$state = array();
foreach ($this->facets as $facet) {
$state = array_merge_recursive($state, $facet->getComponentState());
}
return $state;
}
/**
* {@inheritDoc}
*/
public function getComponentStateDiff ()
{
return $this->state;
}
/**
* {@inheritDoc}
*/
public function getSearchParameters ()
{
$params = array();
foreach ($this->facets as $facet) {
$params = array_merge_recursive($params, $facet->getSearchParameters());
}
return $params;
}
/**
* {@inheritDoc}
*/
public function getIterator ()
{
return new ArrayIterator($this->facets);
}
/**
* .search.post
*
* @param EventInterface $event
* @return EventInterface
*/
public function onSearchPost (EventInterface $event)
{
$backend = $event->getParam('backend');
$operation = $event->getParam('context');
if ($this->isConfiguredFor($backend, $operation)) {
$result = $event->getTarget();
$counts = $result->getFacets();
foreach ($this->facets as $facet) {
$facet->updateFacetCounts($counts);
}
}
return $event;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment