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