5 use Services\ISimfileParser
;
8 class SimfileParser
implements ISimfileParser
11 // 'light' => 'Novice',
12 // 'beginner' => 'Novice',
14 // 'medium' => 'Medium',
16 // 'challenge' => 'Expert',
19 private $_smFileLines;
21 public function parse($simfileData)
23 //XXX: Should I explode on ';' instead? That seems like it might be a more reliable delimiter
24 $this->_smFileLines
= explode(";", $simfileData);
27 public function title()
29 $title = $this->extractKey('TITLE');
30 if(!$title) throw new Exception ('Invalid SM file. TITLE missing');
35 public function artist()
37 $artist = $this->extractKey('ARTIST');
38 if(!$artist) throw new Exception ('Invalid SM file. ARTIST missing');
43 public function stops()
45 $stops = $this->extractKey('STOPS') ?
'Yes' : 'No';
46 if(!$stops) throw new Exception ('Invalid SM file. STOPS missing');
51 public function fgChanges()
53 $fgChanges = $this->extractKey('FGCHANGES') ?
'Yes' : 'No';
54 if(!$fgChanges) throw new Exception ('Invalid SM file. FGCHANGES missing');
59 public function bgChanges()
61 $bgChanges = $this->extractKey('BGCHANGES') ?
'Yes' : 'No';
62 if(!$bgChanges) throw new Exception ('Invalid SM file. BGCHANGES missing');
67 public function bpmChanges()
69 $bmpChanges = $this->extractKey('BPMS') ?
'Yes' : 'No';
70 if(!$bpmChanges) throw new Exception ('Invalid SM file. BPMS missing');
75 public function subtitle()
77 $subtitle = $this->extractKey('SUBTITLE');
78 if(!$subtitle) throw new Exception ('Invalid SM file. SUBTITLE missing');
85 if(empty($this->_smFileLines
)) throw new Exception('SM file data not set.');
88 foreach ($this->_smFileLines
as $line)
90 $pos = strpos($line, '#NOTES:');
93 $noteData = trim(substr($line, $pos +
9));
94 $allSteps = array_merge($allSteps, $this->stepsArrayFromNoteData($noteData));
98 if(empty($allSteps)) throw new Exception('Invalid Sm file. NOTES missing');
102 private function stepsArrayFromNoteData($noteData)
104 $stepData = array_map('trim', explode(':', $noteData));
106 $mode = $stepData[0] == 'dance-single' ?
'single' : null
;
107 $steps[$mode][] = array(
108 'artist' => $stepData[1],
109 'difficulty' => $stepData[2],
110 'rating' => $stepData[3]
116 private function extractKey($key)
118 if(empty($this->_smFileLines
)) throw new Exception('SM file data not set.');
120 foreach ($this->_smFileLines
as $line)
122 $pos = strpos($line, '#' . $key . ':');
123 if ($pos !== false
) return trim(substr($line, $pos +
strlen($key) +
2));