1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TrelloCycleTime\ValueObject; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TimeCard implements \JsonSerializable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $id; |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $title; |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $cycleTimes; |
22
|
|
|
|
23
|
5 |
|
private function __construct(string $id, string $title) |
24
|
|
|
{ |
25
|
5 |
|
$this->id = $id; |
26
|
5 |
|
$this->title = $title; |
27
|
5 |
|
$this->cycleTimes = []; |
28
|
5 |
|
} |
29
|
|
|
|
30
|
5 |
|
public static function create( |
31
|
|
|
string $cardId, |
32
|
|
|
string $cardTitle |
33
|
|
|
): TimeCard |
34
|
|
|
{ |
35
|
5 |
|
return new self($cardId, $cardTitle); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
4 |
|
public function getId(): string |
42
|
|
|
{ |
43
|
4 |
|
return $this->id; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
2 |
|
public function getTitle(): string |
50
|
|
|
{ |
51
|
2 |
|
return $this->title; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
2 |
|
public function getCycleTimes(): array |
58
|
|
|
{ |
59
|
2 |
|
return $this->cycleTimes; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $fromKey |
64
|
|
|
* @param string $toKey |
65
|
|
|
* @return float|null |
66
|
|
|
*/ |
67
|
1 |
|
public function getCycleTimesByFromAndTo(string $fromKey, string $toKey): ?float |
68
|
|
|
{ |
69
|
1 |
|
foreach ($this->cycleTimes as $cycleTime) { |
70
|
1 |
|
if ($cycleTime->getFrom() === $fromKey && $cycleTime->getTo() === $toKey) { |
71
|
1 |
|
return $cycleTime->getDays(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
public function setCycleTimesByFromAndTo(string $from, string $to, float $days) |
79
|
|
|
{ |
80
|
3 |
|
$this->cycleTimes[] = CycleTime::createWithDays($from, $to, $days); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
2 |
|
public function calculateDayDifferenceBetweenColumns( |
84
|
|
|
string $fromKey, |
85
|
|
|
string $fromDate, |
86
|
|
|
string $toKey, |
87
|
|
|
string $toDate |
88
|
|
|
) |
89
|
|
|
{ |
90
|
2 |
|
$dateDifference = strtotime($toDate) - strtotime($fromDate); |
91
|
|
|
|
92
|
2 |
|
$dayDifference = round($dateDifference / (60 * 60 * 24)); |
93
|
2 |
|
$this->setCycleTimesByFromAndTo($fromKey, $toKey, $dayDifference); |
94
|
2 |
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Specify data which should be serialized to JSON |
99
|
|
|
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
100
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
101
|
|
|
* which is a value of any type other than a resource. |
102
|
|
|
* @since 5.4.0 |
103
|
|
|
*/ |
104
|
2 |
|
public function jsonSerialize() |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
2 |
|
'id' => $this->id, |
108
|
2 |
|
'title' => $this->title, |
109
|
2 |
|
'cycleTimes' => $this->cycleTimes |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
} |