|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Buffertools; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Buffertools\Types\TypeInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Template implements \Countable |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var TypeInterface[] |
|
13
|
|
|
*/ |
|
14
|
|
|
private $template = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param array $items |
|
18
|
|
|
*/ |
|
19
|
70 |
|
public function __construct(array $items = []) |
|
20
|
|
|
{ |
|
21
|
70 |
|
foreach ($items as $item) { |
|
22
|
10 |
|
$this->addItem($item); |
|
23
|
|
|
} |
|
24
|
70 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
* @see \Countable::count() |
|
29
|
|
|
* @return int |
|
30
|
|
|
*/ |
|
31
|
56 |
|
public function count(): int |
|
32
|
|
|
{ |
|
33
|
56 |
|
return count($this->template); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Return an array of type serializers in the template |
|
38
|
|
|
* |
|
39
|
|
|
* @return Types\TypeInterface[] |
|
40
|
|
|
*/ |
|
41
|
60 |
|
public function getItems(): array |
|
42
|
|
|
{ |
|
43
|
60 |
|
return $this->template; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Add a new TypeInterface to the Template |
|
48
|
|
|
* |
|
49
|
|
|
* @param TypeInterface $item |
|
50
|
|
|
* @return Template |
|
51
|
|
|
*/ |
|
52
|
66 |
|
public function addItem(TypeInterface $item): Template |
|
53
|
|
|
{ |
|
54
|
66 |
|
$this->template[] = $item; |
|
55
|
66 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Parse a sequence of objects from binary, using the current template. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Parser $parser |
|
62
|
|
|
* @return mixed[]|Buffer[]|int[]|string[] |
|
63
|
|
|
*/ |
|
64
|
6 |
|
public function parse(Parser $parser): array |
|
65
|
|
|
{ |
|
66
|
6 |
|
if (0 == count($this->template)) { |
|
67
|
2 |
|
throw new \RuntimeException('No items in template'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
$values = []; |
|
71
|
4 |
|
foreach ($this->template as $reader) { |
|
72
|
4 |
|
$values[] = $reader->read($parser); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
return $values; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Write the array of $items to binary according to the template. They must |
|
80
|
|
|
* each be an instance of Buffer or implement SerializableInterface. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $items |
|
83
|
|
|
* @return BufferInterface |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function write(array $items): BufferInterface |
|
86
|
|
|
{ |
|
87
|
4 |
|
if (count($items) != count($this->template)) { |
|
88
|
2 |
|
throw new \RuntimeException('Number of items must match template'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$binary = ''; |
|
92
|
|
|
|
|
93
|
2 |
|
foreach ($this->template as $serializer) { |
|
94
|
2 |
|
$item = array_shift($items); |
|
95
|
2 |
|
$binary .= $serializer->write($item); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return new Buffer($binary); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|