<?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\Accommodation\Search;
use Elements\Demi\Accommodation\Search\Service\Vacancies\AbstractAdapter;
use Elements\Demi\Accommodation\Search\Service\Vacancies\Local;
use Elements\Demi\Dependency\DependencyContainer;
use Pimcore\Model\Paginator\PaginateListingInterface;
class Listing implements \Countable, \Iterator, PaginateListingInterface
{
public const RETURNTYPE_ACCOMMODATIONS = 'Accommodations';
public const RETURNTYPE_PRODUCTS = 'Products';
public const RETURNTYPE_PACKAGES = 'Packages';
protected string $returnType = self::RETURNTYPE_ACCOMMODATIONS;
protected ?ResultSet $resultSet = null;
protected ?Parameter $parameter = null;
protected int $preloadPages = 0;
protected int $pageSize = 10;
protected string $orderKey;
protected string $order;
protected int $count = 0;
protected ?int $totalCount = null;
protected ?int $requestTime = null;
protected ?AbstractAdapter $adapter = null;
protected ?DependencyContainer $dc = null;
protected ?array $pagedAccommodations = null;
protected bool $freeCancellationInList = false;
public function __construct(DependencyContainer $dc, AbstractAdapter $adapter)
{
$this->adapter = $adapter;
$this->dc = $dc;
}
public function getParameter(): ?Parameter
{
return !$this->parameter ? throw new \Exception('Parameter not set.') : $this->parameter;
}
public function setParameter(Parameter $parameter): void
{
$this->parameter = $parameter;
$this->adapter->setSearchParameter($parameter);
}
public function setReturnType(string $returnType): void
{
$this->returnType = $returnType;
}
public function getReturnType(): string
{
return $this->returnType;
}
/**
* @param ResultSet\Accommodation[] $pagedAccommodations
*/
public function setPagedAccommodations($pagedAccommodations)
{
$this->pagedAccommodations = $pagedAccommodations;
}
/**
* @return ResultSet\Accommodation[]|null
*/
public function getPagedAccommodations(): ?array
{
return $this->pagedAccommodations;
}
/**
* loads the result into the intern resultSet
*/
public function load()
{
$resultSet = $this->adapter->searchVacancies();
if (method_exists($this->adapter, 'getRequestTime')) {
$this->requestTime = $this->adapter->getRequestTime();
}
$this->pagedAccommodations = $resultSet->getAccommodationPointer(); //for iterator
$this->count = (int) $this->adapter->count();
$this->setTotalCount((int) $this->adapter->getTotalCount());
$this->resultSet = $resultSet;
}
/**
* @return int
*/
public function count(): int
{
$this->checkLoaded();
return $this->adapter->count();
}
/**
* checks if the resultset is filled
*/
public function checkLoaded()
{
$this->getAdapter()->setSearchParameter($this->getParameter());
if ($this->resultSet === null) {
$this->load();
}
}
/**
* @param int $page
* @param int $itemCountPerPage
*
* @return array
*/
public function getItems($page, $itemCountPerPage)
{
$this->checkLoaded();
if ($this->returnType == self::RETURNTYPE_ACCOMMODATIONS) {
//$this->pagedAccommodations = $this->resultSet->getAccommodations($page, $itemCountPerPage);
$this->pagedAccommodations = $this->resultSet->getAccommodations();
return $this->pagedAccommodations;
}
if ($this->returnType == self::RETURNTYPE_PRODUCTS) {
//$this->pagedAccommodations = $this->resultSet->getEachProductSeperated($page, $itemCountPerPage);
$this->pagedAccommodations = $this->resultSet->getEachProductSeperated();
return $this->pagedAccommodations;
}
if ($this->returnType == self::RETURNTYPE_PACKAGES) {
$this->pagedAccommodations = $this->resultSet->getHousePackages($page, $itemCountPerPage);
if ($this->adapter instanceof Local) {
$searchParams = $this->adapter->getSearchParameter();
$orderKeyes = (array) $searchParams->getOrderKey();
$sortFactoryKeys = $this->adapter->getSortFactoryKeys();
foreach ($orderKeyes as $orderKey) {
if (in_array($orderKey, $sortFactoryKeys)) {
$obj = $this->adapter->getSortFactoryByKey($orderKey);
if (!$obj instanceof \Elements\Demi\Accommodation\Search\Service\Vacancies\Local\SortFactory) {
continue;
}
$obj->sortHousePackages($this->pagedAccommodations);
}
}
}
return $this->pagedAccommodations;
}
return [];
}
public function getPaginatorAdapter(): Listing
{
return $this;
}
/**
* Methods for Iterator
*/
public function rewind()
{
$this->checkLoaded();
if ($this->pagedAccommodations === null) {
$this->pagedAccommodations = $this->resultSet->getAccommodations();
}
reset($this->pagedAccommodations);
}
/**
* @return mixed
*/
public function current()
{
$this->checkLoaded();
return current($this->pagedAccommodations);
}
/**
* @return mixed
*/
public function key()
{
$this->checkLoaded();
return key($this->pagedAccommodations);
}
/**
* @return mixed
*/
public function next()
{
$this->checkLoaded();
return next($this->pagedAccommodations);
}
/**
* @return bool
*/
public function valid(): bool
{
return $this->current() !== false;
}
/**
* dummy
*/
public function setLimit()
{
}
/**
* dummy
*/
public function setOffset()
{
}
/**
* @param ResultSet $resultSet
*/
public function setResultSet(ResultSet $resultSet): void
{
$this->resultSet = $resultSet;
}
/**
* @return ResultSet
*/
public function getResultSet(): ?ResultSet
{
$this->checkLoaded();
return $this->resultSet;
}
public function setOrderKey(string $orderKey): void
{
$this->orderKey = $orderKey;
}
public function setOrder(string $order): void
{
$this->order = $order;
}
public function setPageSize(int $pageSize): void
{
$this->pageSize = $pageSize;
}
public function getPageSize(): int
{
return $this->pageSize;
}
public function setCount(?int $count): void
{
$this->count = $count;
}
public function getCount(): ?int
{
return $this->count;
}
public function getAdapter(): ?AbstractAdapter
{
return $this->adapter;
}
public function setRequestTime(?int $requestTime): void
{
$this->requestTime = $requestTime;
}
public function getRequestTime(): ?int
{
return $this->requestTime;
}
public function setTotalCount(?int $totalCount): void
{
$this->totalCount = $totalCount;
}
public function getTotalCount(): ?int
{
return $this->totalCount;
}
}