forked from aternosorg/php-model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelCollection.php
More file actions
144 lines (128 loc) · 2.51 KB
/
ModelCollection.php
File metadata and controls
144 lines (128 loc) · 2.51 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Aternos\Model;
use ArrayAccess;
use Countable;
use Iterator;
/**
* Class ModelCollection
*
* @template TModel of ModelInterface
* @package Aternos\Model
*/
class ModelCollection implements Iterator, Countable, ArrayAccess
{
protected array $models;
protected int $iterator = 0;
/**
* ModelCollection constructor.
*
* @param TModel[] $models
*/
public function __construct(array $models = [])
{
$this->models = $models;
}
/**
* Add a model
*
* @param TModel $model
* @noinspection PhpDocSignatureInspection
*/
public function add(ModelInterface $model)
{
$this->models[] = $model;
}
/**
* Return the current element
*
* @return TModel
*/
public function current(): ModelInterface
{
return $this->models[$this->iterator];
}
/**
* Move forward to next element
*
* @return void
*/
public function next(): void
{
$this->iterator++;
}
/**
* Return the key of the current element
*
* @return int
*/
public function key(): int
{
return $this->iterator;
}
/**
* Checks if current position is valid
*
* @return boolean
*/
public function valid(): bool
{
return array_key_exists($this->iterator, $this->models);
}
/**
* Rewind the Iterator to the first element
*
* @return void
*/
public function rewind(): void
{
$this->iterator = 0;
}
/**
* Count elements of an object
*
* @return int
*/
public function count(): int
{
return count($this->models);
}
/**
* Whether an offset exists
*
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->models[$offset]);
}
/**
* Offset to retrieve
*
* @param mixed $offset
* @return TModel
*/
public function offsetGet(mixed $offset): ModelInterface
{
return $this->models[$offset];
}
/**
* Offset to set
*
* @param mixed $offset
* @param TModel $value
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->models[$offset] = $value;
}
/**
* Offset to unset
*
* @param mixed $offset
*/
public function offsetUnset(mixed $offset): void
{
unset($this->models[$offset]);
}
}