|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of forecast.it.fill project. |
|
7
|
|
|
* (c) Patrick Jaja <[email protected]> |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ForecastAutomation\Activity\Shared\Dto; |
|
13
|
|
|
|
|
14
|
|
|
use ArrayAccess; |
|
15
|
|
|
use Iterator; |
|
16
|
|
|
|
|
17
|
|
|
class ActivityDtoCollection implements Iterator, ArrayAccess |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \ForecastAutomation\Activity\Shared\Dto\ActivityDto[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private array $activityDtos; |
|
23
|
|
|
|
|
24
|
|
|
private int $position = 0; |
|
25
|
|
|
|
|
26
|
4 |
|
public function __construct( |
|
27
|
|
|
ActivityDto ...$activityDtos |
|
28
|
|
|
) { |
|
29
|
4 |
|
$this->activityDtos = $activityDtos; |
|
30
|
4 |
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
public function current(): ActivityDto |
|
33
|
|
|
{ |
|
34
|
2 |
|
return $this->activityDtos[$this->position]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
public function next(): void |
|
38
|
|
|
{ |
|
39
|
2 |
|
++$this->position; |
|
40
|
2 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function key(): int |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->position; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function valid(): bool |
|
48
|
|
|
{ |
|
49
|
2 |
|
return \array_key_exists($this->position, $this->activityDtos); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function rewind(): void |
|
53
|
|
|
{ |
|
54
|
2 |
|
$this->position = 0; |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function offsetExists($offset): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return \array_key_exists($offset, $this->activityDtos); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function offsetGet($offset): mixed |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->activityDtos[$offset] ?? null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function offsetSet($offset, $value): void |
|
68
|
|
|
{ |
|
69
|
|
|
if (null === $offset) { |
|
70
|
|
|
$this->activityDtos[] = $value; |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->activityDtos[$offset] = $value; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function offsetUnset($offset): void |
|
77
|
|
|
{ |
|
78
|
|
|
unset($this->activityDtos[$offset]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function merge(self $activityDtoCollection): self |
|
82
|
|
|
{ |
|
83
|
1 |
|
foreach ($activityDtoCollection as $activityDto) { |
|
84
|
1 |
|
if (!$this->sumDurationIfExist($activityDto)) { |
|
85
|
1 |
|
$this->activityDtos[] = $activityDto; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
private function sumDurationIfExist(ActivityDto $activityDto): bool |
|
93
|
|
|
{ |
|
94
|
1 |
|
$exist = false; |
|
95
|
1 |
|
foreach ($this->activityDtos as $storedActivityDto) { |
|
96
|
|
|
if (strtolower($storedActivityDto->needle) === (strtolower($activityDto->needle))) { |
|
97
|
|
|
$storedActivityDto->duration += $activityDto->duration; |
|
98
|
|
|
$exist = true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
return $exist; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|