-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCollection.php
More file actions
109 lines (94 loc) Β· 2.09 KB
/
Collection.php
File metadata and controls
109 lines (94 loc) Β· 2.09 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
<?php
declare(strict_types=1);
namespace PHPJava\Kernel\Types\Array_;
use PHPJava\Kernel\Filters\Normalizer;
use PHPJava\Kernel\Resolvers\TypeResolver;
use PHPJava\Packages\java\lang\ArrayIndexOutOfBoundsException;
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
{
/**
* @var array
*/
private $data;
/**
* @var int
*/
private $position = 0;
/**
* @var string
*/
private $type;
public function __construct(array &$data = [])
{
$this->data = $data;
}
public function setType(string $type = null): self
{
$this->type = $type;
return $this;
}
public function getType($default = null): ?string
{
if (!isset($this->data[0])) {
return $this->type ?? $default;
}
return TypeResolver::resolveFromPHPType(
Normalizer::getPrimitiveValue($this->data[0])
) ?? $this->type ?? $default;
}
public function __toString(): string
{
return implode($this->data);
}
public function toArray(): array
{
return $this->data;
}
/**
* @param int $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
/**
* @param int $offset
*/
public function offsetGet($offset)
{
if (!$this->offsetExists($offset)) {
throw new ArrayIndexOutOfBoundsException($offset);
}
return $this->data[$offset];
}
/**
* @param int $offset
*/
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
public function offsetSet($offset, $value)
{
if ($offset === null) {
$this->data[] = $value;
return;
}
$this->data[$offset] = $value;
}
/**
* @return int
*/
public function count()
{
return count($this->data);
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->data);
}
}