1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Search_Query_Facet_DateRange extends Search_Query_Facet_Abstract implements Search_Query_Facet_Interface
9{
10	private $ranges = [];
11	private $format;
12	private $keyed = false;
13
14	static function fromField($field)
15	{
16		return new self($field);
17	}
18
19	/**
20	 * @return string
21	 */
22	function getType()
23	{
24		return 'date_range';
25	}
26
27	/**
28	 * @return array
29	 */
30	function getRanges()
31	{
32		return $this->ranges;
33	}
34
35	/**
36	 * Use ES date math format as per https://www.elastic.co/guide/en/elasticsearch/reference/5.6/common-options.html#date-math
37	 * e.g.
38	 *     now+1d/d (tomorrow, starting at midnight)
39	 *     now-10M/M (now minus 10 months, rounded down to the start of the month)
40	 *     or just a plain date :2018/11/28
41	 *
42	 * @param string $to
43	 * @param string $from
44	 * @param string $key
45	 *
46	 * @return Search_Query_Facet_Interface
47	 */
48	function addRange($to, $from, $key = '')
49	{
50		$range = [
51			'to' => $to,
52			'from' => $from,
53		];
54
55		if ($key) {
56			$range['key'] = $key;
57			$this->keyed = true;
58		}
59
60		$this->ranges[] = array_filter($range);
61
62		return $this;
63	}
64
65	public function clearRanges() {
66		$this->ranges = [];
67	}
68
69	/**
70	 * @return mixed
71	 */
72	public function getFormat()
73	{
74		return $this->format;
75	}
76
77	/**
78	 * @param mixed $format
79	 * date format as per https://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html
80	 *
81	 * @return Search_Query_Facet_Interface
82	 */
83	public function setFormat($format)
84	{
85		$this->format = $format;
86		return $this;
87	}
88}
89