<?php
/**
* Elements DeMI
*
* This source file is available under the elements DeMI license version 1
*
* @copyright Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
* @license elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
*/
namespace Elements\Demi\Event\Search;
use Elements\Demi\Deskline\Exception;
use Pimcore\Model\Paginator\PaginateListingInterface;
class Listing implements \Iterator, PaginateListingInterface
{
/**
* @var \Elements\Demi\Event\Search\Parameter
*/
protected $parameter = null;
/**
* @var int
*/
protected $pageSize = 10;
/**
* @var null
*/
protected $randomSeed = null;
/**
* @var \Elements\Demi\Deskline\Service\Search|null
*/
protected $adapter = null;
/**
* @var array
*/
protected $objects = [];
/**
* @var int
*/
protected $count = 0;
/**
* Listing constructor.
*
* @param \Elements\Demi\Deskline\Service\Search $adapter
* @param int $page
* @param int $entriesPerPage
*/
public function __construct(\Elements\Demi\Deskline\Service\Search $adapter, protected $page = 1, $entriesPerPage = 10)
{
$this->pageSize = $entriesPerPage;
$this->adapter = $adapter;
}
/**
* @return Parameter
*
* @throws Exception
*/
public function getParameter(): Parameter
{
if (empty($this->parameter)) {
throw new Exception('Parameter not set!');
}
return $this->parameter;
}
/**
* @param Parameter $parameter
*/
public function setParameter(Parameter $parameter)
{
if (!$parameter->getCustomOrderKey() && !$parameter->getOrderConst()) {
$parameter->setOrderConst(\Elements\Demi\Deskline\SearchParameter\Events::ORDER_BY_DATE_BEGIN_ASC);
}
$this->parameter = $parameter;
}
/**
* @return int
*/
public function count()
{
if (!$this->objects) {
$this->load();
}
return $this->count;
}
/**
* @param int $page
* @param int $itemCountPerPage
*
* @return array
*
*/
public function getItems($page, $itemCountPerPage)
{
if (! $this->objects) {
$this->page = $page;
$this->pageSize = $itemCountPerPage;
$this->load();
}
return $this->objects;
}
/**
* @inheritdoc
*/
public function load()
{
$result = $this->adapter->searchEvent($this->getParameter(), $this->page, $this->pageSize, $this->getParameter()->getOrderConst(), $this->randomSeed, $this->getParameter()->getCustomOrderKey(), $this->getParameter()->getCustomOrder());
$this->count = $result->getTotalAmount();
$this->objects = $result->getEntries();
}
/**
* @return \Elements\Demi\Accommodation\Search\Listing\VacancyLocal|\Zend\Paginator\Adapter\AdapterInterface
*/
public function getPaginatorAdapter()
{
return $this;
}
/**
* @inheritdoc
*/
public function rewind()
{
if ($this->objects === null) {
$this->load();
}
reset($this->objects);
}
/**
* @return mixed
*/
public function current()
{
if ($this->objects === null) {
$this->load();
}
return current($this->objects);
}
/**
* @return mixed
*/
public function key()
{
if ($this->objects === null) {
$this->load();
}
return key($this->objects);
}
/**
* @return mixed
*/
public function next()
{
if ($this->objects === null) {
$this->load();
}
return next($this->objects);
}
/**
* @return bool
*/
public function valid()
{
return $this->current() !== false;
}
/**
* @param array $objects
*/
public function setObjects($objects)
{
$this->objects = $objects;
}
/**
* @return array
*/
public function getObjects()
{
return $this->objects;
}
}