Passed
Pull Request — developer (#16984)
by Arkadiusz
29:54
created

DateField::operatorCustom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * Date field condition record field file.
5
 *
6
 * @package UIType
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 */
12
13
namespace App\Conditions\RecordFields;
14
15
use App\Log;
16
17
/**
18
 * Date field condition record field class.
19
 */
20
class DateField extends BaseField
21
{
22
	use \App\Conditions\RecordTraits\Comparison;
23
	use \App\Conditions\RecordTraits\ComparisonField;
24
25
	/** {@inheritdoc} */
26
	public function check()
27
	{
28
		$fn = 'operator' . ucfirst($this->operator);
29
		if (isset(\App\Condition::DATE_OPERATORS[$this->operator]) && !method_exists($this, $fn)) {
30
			$fn = 'getStdOperator';
31
		}
32
		if (method_exists($this, $fn)) {
33
			Log::trace("Entering to $fn in " . __CLASS__);
34
			return $this->{$fn}();
35
		}
36
		Log::error("Not found operator: $fn in  " . __CLASS__);
37
		return false;
38
	}
39
40
	/**
41
	 * Today operator.
42
	 *
43
	 * @return bool
44
	 */
45
	public function operatorToday()
46
	{
47
		return date('Y-m-d', strtotime($this->getValue())) === date('Y-m-d');
48
	}
49
50
	/**
51
	 * Smaller operator.
52
	 *
53
	 * @return bool
54
	 */
55
	public function operatorSmaller()
56
	{
57
		return date('Y-m-d', strtotime($this->getValue())) < date('Y-m-d');
58
	}
59
60
	/**
61
	 * Greater operator.
62
	 *
63
	 * @return bool
64
	 */
65
	public function operatorGreater()
66
	{
67
		return date('Y-m-d', strtotime($this->getValue())) > date('Y-m-d');
68
	}
69
70
	/**
71
	 * Greater operator.
72
	 *
73
	 * @return bool
74
	 */
75
	public function operatorPrevfy()
76
	{
77
		return date('Y', strtotime($this->getValue())) === date('Y', strtotime('last year'));
78
	}
79
80
	/**
81
	 * Thisfy operator.
82
	 *
83
	 * @return bool
84
	 */
85
	public function operatorThisfy()
86
	{
87
		return date('Y', strtotime($this->getValue())) === date('Y');
88
	}
89
90
	/**
91
	 * Nextfy operator.
92
	 *
93
	 * @return bool
94
	 */
95
	public function operatorNextfy()
96
	{
97
		return date('Y', strtotime($this->getValue())) === date('Y', strtotime('next year'));
98
	}
99
100
	/**
101
	 * Prevfq operator.
102
	 *
103
	 * @return bool
104
	 */
105
	public function operatorPrevfq()
106
	{
107
		return (ceil(date('n', strtotime($this->value)) / 3)) === (ceil(date('n') / 3) - 1);
108
	}
109
110
	/**
111
	 * Thisfq operator.
112
	 *
113
	 * @return bool
114
	 */
115
	public function operatorThisfq()
116
	{
117
		return (ceil(date('n', strtotime($this->value)) / 3)) === (ceil(date('n') / 3));
118
	}
119
120
	/**
121
	 * Nextfq operator.
122
	 *
123
	 * @return bool
124
	 */
125
	public function operatorNextfq()
126
	{
127
		return (ceil(date('n', strtotime($this->value)) / 3)) === (ceil(date('n') / 3) + 1);
128
	}
129
130
	/**
131
	 * Yesterday operator.
132
	 *
133
	 * @return bool
134
	 */
135
	public function operatorYesterday()
136
	{
137
		return date('Y-m-d', strtotime($this->getValue())) === date('Y-m-d', strtotime('last day'));
138
	}
139
140
	/**
141
	 * Untiltoday operator.
142
	 *
143
	 * @return bool
144
	 */
145
	public function operatorUntiltoday()
146
	{
147
		return date('Y-m-d', strtotime($this->getValue())) <= date('Y-m-d');
148
	}
149
150
	/**
151
	 * Tomorrow operator.
152
	 *
153
	 * @return bool
154
	 */
155
	public function operatorTomorrow()
156
	{
157
		return date('Y-m-d', strtotime($this->getValue())) === date('Y-m-d', strtotime('tomorrow'));
158
	}
159
160
	/**
161
	 * Lastweek operator.
162
	 *
163
	 * @return bool
164
	 */
165
	public function operatorLastweek()
166
	{
167
		$startDay = date('Y-m-d', strtotime('last week'));
168
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
169
		return ($dateValue >= $startDay) && ($dateValue <= date('Y-m-d', strtotime($startDay . '+6 day')));
170
	}
171
172
	/**
173
	 * Thisweek operator.
174
	 *
175
	 * @return bool
176
	 */
177
	public function operatorThisweek()
178
	{
179
		$startDay = date('Y-m-d', strtotime('this week'));
180
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
181
		return ($dateValue >= $startDay) && ($dateValue <= date('Y-m-d', strtotime($startDay . '+6 day')));
182
	}
183
184
	/**
185
	 * Nextweek operator.
186
	 *
187
	 * @return bool
188
	 */
189
	public function operatorNextweek()
190
	{
191
		$startDay = date('Y-m-d', strtotime('next week'));
192
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
193
		return ($dateValue >= $startDay) && ($dateValue <= date('Y-m-d', strtotime($startDay . '+6 day')));
194
	}
195
196
	/**
197
	 * Lastmonth operator.
198
	 *
199
	 * @return bool
200
	 */
201
	public function operatorLastmonth()
202
	{
203
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
204
		return ($dateValue >= date('Y-m-01', strtotime('first day of last month'))) && ($dateValue <= date('Y-m-t', strtotime('first day of last month')));
205
	}
206
207
	/**
208
	 * Thismonth operator.
209
	 *
210
	 * @return bool
211
	 */
212
	public function operatorThismonth()
213
	{
214
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
215
		return ($dateValue >= date('Y-m-01', strtotime('this month'))) && ($dateValue <= date('Y-m-t', strtotime('this month')));
216
	}
217
218
	/**
219
	 * Nextmonth operator.
220
	 *
221
	 * @return bool
222
	 */
223
	public function operatorNextmonth()
224
	{
225
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
226
		return ($dateValue >= date('Y-m-01', strtotime('first day of next month'))) && ($dateValue <= date('Y-m-t', strtotime('first day of next month')));
227
	}
228
229
	/**
230
	 * Last7days operator.
231
	 *
232
	 * @return bool
233
	 */
234
	public function operatorLast7days()
235
	{
236
		$today = date('Y-m-d');
237
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
238
		return ($dateValue >= date('Y-m-d', strtotime($today . '-6 day'))) && ($dateValue <= $today);
239
	}
240
241
	/**
242
	 * Last15days operator.
243
	 *
244
	 * @return bool
245
	 */
246
	public function operatorLast15days()
247
	{
248
		$today = date('Y-m-d');
249
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
250
		return ($dateValue >= date('Y-m-d', strtotime($today . '-14 day'))) && ($dateValue <= $today);
251
	}
252
253
	/**
254
	 * Last30days operator.
255
	 *
256
	 * @return bool
257
	 */
258
	public function operatorLast30days()
259
	{
260
		$today = date('Y-m-d');
261
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
262
		return ($dateValue >= date('Y-m-d', strtotime($today . '-29 day'))) && ($dateValue <= $today);
263
	}
264
265
	/**
266
	 * Last60days operator.
267
	 *
268
	 * @return bool
269
	 */
270
	public function operatorLast60days()
271
	{
272
		$today = date('Y-m-d');
273
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
274
		return ($dateValue >= date('Y-m-d', strtotime($today . '-59 day'))) && ($dateValue <= $today);
275
	}
276
277
	/**
278
	 * Last90days operator.
279
	 *
280
	 * @return bool
281
	 */
282
	public function operatorLast90days()
283
	{
284
		$today = date('Y-m-d');
285
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
286
		return ($dateValue >= date('Y-m-d', strtotime($today . '-89 day'))) && ($dateValue <= $today);
287
	}
288
289
	/**
290
	 * Last120days operator.
291
	 *
292
	 * @return bool
293
	 */
294
	public function operatorLast120days()
295
	{
296
		$today = date('Y-m-d');
297
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
298
		return ($dateValue >= date('Y-m-d', strtotime($today . '-119 day'))) && ($dateValue <= $today);
299
	}
300
301
	/**
302
	 * Next15days operator.
303
	 *
304
	 * @return bool
305
	 */
306
	public function operatorNext15days()
307
	{
308
		$today = date('Y-m-d');
309
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
310
		return ($dateValue >= $today) && ($dateValue <= date('Y-m-d', strtotime($today . '+14 day')));
311
	}
312
313
	/**
314
	 * Next30days operator.
315
	 *
316
	 * @return bool
317
	 */
318
	public function operatorNext30days()
319
	{
320
		$today = date('Y-m-d');
321
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
322
		return ($dateValue >= $today) && ($dateValue <= date('Y-m-d', strtotime($today . '+29 day')));
323
	}
324
325
	/**
326
	 * Next60days operator.
327
	 *
328
	 * @return bool
329
	 */
330
	public function operatorNext60days()
331
	{
332
		$today = date('Y-m-d');
333
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
334
		return ($dateValue >= $today) && ($dateValue <= date('Y-m-d', strtotime($today . '+59 day')));
335
	}
336
337
	/**
338
	 * Next90days operator.
339
	 *
340
	 * @return bool
341
	 */
342
	public function operatorNext90days()
343
	{
344
		$today = date('Y-m-d');
345
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
346
		return ($dateValue >= $today) && ($dateValue <= date('Y-m-d', strtotime($today . '+89 day')));
347
	}
348
349
	/**
350
	 * Next120days operator.
351
	 *
352
	 * @return bool
353
	 */
354
	public function operatorNext120days()
355
	{
356
		$today = date('Y-m-d');
357
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
358
		return ($dateValue >= $today) && ($dateValue <= date('Y-m-d', strtotime($today . '+119 day')));
359
	}
360
361
	/**
362
	 * MoreThanDaysAgo operator.
363
	 *
364
	 * @return bool
365
	 */
366
	public function operatorMoreThanDaysAgo()
367
	{
368
		return $this->getValue() <= date('Y-m-d', strtotime('-' . $this->value . ' days'));
369
	}
370
371
	/**
372
	 * Between operator.
373
	 *
374
	 * @return array
375
	 */
376
	public function operatorBw()
377
	{
378
		[$startDate, $endDate] = explode(',', $this->value);
379
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
380
		return ($dateValue >= date('Y-m-d', strtotime($startDate))) && ($dateValue <= date('Y-m-d', strtotime($endDate)));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $dateValue >= dat...', strtotime($endDate)) returns the type boolean which is incompatible with the documented return type array.
Loading history...
381
	}
382
383
	/**
384
	 * Greater operator.
385
	 *
386
	 * @return array
387
	 */
388
	public function operatorGreaterthannow()
389
	{
390
		return $this->operatorGreater();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->operatorGreater() returns the type boolean which is incompatible with the documented return type array.
Loading history...
391
	}
392
393
	/**
394
	 * Smaller operator.
395
	 *
396
	 * @return array
397
	 */
398
	public function operatorSmallerthannow()
399
	{
400
		return $this->operatorSmaller();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->operatorSmaller() returns the type boolean which is incompatible with the documented return type array.
Loading history...
401
	}
402
403
	/**
404
	 * Get value.
405
	 *
406
	 * @return mixed
407
	 */
408
	public function getStdOperator()
409
	{
410
		$dateValue = date('Y-m-d', strtotime($this->getValue()));
411
		$value = \DateTimeRange::getDateRangeByType($this->operator);
412
		if ($value[0] === $value[1]) {
413
			return $dateValue <= $value[0];
414
		}
415
		return ($dateValue >= $value[0]) && ($dateValue <= $value[1]);
416
	}
417
}
418