* Date: 19/06/2019 17:34 */ namespace Beds24\Beds24; use Thelia\Core\HttpFoundation\Request; use Thelia\Core\HttpFoundation\Session\Session; class SearchParameters { const SESSION_VAR_NAME = 'beds24.search-parameters'; /** @var Session */ protected $session; /** * SearchParameters constructor. * @param Request|\Symfony\Component\HttpFoundation\Request $request */ public function __construct(Request $request) { $this->session = $request->getSession(); } /** * @param \DateTime $startDate * @param \DateTime $endDate * @param int $adultCount * @param int $childCount * @return SearchParameters */ public function setSearchParameters(\DateTime $startDate, \DateTime $endDate, $adultCount, $childCount) { $this->session->set(self::SESSION_VAR_NAME, [ 'start_date' => $startDate, 'end_date' => $endDate, 'adultCount' => $adultCount, 'childCount' => $childCount ]); return $this; } /** * @return array * @throws \Exception */ public function getSearchParameters() { if (! $this->session->has(self::SESSION_VAR_NAME)) { // Set default values // $startDate = new \DateTime("next monday"); // $endDate = new \DateTime("next monday + 4 days"); $startDate = new \DateTime("today"); $endDate = new \DateTime("today + 1 days"); $this->setSearchParameters($startDate, $endDate, 1, 0); } return $this->session->get(self::SESSION_VAR_NAME); } public function clearSearchParameters() { $this->session->remove(self::SESSION_VAR_NAME); } /** * @return \DateTime() * @throws \Exception */ public function getStartDate() { return $this->getSearchParameters()['start_date']; } /** * @return \DateTime() * @throws \Exception */ public function getEndDate() { return $this->getSearchParameters()['end_date']; } /** * @return int * @throws \Exception */ public function getAdultCount() { return $this->getSearchParameters()['adultCount']; } /** * @return int * @throws \Exception */ public function getChildCount() { return $this->getSearchParameters()['childCount']; } }