-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAbstractModelCollection.php
90 lines (78 loc) · 1.52 KB
/
AbstractModelCollection.php
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
<?php declare(strict_types=1);
namespace MyENA\RGW;
/**
* Class AbstractArrayModel
* @package MyENA\RGW
*/
abstract class AbstractModelCollection extends AbstractModel implements \Iterator, \Countable
{
/** @var array */
private $data = [];
/**
* AbstractArrayResponse constructor.
* @param array $data
*/
public function __construct(array $data = [])
{
$class = $this->containedType();
foreach ($data as $datum) {
$this->data[] = new $class($datum);
}
}
/**
* Must return the fully qualified class name of the contained object
*
* @return string
*/
abstract protected function containedType(): string;
/**
* @return mixed
*/
public function first()
{
return $this->data[0] ?? null;
}
/**
* @return mixed
*/
public function current()
{
return current($this->data);
}
public function next()
{
next($this->data);
}
/**
* @return int|mixed|null|string
*/
public function key()
{
return key($this->data);
}
/**
* @return bool
*/
public function valid()
{
return null !== key($this->data);
}
public function rewind()
{
reset($this->data);
}
/**
* @return int
*/
public function count()
{
return count($this->data);
}
/**
* @return array
*/
public function jsonSerialize()
{
return $this->data;
}
}