-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequenceData.php
More file actions
182 lines (144 loc) · 3.25 KB
/
SequenceData.php
File metadata and controls
182 lines (144 loc) · 3.25 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace SyncEngine\Structure\Data;
/**
* @template T
*/
class SequenceData implements \ArrayAccess, \Countable, \SeekableIterator
{
private int $current = 0;
private array $sequence = [];
/**
* @var string[]
*/
private array $refs = [];
private $initCallback;
public function __construct( array $sequence = [], ?callable $initCallback = null )
{
foreach ( $sequence as $index => $step ) {
$this->add( $index, $step );
}
$this->initCallback = $initCallback;
}
public function add( int $index, mixed $step ): static
{
if ( is_array( $step ) || is_object( $step ) ) {
$resource = ResourceData::create( $step );
$ref = $resource->get( '_ref' ) ?: $resource->get( 'ref' ) ?: null;
}
$this->refs[ $index ] = (string) ( $ref ?? $index );
$this->sequence[ $index ] = $step;
return $this;
}
public function remove( int|string $ref ): static
{
$index = $this->getIndex( $ref );
unset( $this->sequence[ $index ] );
unset( $this->refs[ $index ] );
return $this;
}
/**
* @param int|string $ref
*
* @return T
*/
public function init( int|string $ref ): mixed
{
$index = $this->getIndex( $ref );
$ref = $this->getRef( $index );
$config = $this->_get( $index );
$initialized = $this->initCallback ? call_user_func( $this->initCallback, $config, $ref, $index ) : $config;
$this->sequence[ $index ] = $initialized;
return $initialized;
}
/**
* @param int|string $ref
*
* @return T
*/
public function get( int|string $ref ): mixed
{
return $this->init( $ref );
}
public function _get( int|string $ref ): mixed
{
return $this->sequence[ $this->getIndex( $ref ) ] ?? null;
}
public function getIndex( int|string $ref ): ?int
{
if ( is_int( $ref ) ) {
return $ref;
}
return array_search( $ref, $this->refs, true );
}
public function getRefs(): array
{
return $this->refs;
}
public function getRef( int $offset ): ?string
{
return $this->refs[ $offset ] ?? null;
}
public function offsetExists( mixed $offset ): bool
{
if ( is_int( $offset ) ) {
return isset( $this->sequence[ $offset ] );
}
return in_array( $offset, $this->refs, true );
}
public function offsetGet( mixed $offset ): ?array
{
return $this->get( $offset );
}
public function offsetSet( mixed $offset, mixed $value ): void
{
$this->add( $offset, $value );
}
public function offsetUnset( mixed $offset ): void
{
$this->remove( $offset );
}
public function count(): int
{
return count( $this->sequence );
}
public function normalize(): array
{
return $this->sequence;
}
public function current(): mixed
{
return $this->get( $this->current );
}
public function previous(): void
{
--$this->current;
}
public function next(): void
{
++$this->current;
}
public function ref(): string
{
return $this->refs[ $this->current ];
}
public function key(): mixed
{
return $this->current;
}
public function valid(): bool
{
return $this->offsetExists( $this->current );
}
public function rewind(): void
{
$this->current = 0;
}
public function seek( int|string $offset ): void
{
$index = $this->getIndex( $offset );
if ( null === $index ) {
throw new \OutOfBoundsException("invalid seek position ($offset)");
}
$this->current = $index;
}
}