This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Block.php
executable file
·190 lines (163 loc) · 3.82 KB
/
Block.php
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
183
184
185
186
187
188
189
190
<?php
/**
* This file is part of the ChaosTangent PHP-ASS package
*
* (c) John Noel <john.noel@chaostangent.com>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code
*/
namespace ChaosTangent\ASS\Block;
use ChaosTangent\ASS\Line\Line;
use ChaosTangent\ASS\Exception\InvalidBlockException;
/**
* ASS script block base class
*
* @author John Noel <john.noel@chaostangent.com>
* @package php-ass
*/
abstract class Block implements \IteratorAggregate, \ArrayAccess
{
/** @var string */
protected $id;
/** @var array */
protected $lines = [];
/** @var array */
protected static $classMap = [
'Script Info' => ScriptInfo::class,
'V4+ Styles' => Styles::class,
'Events' => Events::class,
//'Fonts' => null,
//'Graphics' => null,
];
/**
* Whether the passed line is a block header
*
* @param string $line
* @return boolean
*/
public static function isBlockHeader($line)
{
return preg_match('/^\[[^\]]+\]\s*$/', $line) === 1;
}
/**
* Parse an array of lines into this block
*
* @param array $lines Raw (string) lines
* @return Block|null
* @throws InvalidBlockException
*/
public static function parse(array $lines)
{
$rawId = $lines[0];
$matches = [];
if (preg_match('/^\[([^\]]+)\]\s*$/', $rawId, $matches) !== 1) {
throw new InvalidBlockException(null, 'Cannot read ID for block, first line: '.$rawId);
}
$id = $matches[1];
// don't recognise the key or not yet implemented
if (!array_key_exists($id, self::$classMap)) {
return null;
}
$block = new self::$classMap[$id];
$block->setId($id);
$block->doParse(array_slice($lines, 1));
return $block;
}
/**
* Add a line
*
* @param Line $line
* @return Block
*/
public function addLine(Line $line)
{
$this->lines[] = $line;
return $this;
}
/**
* Remove a line
*
* @param Line $line
* @return Block
*/
public function removeLine(Line $line)
{
$k = array_search($line, $this->lines, true);
if ($k !== false) {
unset($this->lines[$k]);
}
return $this;
}
/**
* @return Line[]
*/
public function getLines()
{
return $this->lines;
}
/**
* @param string $id
* @return Block
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritDoc}
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->lines);
}
/**
* {@inheritDoc}
*/
public function offsetGet($offset)
{
return $this->lines[$offset];
}
/**
* {@inheritDoc}
*/
public function offsetSet($offset, $value)
{
if (!($value instanceof Line)) {
throw new \InvalidArgumentException('You must only set Lines using array access');
}
if ($offset === null) {
$this->lines[] = $value;
} else {
$this->lines[$offset] = $value;
}
}
/**
* {@inheritDoc}
*/
public function offsetUnset($offset)
{
unset($this->lines[$offset]);
}
/**
* {@inheritDoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->lines);
}
/**
* Parse the remainder of the block according to its type
*
* @param array $lines The individual lines to parse
*/
abstract protected function doParse(array $lines);
}