|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the sj-i/typed-cdata package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace TypedCData; |
|
15
|
|
|
|
|
16
|
|
|
use FFI\CData; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @template T of TypedCDataInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
class TypedCDataArray implements \ArrayAccess |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var \FFI\CDataArray */ |
|
24
|
|
|
private CData $cdata; |
|
25
|
|
|
/** @var class-string<T> $typed_cdata_class */ |
|
|
|
|
|
|
26
|
|
|
private string $typed_cdata_class; |
|
27
|
|
|
/** @var array<int, T> */ |
|
28
|
|
|
private array $typed_element_cache; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* TypedCDataArray constructor. |
|
32
|
|
|
* @param \FFI\CDataArray $cdata |
|
33
|
|
|
* @param class-string<T> $typed_cdata_class |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(CData $cdata, string $typed_cdata_class) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->cdata = $cdata; |
|
|
|
|
|
|
38
|
|
|
$this->typed_cdata_class = $typed_cdata_class; |
|
39
|
|
|
$this->typed_element_cache = []; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function offsetExists($offset) |
|
43
|
|
|
{ |
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param int $offset |
|
49
|
|
|
*/ |
|
50
|
|
|
public function offsetGet($offset): TypedCDataInterface |
|
51
|
|
|
{ |
|
52
|
|
|
if (!isset($this->typed_element_cache[$offset])) { |
|
53
|
|
|
$this->typed_element_cache[$offset] = $this->typed_cdata_class::fromCData($this->cdata[$offset]); |
|
54
|
|
|
} |
|
55
|
|
|
return $this->typed_element_cache[$offset]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param int $offset |
|
60
|
|
|
* @param TypedCDataInterface $value |
|
61
|
|
|
*/ |
|
62
|
|
|
public function offsetSet($offset, $value) |
|
63
|
|
|
{ |
|
64
|
|
|
$value->toCData($this->cdata[$offset]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function offsetUnset($offset) |
|
68
|
|
|
{ |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @template T2 of TypedCDataInterface |
|
73
|
|
|
* @param \FFI\CDataArray $cdata |
|
74
|
|
|
* @param class-string<T2> $typed_cdata_class |
|
|
|
|
|
|
75
|
|
|
* @return self<T2> |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function fromCData(CData $cdata, string $typed_cdata_class): TypedCDataArray |
|
78
|
|
|
{ |
|
79
|
|
|
return new self($cdata, $typed_cdata_class); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function toCData(?CData $cdata): CData |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($this->typed_element_cache as $offset => $item) { |
|
85
|
|
|
assert($item instanceof TypedCDataInterface); |
|
86
|
|
|
$item->toCData($this->cdata[$offset]); |
|
87
|
|
|
} |
|
88
|
|
|
return $this->cdata; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|