Hello
[rock.divinelegy.git] / Services / SimfileParser.php
1 <?php
2
3 namespace Services;
4
5 use Services\ISimfileParser;
6 use Exception;
7
8 class SimfileParser implements ISimfileParser
9 {
10
11 // 'light' => 'Novice',
12 // 'beginner' => 'Novice',
13 // 'easy' => 'Easy',
14 // 'medium' => 'Medium',
15 // 'hard' => 'Hard',
16 // 'challenge' => 'Expert',
17 // 'edit' => 'Edit'
18
19 private $_smFileLines;
20
21 public function __construct($simfileData)
22 {
23 //XXX: Should I explode on ';' instead? That seems like it might be a more reliable delimiter
24 $this->_smFileLines = explode("\n", $simfileData);
25 }
26
27 public function title()
28 {
29 $title = $this->extractKey('TITLE');
30 if(!$title) throw new Exception ('Invalid SM file. Title missing');
31
32 return $title;
33 }
34
35 public function stops()
36 {
37 return $this->extractKey('STOPS') ? 'Yes' : 'No';
38 }
39
40 public function steps($mode, $difficulty)
41 {
42 $steps = array();
43
44 foreach ($this->_smFileLines as $index => $line) {
45 if (strpos($line, '#NOTES') !== false) {
46 $mode = substr(trim($lines[$index + 1]), 0, -1) == 'dance-single' ? 'single' : null;
47 $notes[$mode][] = array(
48 'artist' => substr(trim($lines[$index + 2]), 0, -1),
49 'difficulty' => substr(trim($lines[$index + 3]), 0, -1),
50 'rating' => substr(trim($lines[$index + 4]), 0, -1)
51 );
52 }
53 }
54
55 return $steps;
56 }
57
58 private function extractKey($key) {
59 foreach ($lines as $line) {
60 $pos = strpos($line, '#' . $key . ':');
61 if($pos !== false) return substr(trim($line), strlen($key) + 2, -1);
62 }
63 }
64
65 }