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 $this->_smFileLines
= explode(";", $simfileData);
26 public function banner()
28 return $this->extractKey('BANNER') ?
: null
;
31 public function title()
33 $title = $this->extractKey('TITLE');
34 if(!$title) throw new Exception ('Invalid SM file. TITLE missing');
39 public function artist()
41 $artist = $this->extractKey('ARTIST');
42 if(!$artist) throw new Exception ('Invalid SM file. ARTIST missing');
44 return new \Domain\VOs\StepMania\
Artist($artist);
47 public function stops()
49 $stops = $this->extractKey('STOPS') ?
'Yes' : 'No';
50 if(!$stops) throw new Exception ('Invalid SM file. STOPS missing');
55 public function fgChanges()
57 $fgChanges = $this->extractKey('FGCHANGES') ?
'Yes' : 'No';
58 if(!$fgChanges) throw new Exception ('Invalid SM file. FGCHANGES missing');
65 $displayBpm = $this->extractKey('DISPLAYBPM');
69 $bpmRange = explode(":",$displayBpm);
70 $bpmRange[1] = @$bpmRange[1] ?
: $bpmRange[0];
72 $bpms = $this->extractKey('BPMS');
73 $bpmRange = $this->parseBpms($bpms);
76 //I have nfi why I made the BPM VO high-low instead of low-high in the constructor but yolo
77 return new \Domain\VOs\StepMania\
BPM($bpmRange[1], $bpmRange[0]);
80 public function bgChanges()
82 $bgChanges = $this->extractKey('BGCHANGES') ?
'Yes' : 'No';
83 if(!$bgChanges) throw new Exception ('Invalid SM file. BGCHANGES missing');
88 public function bpmChanges()
90 $bpmChanges = $this->extractKey('BPMS') ?
'Yes' : 'No';
91 if(!$bpmChanges) throw new Exception ('Invalid SM file. BPMS missing');
96 public function subtitle()
98 $subtitle = $this->extractKey('SUBTITLE');
99 if(!$subtitle) throw new Exception ('Invalid SM file. SUBTITLE missing');
106 if(empty($this->_smFileLines
)) throw new Exception('SM file data not set.');
109 foreach ($this->_smFileLines
as $line)
111 $pos = strpos($line, '#NOTES:');
114 $noteData = trim(substr($line, $pos +
9));
115 $allSteps[] = $this->stepchartFromNoteData($noteData);
119 if(empty($allSteps)) throw new Exception('Invalid Sm file. NOTES missing');
123 private function stepchartFromNoteData($noteData)
125 $stepData = array_map('trim', explode(':', $noteData));
126 return new \Domain\VOs\StepMania\
StepChart(
127 new \Domain\VOs\StepMania\
DanceMode($stepData[0]),
128 new \Domain\VOs\StepMania\
Difficulty($stepData[2]),
129 empty($stepData[1]) ? null
: new \Domain\VOs\StepMania\
StepArtist($stepData[1]),
134 private function extractKey($key)
136 if(empty($this->_smFileLines
)) throw new Exception('SM file data not set.');
138 foreach ($this->_smFileLines
as $line)
140 $pos = strpos($line, '#' . $key . ':');
141 if ($pos !== false
) return trim(substr($line, $pos +
strlen($key) +
2));
145 private function parseBpms($bpms)
147 $bpms = explode(",", $bpms);
148 $bpmRange = array('high' => null
, 'low' => null
);
150 foreach($bpms as $bpm)
152 $bpmMeasure = explode('=', $bpm);
153 $bpmValue = floatval($bpmMeasure[1]);
155 if(empty($bpmRange['low'])) $bpmRange['low'] = $bpmRange['high'] = $bpmValue;
157 $bpmRange['high'] = ($bpmValue > $bpmRange['high']) ?
$bpmValue : $bpmRange['high'];
158 $bpmRange['low'] = ($bpmValue < $bpmRange['low']) ?
$bpmValue : $bpmRange['low'];
161 return array($bpmRange['low'], $bpmRange['high']);