1 : <?php
2 :
3 : /**
4 : * \Midi\Reporting\TextFormatter
5 : *
6 : * @package Midi
7 : * @subpackage Reporting
8 : * @copyright © 2009 Tommy Montgomery <http://phpmidiparser.com/>
9 : * @since 1.0
10 : */
11 :
12 : namespace Midi\Reporting;
13 :
14 : use \Midi\Delta;
15 : use \Midi\FileHeader;
16 : use \Midi\TrackHeader;
17 : use \Midi\Event;
18 :
19 : /**
20 : * Textual parse results formatter
21 : *
22 : * @package Midi
23 : * @subpackage Reporting
24 : * @since 1.0
25 : */
26 1 : class TextFormatter extends Formatter {
27 :
28 : /**
29 : * The indentation string
30 : *
31 : * @var string
32 : */
33 : protected $indent;
34 :
35 : /**
36 : * The number of the current track
37 : *
38 : * @var int
39 : */
40 : protected $currentTrack;
41 :
42 : /**
43 : * Constructor
44 : *
45 : * @since 1.0
46 : */
47 : public function __construct() {
48 7 : $this->indent = '';
49 7 : $this->currentTrack = 1;
50 7 : }
51 :
52 : /**
53 : * Centralized place to output text
54 : *
55 : * @since 1.0
56 : *
57 : * @param string $text
58 : */
59 : protected function formatText($text) {
60 6 : return $this->indent . $text . "\n";
61 : }
62 :
63 : /**
64 : * Increases the indentation
65 : *
66 : * @since 1.0
67 : */
68 : protected function increaseIndent() {
69 3 : $this->indent .= ' ';
70 3 : }
71 :
72 : /**
73 : * Decreases the indentation
74 : *
75 : * @since 1.0
76 : */
77 : protected function decreaseIndent() {
78 1 : $this->indent = substr($this->indent, 0, -2);
79 1 : }
80 :
81 : /**
82 : * @since 1.0
83 : * @uses formatText()
84 : * @uses increaseIndent()
85 : *
86 : * @return string
87 : */
88 : public function beforeFile() {
89 1 : $text = $this->formatText('---- FILE START ----');
90 1 : $this->increaseIndent();
91 1 : return $text;
92 : }
93 :
94 : /**
95 : * @since 1.0
96 : * @uses formatText()
97 : * @uses increaseIndent()
98 : *
99 : * @return string
100 : */
101 : public function beforeFileHeader(FileHeader $fileHeader) {
102 1 : $text = $this->formatText('---- FILE HEADER ----');
103 1 : $this->increaseIndent();
104 1 : return $text;
105 : }
106 :
107 : /**
108 : * @since 1.0
109 : * @uses FileHeader::getData()
110 : * @uses formatText()
111 : *
112 : * @param FileHeader $fileHeader
113 : * @return string
114 : */
115 : public function formatFileHeader(FileHeader $fileHeader) {
116 1 : list($format, $tracks, $timeDivision) = $fileHeader->getData();
117 :
118 1 : $text = $this->formatText('format: ' . $format);
119 1 : $text .= $this->formatText('tracks: ' . $tracks);
120 1 : $text .= $this->formatText('time division: ' . $timeDivision);
121 1 : return $text;
122 : }
123 :
124 : /**
125 : * @since 1.0
126 : * @uses decreaseIndent()
127 : */
128 : public function afterFileHeader(FileHeader $fileHeader) {
129 1 : $this->decreaseIndent();
130 1 : }
131 :
132 : /**
133 : * @since 1.0
134 : * @uses TrackHeader::getData()
135 : * @uses formatText()
136 : * @uses decreaseIndent()
137 : *
138 : * @param TrackHeader $trackHeader
139 : * @return string
140 : */
141 : public function formatTrackHeader(TrackHeader $trackHeader) {
142 1 : list($size) = $trackHeader->getData();
143 :
144 1 : $text = $this->formatText('---- TRACK ' . $this->currentTrack . ' (' . $size . ' bytes) ----');
145 1 : $this->currentTrack++;
146 1 : $this->increaseIndent();
147 1 : return $text;
148 : }
149 :
150 : /**
151 : * @since 1.0
152 : * @uses Delta::getData()
153 : * @uses formatText()
154 : *
155 : * @param Delta $delta
156 : *
157 : * @return string
158 : */
159 : public function formatDelta(Delta $delta) {
160 1 : list($ticks) = $delta->getData();
161 1 : return $this->formatText('delta: ' . $ticks);
162 : }
163 :
164 : /**
165 : * @since 1.0
166 : * @uses formatText()
167 : * @uses Event::__toString()
168 : *
169 : * @param Event $event
170 : * @return string
171 : */
172 : public function formatEvent(Event $event) {
173 1 : return $this->formatText($event->__toString());
174 : }
175 :
176 : /**
177 : * @since 1.0
178 : * @uses decreaseIndent()
179 : */
180 : public function afterTrack() {
181 1 : $this->decreaseIndent();
182 1 : }
183 :
184 : }
185 :
|