|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ayaml; |
|
4
|
|
|
|
|
5
|
|
|
use Ayaml\Sequence\Calculator\Numeric\Decrementer as NumericDecrementer; |
|
6
|
|
|
use Ayaml\Sequence\Calculator\Numeric\Incrementer as NumericIncrementer; |
|
7
|
|
|
use Ayaml\Sequence\Calculator\Datetime\Decrementer as DatetimeDecrementer; |
|
8
|
|
|
use Ayaml\Sequence\Calculator\Datetime\Incrementer as DatetimeIncrementer; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ContainerCollection |
|
13
|
|
|
* |
|
14
|
|
|
* @package Ayaml |
|
15
|
|
|
*/ |
|
16
|
|
|
class ContainerCollection |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
public $index = 0; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Container[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $containers = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Container |
|
30
|
|
|
*/ |
|
31
|
|
|
private $baseContainer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Container $container |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Container $container) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->baseContainer = $container; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $targetKey |
|
43
|
|
|
* @param $start |
|
44
|
|
|
* @param $end |
|
45
|
|
|
* @return NumericDecrementer|NumericIncrementer |
|
46
|
|
|
* @throws AyamlSeqInvalidTypeException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function range($targetKey, $start, $end) |
|
49
|
|
|
{ |
|
50
|
|
|
if (! is_numeric($start) || ! is_numeric($end)) { |
|
51
|
|
|
throw new AyamlSeqInvalidTypeException("'range' expects numeric var"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->add($this->baseContainer->with([$targetKey => $start])); |
|
55
|
|
|
if ($start <= $end) { |
|
56
|
|
|
return new NumericIncrementer($this, $targetKey, $start, $end); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return new NumericDecrementer($this, $targetKey, $start, $end); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $targetKey |
|
64
|
|
|
* @param $from |
|
65
|
|
|
* @param $to |
|
66
|
|
|
* @return DatetimeDecrementer|DatetimeIncrementer |
|
67
|
|
|
* @throws AyamlSeqInvalidTypeException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function between($targetKey, $from, $to) |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
|
|
$fromDate = Carbon::parse($from); |
|
73
|
|
|
$toDate = Carbon::parse($to); |
|
74
|
|
|
} catch (\Exception $e) { |
|
75
|
|
|
throw new AyamlSeqInvalidTypeException("'between' expects date format string / " . $e->getMessage()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->add($this->baseContainer->with([$targetKey => $fromDate->toDateTimeString()])); |
|
79
|
|
|
if ($fromDate->lte($toDate)) { |
|
80
|
|
|
return new DatetimeIncrementer($this, $targetKey, $fromDate, $toDate); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return new DatetimeDecrementer($this, $targetKey, $fromDate, $toDate); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function dump() |
|
90
|
|
|
{ |
|
91
|
|
|
$containersArray = []; |
|
92
|
|
|
foreach ($this->containers as $container) { |
|
93
|
|
|
$containersArray[] = $container->dump(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $containersArray; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Container $container |
|
101
|
|
|
*/ |
|
102
|
|
|
public function add(Container $container) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->containers[$this->index] = $container; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param int|null $index |
|
109
|
|
|
* @return Container |
|
110
|
|
|
*/ |
|
111
|
|
|
public function get($index = null) |
|
112
|
|
|
{ |
|
113
|
|
|
$index = is_null($index) ? $this->index : $index; |
|
114
|
|
|
|
|
115
|
|
|
if (isset($this->containers[$index])) { |
|
116
|
|
|
return $this->containers[$index]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return clone $this->baseContainer; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return Container[] |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getAll() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->containers; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|