1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spatie\Typed; |
6
|
|
|
|
7
|
|
|
use Iterator; |
8
|
|
|
use ArrayAccess; |
9
|
|
|
|
10
|
|
|
class Tuple implements ArrayAccess |
11
|
|
|
{ |
12
|
|
|
use ValidatesType; |
13
|
|
|
|
14
|
|
|
/** @var \Spatie\Typed\Type[] */ |
15
|
|
|
private $types; |
16
|
|
|
|
17
|
|
|
/** @var @var array */ |
|
|
|
|
18
|
|
|
private $data; |
19
|
|
|
|
20
|
|
|
public function __construct(Type ...$types) |
21
|
|
|
{ |
22
|
|
|
$this->types = $types; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function set(array $data): self |
26
|
|
|
{ |
27
|
|
|
$iterator = $this->createIterator($data); |
28
|
|
|
|
29
|
|
|
foreach ($iterator as $key => ['type' => $type, 'value' => $value]) { |
30
|
|
|
$data[$key] = $this->validateType($type, $value); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->data = $data; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function offsetGet($offset) |
39
|
|
|
{ |
40
|
|
|
return isset($this->data[$offset]) ? $this->data[$offset] : null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function offsetSet($offset, $value) |
44
|
|
|
{ |
45
|
|
|
if ($offset === null || ! is_numeric($offset)) { |
46
|
|
|
throw WrongType::fromMessage('You must specify a numeric offset'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$type = $this->types[$offset] ?? null; |
50
|
|
|
|
51
|
|
|
if (! $type) { |
52
|
|
|
throw WrongType::fromMessage("No type was configured for this tuple at offset {$offset}"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->data[$offset] = $this->validateType($type, $value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function offsetExists($offset) |
59
|
|
|
{ |
60
|
|
|
return array_key_exists($offset, $this->data); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function offsetUnset($offset) |
64
|
|
|
{ |
65
|
|
|
throw WrongType::fromMessage('Tuple values cannot be unset'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function toArray(): array |
69
|
|
|
{ |
70
|
|
|
return $this->data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function createIterator(array $data): Iterator |
74
|
|
|
{ |
75
|
|
|
return new class($this->types, $data) implements Iterator { |
76
|
|
|
/** @var array */ |
77
|
|
|
private $types; |
78
|
|
|
|
79
|
|
|
/** @var array */ |
80
|
|
|
private $data; |
81
|
|
|
|
82
|
|
|
/** @var int */ |
83
|
|
|
private $position; |
84
|
|
|
|
85
|
|
|
public function __construct(array $types, array $data) |
86
|
|
|
{ |
87
|
|
|
$typeCount = count($types); |
88
|
|
|
|
89
|
|
|
$dataCount = count($data); |
90
|
|
|
|
91
|
|
|
if ($typeCount !== $dataCount) { |
92
|
|
|
throw WrongType::fromMessage("Tuple count mismatch, excpected exactly {$typeCount} elements, and got {$dataCount}"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->types = $types; |
96
|
|
|
$this->data = $data; |
97
|
|
|
$this->position = 0; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function current(): array |
101
|
|
|
{ |
102
|
|
|
return ['type' => current($this->types), 'value' => current($this->data)]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function next(): void |
106
|
|
|
{ |
107
|
|
|
$this->position++; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function key(): int |
111
|
|
|
{ |
112
|
|
|
return $this->position; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function valid(): bool |
116
|
|
|
{ |
117
|
|
|
return isset($this->types[$this->position]) && array_key_exists($this->position, $this->data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function rewind(): void |
121
|
|
|
{ |
122
|
|
|
$this->position = 0; |
123
|
|
|
} |
124
|
|
|
}; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|