forked from johnnoel/php-ass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line.php
executable file
·167 lines (143 loc) · 3.43 KB
/
Line.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
<?php
namespace ChaosTangent\ASS\Line;
/**
* Line base class
*
* @author John Noel <[email protected]>
* @package php-ass
*/
class Line
{
/** @var string */
protected $key;
/** @var string */
protected $value;
/** @var array */
protected static $classMap = [
'Style' => Style::class,
'Dialogue' => Dialogue::class,
'Format' => Format::class,
];
/**
* Whether the passed string is a format line
*
* @param string $line
* @return boolean
*/
public static function isFormatLine($line)
{
return preg_match('/^Format:\s+/', $line) === 1;
}
/**
* Parse an individual line in the format "Key with spaces": "Value"
*
* @param string $line
* @param array $mapping A line mapping array (from a Format line for
* instances) that defines line parameter ordering
* @return Line|null A line of a specific type or null if a comment
* or unreadable
*/
public static function parse($content, array $mapping = [])
{
// comment, do nothing
if (empty($content) || substr($content, 0, 1) == ';') {
return null;
}
$matches = [];
if (preg_match('/^([^:]+):\s*(.*)$/', trim($content), $matches) !== 1) {
// doesn't match this format, do nothing
return null;
}
$key = $matches[1];
$value = $matches[2];
if (array_key_exists($key, self::$classMap)) {
$line = new self::$classMap[$key];
} else {
$line = new self();
}
$line->setKey($key)
->setValue($value)
->doParse($value, $mapping);
return $line;
}
/**
* Converts a timecode into seconds
*
* E.g. 0:07:02.44 is 0 hours, 7 minutes, 2.44 seconds which is 422.44
* seconds
*
* @param string The timecode to parse
* @return float
*/
public static function parseTimecodeIntoSeconds($timecode)
{
$parts = explode(':', $timecode);
$ret = 0;
if (count($parts) == 3) {
$hours = intval(reset($parts));
$ret += ($hours * 60 * 60);
array_shift($parts);
}
if (count($parts) == 2) {
$minutes = intval(reset($parts));
$ret += ($minutes * 60);
array_shift($parts);
}
if (count($parts) == 1) {
$seconds = floatval(reset($parts));
$ret += $seconds;
}
return $ret;
}
/**
* Set key
*
* @param string $key
* @return Line
*/
public function setKey($key)
{
$this->key = $key;
return $this;
}
/**
* Get key
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Set value
*
* @param string $value
* @return Line
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Do any subsequent, type specific parsing
*
* @param string $value
*/
protected function doParse($value, array $mapping)
{
}
public function toString($mapping = []) {
return $this->key.': '.$this->value;
}
}