@@ -14,16 +14,65 @@ public function stringify()
14
14
15
15
foreach ($ this ->getItems () as $ item ) {
16
16
$ itemArray = $ item ->toArray ();
17
- var_dump ( $ itemArray );
17
+ $ m3uitem = new M3uItem ( );
18
18
// if (!empty($itemTitle)) {
19
19
// $entryTitle = $document->createElement('title');
20
20
// $entry->appendChild($entryTitle);
21
21
// $entryTitle->appendChild($document->createTextNode($itemTitle));
22
22
// }
23
- if (isset ($ itemArray ['itunes ' ]) && isset ($ itemArray ['enclosure ' ])) {
24
- $ contents .= $ itemArray ['enclosure ' ]['url ' ] . "\n" ;
23
+
24
+ if (isset ($ itemArray ['enclosure ' ])) {
25
+ $ m3uitem ->url = $ itemArray ['enclosure ' ]['url ' ];
26
+ $ m3uitem ->bytes = $ itemArray ['enclosure ' ]['length ' ];
27
+ }
28
+ if (isset ($ itemArray ['itunes ' ]) && isset ($ itemArray ['itunes ' ]['duration ' ])) {
29
+ $ m3uitem ->duration = parse_duration ($ itemArray ['itunes ' ]['duration ' ]);
30
+ }
31
+ if (isset ($ itemArray ['title ' ])) {
32
+ $ m3uitem ->title = $ itemArray ['title ' ];
25
33
}
34
+ $ contents .= $ m3uitem ->render ();
26
35
}
27
36
return mb_convert_encoding ($ contents , $ this ->getCharset (), 'UTF-8 ' );
28
37
}
29
38
}
39
+
40
+ function parse_duration ($ duration_string )
41
+ {
42
+ $ seconds = 0 ;
43
+ $ parts = explode (': ' , $ duration_string );
44
+ for ($ i = 0 ; $ i < count ($ parts ); $ i ++) {
45
+ $ seconds += intval ($ parts [count ($ parts ) - $ i - 1 ]) * pow (60 , $ i );
46
+ }
47
+ return $ seconds ;
48
+ }
49
+
50
+ class M3uItem
51
+ {
52
+ public $ duration = null ;
53
+ public $ title = null ;
54
+ public $ url = null ;
55
+ public $ bytes = null ;
56
+
57
+ public function render ()
58
+ {
59
+ if ($ this ->url === null ) {
60
+ return '' ;
61
+ }
62
+ $ text = '' ;
63
+ $ commentParts = [];
64
+ if ($ this ->duration !== null && $ this ->duration > 0 ) {
65
+ $ commentParts [] = $ this ->duration ;
66
+ }
67
+ if ($ this ->title !== null ) {
68
+ $ commentParts [] = $ this ->title ;
69
+ }
70
+
71
+ if (count ($ commentParts ) !== 0 ) {
72
+ $ text .= '#EXTINF: ' . implode (', ' , $ commentParts ) . "\n" ;
73
+ }
74
+
75
+ $ text .= $ this ->url . "\n" ;
76
+ return $ text ;
77
+ }
78
+ }
0 commit comments