Bleh
[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 parse($simfileData)
22 {
23 //XXX: Should I explode on ';' instead? That seems like it might be a more reliable delimiter
24 $this->_smFileLines = explode(";", $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 artist()
36 {
37 $artist = $this->extractKey('ARTIST');
38 if(!$artist) throw new Exception ('Invalid SM file. ARTIST missing');
39
40 return $artist;
41 }
42
43 public function stops()
44 {
45 $stops = $this->extractKey('STOPS') ? 'Yes' : 'No';
46 if(!$stops) throw new Exception ('Invalid SM file. STOPS missing');
47
48 return $stops;
49 }
50
51 public function fgChanges()
52 {
53 $fgChanges = $this->extractKey('FGCHANGES') ? 'Yes' : 'No';
54 if(!$fgChanges) throw new Exception ('Invalid SM file. FGCHANGES missing');
55
56 return $fgChanges;
57 }
58
59 public function bgChanges()
60 {
61 $bgChanges = $this->extractKey('BGCHANGES') ? 'Yes' : 'No';
62 if(!$bgChanges) throw new Exception ('Invalid SM file. BGCHANGES missing');
63
64 return $bgChanges;
65 }
66
67 public function bpmChanges()
68 {
69 $bmpChanges = $this->extractKey('BPMS') ? 'Yes' : 'No';
70 if(!$bpmChanges) throw new Exception ('Invalid SM file. BPMS missing');
71
72 return $bpmChanges;
73 }
74
75 public function subtitle()
76 {
77 $subtitle = $this->extractKey('SUBTITLE');
78 if(!$subtitle) throw new Exception ('Invalid SM file. SUBTITLE missing');
79
80 return $subtitle;
81 }
82
83 function steps()
84 {
85 if(empty($this->_smFileLines)) throw new Exception('SM file data not set.');
86 $allSteps = array();
87
88 foreach ($this->_smFileLines as $line)
89 {
90 $pos = strpos($line, '#NOTES:');
91 if ($pos !== false)
92 {
93 $noteData = trim(substr($line, $pos + 9));
94 $allSteps = array_merge($allSteps, $this->stepsArrayFromNoteData($noteData));
95 }
96 }
97
98 if(empty($allSteps)) throw new Exception('Invalid Sm file. NOTES missing');
99 return $allSteps;
100 }
101
102 private function stepsArrayFromNoteData($noteData)
103 {
104 $stepData = array_map('trim', explode(':', $noteData));
105 $steps = array();
106 $mode = $stepData[0] == 'dance-single' ? 'single' : null;
107 $steps[$mode][] = array(
108 'artist' => $stepData[1],
109 'difficulty' => $stepData[2],
110 'rating' => $stepData[3]
111 );
112
113 return $steps;
114 }
115
116 private function extractKey($key)
117 {
118 if(empty($this->_smFileLines)) throw new Exception('SM file data not set.');
119
120 foreach ($this->_smFileLines as $line)
121 {
122 $pos = strpos($line, '#' . $key . ':');
123 if ($pos !== false) return trim(substr($line, $pos + strlen($key) + 2));
124 }
125 }
126 }