1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Domain Policy Class |
5
|
|
|
* @package Ticaje_BookingApi |
6
|
|
|
* @author Hector Luis Barrientos <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS; |
10
|
|
|
|
11
|
|
|
use DatePeriod; |
12
|
|
|
use DateTimeInterface; |
13
|
|
|
use Ticaje\BookingApi\Domain\Signatures\PeriodSignature; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Query |
17
|
|
|
* @package Ticaje\BookingApi\Domain\Policies\Calendar\Disabling\CQRS |
18
|
|
|
*/ |
19
|
|
|
class Query implements QuerySignature |
20
|
|
|
{ |
21
|
|
|
use Validator; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
private $list = []; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $format; |
28
|
|
|
|
29
|
|
|
/** @var Constraint\WeekDays */ |
30
|
|
|
private $weekDaysConstraint; |
31
|
|
|
|
32
|
|
|
/** @var Constraint\Period */ |
33
|
|
|
private $periodConstraint; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Query constructor. |
37
|
|
|
* |
38
|
|
|
* @param Constraint\WeekDays $weekDaysConstraint |
39
|
|
|
* @param Constraint\Period $periodConstraint |
40
|
|
|
* @param string $format |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
Constraint\WeekDays $weekDaysConstraint, |
44
|
|
|
Constraint\Period $periodConstraint, |
45
|
|
|
string $format = PeriodSignature::DEFAULT_FORMAT |
46
|
|
|
) { |
47
|
|
|
$this->format = $format; |
48
|
|
|
$this->weekDaysConstraint = $weekDaysConstraint; |
49
|
|
|
$this->periodConstraint = $periodConstraint; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
|
|
public function interpret(string $rule, string $type): QuerySignature |
56
|
|
|
{ |
57
|
|
|
$decoded = $this->decode($rule); |
58
|
|
|
$this->validate($decoded, $type); |
59
|
|
|
$result = [ |
60
|
|
|
'single' => (function () use ($decoded) { |
61
|
|
|
$period = $this->periodConstraint->extract([ |
62
|
|
|
'from' => $decoded['date'], |
63
|
|
|
'to' => $decoded['date'], |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
return $this->extract($period); |
|
|
|
|
67
|
|
|
}), |
68
|
|
|
'period' => (function () use ($decoded) { |
69
|
|
|
$period = $this->periodConstraint->extract($decoded); |
70
|
|
|
|
71
|
|
|
return $this->extract($period); |
|
|
|
|
72
|
|
|
}), |
73
|
|
|
'recurrent_day' => (function () use ($decoded) { |
74
|
|
|
$dayOfWeek = jddayofweek($decoded['dayOfWeek'] - 1, 1); |
75
|
|
|
$period = $this->weekDaysConstraint->extract([ |
76
|
|
|
'weekDay' => $dayOfWeek, |
77
|
|
|
'year' => date("Y"), |
78
|
|
|
'month' => date("m"), |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
return $this->extract($period); |
|
|
|
|
82
|
|
|
}), |
83
|
|
|
]; |
84
|
|
|
$result[$type](); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function fetchList(): array |
93
|
|
|
{ |
94
|
|
|
usort($this->list, function ($previous, $next) { |
95
|
|
|
$previousDate = strtotime($previous); |
96
|
|
|
$nextDate = strtotime($next); |
97
|
|
|
|
98
|
|
|
return ($previousDate - $nextDate); |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
return $this->list; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $rule |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
* It's separated since it could change |
109
|
|
|
*/ |
110
|
|
|
private function decode($rule) |
111
|
|
|
{ |
112
|
|
|
return json_decode($rule, true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param DatePeriod $period |
117
|
|
|
* Given a period, extract proper dates based upon two constraints: greater than current date and not existing |
118
|
|
|
* in current list |
119
|
|
|
*/ |
120
|
|
|
private function extract(DatePeriod $period) |
121
|
|
|
{ |
122
|
|
|
$today = date($this->format); |
123
|
|
|
/** |
124
|
|
|
* @var $key |
125
|
|
|
* @var DateTimeInterface $date |
126
|
|
|
*/ |
127
|
|
|
foreach ($period as $key => $date) { |
128
|
|
|
$value = $date->format($this->format); |
129
|
|
|
if ($value >= $today && !in_array($value, $this->list)) { |
130
|
|
|
$this->list[] = $value; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.