-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestParams.php
96 lines (74 loc) · 2.26 KB
/
RequestParams.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
<?php
declare(strict_types=1);
namespace Manyou\BingHomepage;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
class RequestParams
{
private Market $market;
private DateTimeImmutable $date;
private int $offset;
private function __construct()
{
}
public static function createToday(Market $market, ?DateTimeImmutable $today = null): self
{
$instance = new self();
$instance->market = $market;
$instance->date = $market->getToday($today);
$instance->offset = 0;
return $instance;
}
public static function create(Market $market, DateTimeInterface $date, ?DateTimeImmutable $today = null): self
{
$instance = new self();
$instance->market = $market;
$instance->date = $market->getDate($date);
$instance->offset = $instance->getDaysAgo($today);
return $instance;
}
public static function createFromOffset(Market $market, int $offset, ?DateTimeImmutable $today = null): self
{
$instance = new self();
$instance->market = $market;
$instance->date = $market->getDateBefore($offset, $today);
$instance->offset = $offset;
return $instance;
}
/** Get how many days ago was "$date" */
public function getDaysAgo(?DateTimeImmutable $today = null): int
{
$today = $this->market->getToday($today);
$diff = $this->date->diff($today, false);
return (int) $diff->format('%r%a');
}
public function isDateExpected(DateTimeInterface $date): bool
{
return $date->format('Y-m-d') === $this->date->format('Y-m-d');
}
public function isTimeZoneOffsetExpected(DateTimeInterface $date): bool
{
return $date->format('Z') === $this->date->format('Z');
}
public function getMarketTimeZone(): Market
{
return $this->market;
}
public function getMarket(): string
{
return $this->market->getName();
}
public function getTimeZone(): DateTimeZone
{
return $this->market->getTimeZone();
}
public function getOffset(): int
{
return $this->offset;
}
public function getDate(): DateTimeImmutable
{
return $this->date;
}
}