1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DJStarCOM\BookingComSDK\Models; |
4
|
|
|
|
5
|
|
|
use Countable; |
6
|
|
|
use Iterator; |
7
|
|
|
use function count; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Result |
11
|
|
|
* @package DJStarCOM\BookingComSDK\Models |
12
|
|
|
*/ |
13
|
|
|
class Result implements Iterator, Countable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Collections list. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $collections = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Model $item |
24
|
|
|
* @param $key string Item key in collection |
25
|
|
|
*/ |
26
|
|
|
public function addItem(Model $item, ?string $key = null): void |
27
|
|
|
{ |
28
|
|
|
if ($key) { |
29
|
|
|
$this->collections[$key] = $item; |
30
|
|
|
} else { |
31
|
|
|
$this->collections[] = $item; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get Model by key from collection |
37
|
|
|
* @param string $key |
38
|
|
|
* @return Model|null |
39
|
|
|
*/ |
40
|
|
|
public function getBy(string $key): ?Model |
41
|
|
|
{ |
42
|
|
|
if (!array_key_exists($key, $this->collections)) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->collections[$key]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return Model[] |
51
|
|
|
*/ |
52
|
|
|
public function getItems(): array |
53
|
|
|
{ |
54
|
|
|
return $this->collections; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Reset collection. |
59
|
|
|
* @return Model |
60
|
|
|
*/ |
61
|
|
|
public function rewind(): Model |
62
|
|
|
{ |
63
|
|
|
return reset($this->collections); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Model |
68
|
|
|
*/ |
69
|
|
|
public function current(): Model |
70
|
|
|
{ |
71
|
|
|
return current($this->collections); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return int|null|string |
76
|
|
|
*/ |
77
|
|
|
public function key() |
78
|
|
|
{ |
79
|
|
|
return key($this->collections); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
|
|
public function next() |
86
|
|
|
{ |
87
|
|
|
return next($this->collections); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function valid(): bool |
94
|
|
|
{ |
95
|
|
|
return key($this->collections) !== null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Count elements of an object |
100
|
|
|
* @return int The custom count as an integer. |
101
|
|
|
* The return value is cast to an integer. |
102
|
|
|
*/ |
103
|
|
|
public function count(): int |
104
|
|
|
{ |
105
|
|
|
return count($this->collections); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|