-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shipper.php
172 lines (127 loc) Β· 2.78 KB
/
Shipper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Created by PhpStorm.
* User: Galek
* Date: 9.3.2018
*/
declare(strict_types = 1);
namespace Galek\Utils\Calendar\Business;
use Galek\Utils\Calendar\Calendar;
use Galek\Utils\Calendar\Day;
use Galek\Utils\Calendar\Configuration\Localization;
use Galek\Utils\Calendar\Time;
class Shipper implements IShipper
{
/**
* @var string
*/
private $name;
/**
* @var int
*/
private $hour;
/**
* @var int
*/
private $minute;
/**
* @var bool
*/
private $weekend;
/**
* @var \Galek\Utils\Calendar\Configuration\Localization
*/
private $configuration;
/**
* @var int
*/
private $deliveryTime;
/**
* @var Calendar
*/
private $currentDate;
public function __construct(string $name, Localization $configuration, int $hour, int $minute, bool $weekend = false, int $deliveryTime = 1)
{
$this->setTime($hour, $minute);
$this->enableWeekend($weekend);
$this->configuration = $configuration;
$this->deliveryTime = $deliveryTime;
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function getCurrentDate(): Calendar
{
if ($this->currentDate) {
return $this->currentDate;
}
$date = new Calendar('now', null, $this->configuration);
return $date;
}
public function setCurrentDate(Calendar $date = null): void
{
$this->currentDate = clone $date;
}
public function getDate(): Calendar
{
$date = $this->getCurrentDate();
if (Day::isWork($this->configuration->getHolidays(), $date)) {
if (Time::over($date, $this->hour, $this->minute)) {
$date->modify('+1 days');
}
if ($this->weekend === false) {
$date = $date->getWorkDay();
}
} else {
if ($this->weekend === false) {
$date = $date->getWorkDay();
}
}
$date->modify('+' . $this->deliveryTime . ' days');
if ($this->weekend === false) {
$date = $date->getWorkDay();
}
//return $date->getWorkDay();
return $date;
}
public function getDeliveryTextDate(string $format = 'Y.m.d'): string
{
$date = $this->getDate();
$dayNumber = $date->dayNumber();
$local = $this->configuration->getLocalization();
$at = $local->getAt($dayNumber);
$day = $local->getInflexion($dayNumber, 4);
return $at . ' ' . $day . ' ' . $date->format($format);
}
public function setTime(int $hour, int $minute): void
{
$this->setHour($hour);
$this->setMinute($minute);
}
public function setHour(int $hour): void
{
$this->hour = $hour;
}
public function getHour(): int
{
return $this->hour;
}
public function setMinute(int $minute): void
{
$this->minute = $minute;
}
public function getMinute(): int
{
return $this->minute;
}
public function enableWeekend($value = true): void
{
$this->weekend = $value;
}
public function disableWeekend(): void
{
$this->enableWeekend(false);
}
}