1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed under The GPL-3.0 License |
4
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
5
|
|
|
* Redistributions of files must retain the above copyright notice. |
6
|
|
|
* |
7
|
|
|
* @since 2.0.0 |
8
|
|
|
* @author Christopher Castro <[email protected]> |
9
|
|
|
* @link http://www.quickappscms.org |
10
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License |
11
|
|
|
*/ |
12
|
|
|
namespace Search\Operator; |
13
|
|
|
|
14
|
|
|
use Cake\ORM\Query; |
15
|
|
|
use Cake\ORM\Table; |
16
|
|
|
use Search\Operator\BaseOperator; |
17
|
|
|
use Search\Operator\RangeOperator; |
18
|
|
|
use Search\Parser\TokenInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Handles date ranges operators. |
22
|
|
|
* |
23
|
|
|
* For instance: |
24
|
|
|
* |
25
|
|
|
* ``` |
26
|
|
|
* created:<date> |
27
|
|
|
* created:<date1>..<date2> |
28
|
|
|
* ``` |
29
|
|
|
* |
30
|
|
|
* Dates must be in YEAR-MONTH-DATE format. e.g. `2014-12-30` |
31
|
|
|
*/ |
32
|
|
|
class DateOperator extends RangeOperator |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Default configuration for this operator. |
37
|
|
|
* |
38
|
|
|
* ### Options: |
39
|
|
|
* |
40
|
|
|
* - field: Name of the table column which should be scoped, defaults to |
41
|
|
|
* `created`. This column should be of type Date or DateTime. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $_defaultConfig = [ |
46
|
|
|
'field' => 'created', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Parses and extracts lower and upper date values from the given range given |
51
|
|
|
* as `lower..upper`. |
52
|
|
|
* |
53
|
|
|
* Dates must be in YEAR-MONTH-DATE format. e.g. `2014-12-30`. It automatically |
54
|
|
|
* reorder dates if they are given in inversed order (upper..lower). |
55
|
|
|
* |
56
|
|
|
* @param string $value A date range given as `<dateLower>..<dateUpper>`. For |
57
|
|
|
* instance. `2014-12-30..2015-12-30` |
58
|
|
|
* @return array Associative array with two keys: `lower` and `upper`, returned |
59
|
|
|
* dates are fully PHP compliant |
60
|
|
|
*/ |
61
|
|
|
protected function _parseRange($value) |
62
|
|
|
{ |
63
|
|
|
$range = parent::_parseRange($value); |
64
|
|
|
$lower = $this->_normalize($range['lower']); |
65
|
|
|
$upper = $this->_normalize($range['upper']); |
66
|
|
|
if (strtotime($lower) > strtotime($upper)) { |
67
|
|
|
list($lower, $upper) = [$upper, $lower]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return [ |
71
|
|
|
'lower' => $lower, |
72
|
|
|
'upper' => $upper, |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Normalizes the given date. |
78
|
|
|
* |
79
|
|
|
* @param string $date Date to normalize |
80
|
|
|
* @return string Date formated as `Y-m-d` |
81
|
|
|
*/ |
82
|
|
|
protected function _normalize($date) |
83
|
|
|
{ |
84
|
|
|
$date = preg_replace('/[^0-9\-]/', '', $date); |
85
|
|
|
$parts = explode('-', $date); |
86
|
|
|
$year = date('Y'); |
87
|
|
|
$month = 1; |
88
|
|
|
$day = 1; |
89
|
|
|
|
90
|
|
View Code Duplication |
if (!empty($parts[0]) && |
91
|
|
|
1 <= intval($parts[0]) && |
92
|
|
|
intval($parts[0]) <= 32767 |
93
|
|
|
) { |
94
|
|
|
$year = intval($parts[0]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
if (!empty($parts[1]) && |
98
|
|
|
1 <= intval($parts[1]) && |
99
|
|
|
intval($parts[1]) <= 12 |
100
|
|
|
) { |
101
|
|
|
$month = intval($parts[1]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
if (!empty($parts[2]) && |
105
|
|
|
1 <= intval($parts[2]) && |
106
|
|
|
intval($parts[2]) <= 31 |
107
|
|
|
) { |
108
|
|
|
$day = intval($parts[2]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return date('Y-m-d', strtotime("{$year}-{$month}-{$day}")); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|