forked from johnnoel/php-ass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.php
executable file
·185 lines (159 loc) · 4.11 KB
/
Script.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
<?php
namespace ChaosTangent\ASS;
use ChaosTangent\ASS\Exception\InvalidScriptException;
use ChaosTangent\ASS\Block\Block;
/**
* ASS script
*
* @author John Noel <john.noel@chaostangent.com>
* @package php-ass
*/
class Script implements \IteratorAggregate
{
/** @var string */
protected $filename;
/** @var string */
protected $content;
/** @var array */
protected $blocks = [];
/** @var boolean */
protected $parsed = false;
/** @var boolean */
protected $isUTF8 = false;
/**
* @param string $content The plain text content of the ASS script
*/
public function __construct($content, $filename = null)
{
$this->content = $content;
$this->filename = $filename;
}
/**
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* Quick check as to whether the content looks like an ASS script
*
* @return boolean True if it does, false otherwise
*/
public function isASSScript()
{
// check for UTF8 BOM
if (substr($this->content, 0, 3) == (chr(0xEF).chr(0xBB).chr(0xBF))) {
$this->isUTF8 = true;
$this->content = substr($this->content, 3);
}
$string = '[Script Info]';
return substr($this->content, 0, strlen($string)) == $string;
}
/**
* Whether this script has been parsed or not
*
* @return boolean
*/
public function isParsed()
{
return $this->parsed;
}
/**
* Parse this script
*/
public function parse()
{
if ($this->parsed) {
return;
}
if (!$this->isASSScript()) {
throw new InvalidScriptException($this, 'Content doesn\'t contain "[Script Info]" as the first line');
}
$lines = explode("\n", $this->content); // SSA/ASS files are always DOS
if (count($lines) == 1) {
throw new InvalidScriptException($this, 'Only one line in the script, probably incorrect line endings.');
}
$lineBuffer = [];
foreach ($lines as $line) {
if (Block::isBlockHeader($line) && !empty($lineBuffer)) {
$block = Block::parse($lineBuffer);
if ($block !== null) {
$this->addBlock($block);
}
$lineBuffer = [];
}
$lineBuffer[] = $line;
}
$lastBlock = Block::parse($lineBuffer);
if ($lastBlock !== null) {
$this->addBlock($lastBlock);
}
$this->parsed = true;
}
/**
* Add a block to this script
*
* @param Block $block
* @return Script
*/
public function addBlock(Block $block)
{
$this->blocks[$block->getId()] = $block;
return $this;
}
/**
* Remove a block from this script
*
* @param Block $block
* @return Script
*/
public function removeBlock(Block $block)
{
unset($this->blocks[$block->getId()]);
return $this;
}
/**
* Whether a block exists within this script
*
* You can pass either a full block object in which case this will search
* for a Block with the same ID (not object identity) or you can pass an
* ID e.g. hasBlock('Script Info').
*
* @param Block|string $block
* @return boolean
*/
public function hasBlock($block)
{
if ($block instanceof Block) {
return array_key_exists($block->getId(), $this->blocks);
}
return array_key_exists($block, $this->blocks);
}
/**
* Get blocks
*
* @return Block[] An array of blocks
*/
public function getBlocks()
{
return $this->blocks;
}
/**
* Get a single block
*
* @param string $blockId
* @return Block|null
*/
public function getBlock($blockId)
{
return ($this->hasBlock($blockId)) ? $this->blocks[$blockId] : null;
}
/**
* {@inheritDoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->blocks);
}
}