Beginnings of SM parser.
[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 $this->_smFileLines = explode("\n", $simfileData);
24 }
25
26 public function title()
27 {
28 $title = $this->extractKey('TITLE');
29 if(!$title) throw new Exception ('Invalid SM file. Title missing');
30
31 return $title;
32 }
33
34 public function stops()
35 {
36 return $this->extractKey('STOPS') ? 'Yes' : 'No';
37 }
38
39 public function steps($mode, $difficulty)
40 {
41 $steps = array();
42
43 foreach ($this->_smFileLines as $index => $line) {
44 if (strpos($line, '#NOTES') !== false) {
45 $mode = substr(trim($lines[$index + 1]), 0, -1) == 'dance-single' ? 'single' : null;
46 $notes[$mode][] = array(
47 'artist' => substr(trim($lines[$index + 2]), 0, -1),
48 'difficulty' => substr(trim($lines[$index + 3]), 0, -1),
49 'rating' => substr(trim($lines[$index + 4]), 0, -1)
50 );
51 }
52 }
53
54 return $steps;
55 }
56
57 private function extractKey($key) {
58 foreach ($lines as $line) {
59 $pos = strpos($line, '#' . $key . ':');
60 if($pos !== false) return substr(trim($line), strlen($key) + 2, -1);
61 }
62 }
63
64 }