Skip to content

Commit f7e232d

Browse files
authored
Merge pull request #111 from bigfoot90/formats-merging
Handling merging formats arrays
2 parents f3c5add + 88baea9 commit f7e232d

File tree

2 files changed

+402
-115
lines changed

2 files changed

+402
-115
lines changed

src/Utils/Formatter.php

Lines changed: 188 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -55,128 +55,201 @@ class Formatter
5555
*/
5656
public function __construct(array $options = array())
5757
{
58-
// The specified formatting options are merged with the default values.
59-
$this->options = array_merge(
60-
array(
58+
$this->options = $this->getMergedOptions($options);
59+
}
6160

62-
/**
63-
* The format of the result.
64-
*
65-
* @var string The type ('text', 'cli' or 'html')
66-
*/
67-
'type' => php_sapi_name() == 'cli' ? 'cli' : 'text',
68-
69-
/**
70-
* The line ending used.
71-
* By default, for text this is "\n" and for HTML this is "<br/>".
72-
*
73-
* @var string
74-
*/
75-
'line_ending' => NULL,
76-
77-
/**
78-
* The string used for indentation.
79-
*
80-
* @var string
81-
*/
82-
'indentation' => ' ',
83-
84-
/**
85-
* Whether comments should be removed or not.
86-
*
87-
* @var bool
88-
*/
89-
'remove_comments' => false,
90-
91-
/**
92-
* Whether each clause should be on a new line.
93-
*
94-
* @var bool
95-
*/
96-
'clause_newline' => true,
97-
98-
/**
99-
* Whether each part should be on a new line.
100-
* Parts are delimited by brackets and commas.
101-
*
102-
* @var bool
103-
*/
104-
'parts_newline' => true,
105-
106-
/**
107-
* Whether each part of each clause should be indented.
108-
*
109-
* @var bool
110-
*/
111-
'indent_parts' => true,
112-
113-
/**
114-
* The styles used for HTML formatting.
115-
* array($type, $flags, $span, $callback)
116-
*
117-
* @var array[]
118-
*/
119-
'formats' => array(
120-
array(
121-
'type' => Token::TYPE_KEYWORD,
122-
'flags' => Token::FLAG_KEYWORD_RESERVED,
123-
'html' => 'class="sql-reserved"',
124-
'cli' => "\x1b[35m",
125-
'function' => 'strtoupper',
126-
),
127-
array(
128-
'type' => Token::TYPE_KEYWORD,
129-
'flags' => 0,
130-
'html' => 'class="sql-keyword"',
131-
'cli' => "\x1b[95m",
132-
'function' => 'strtoupper',
133-
),
134-
array(
135-
'type' => Token::TYPE_COMMENT,
136-
'flags' => 0,
137-
'html' => 'class="sql-comment"',
138-
'cli' => "\x1b[37m",
139-
'function' => '',
140-
),
141-
array(
142-
'type' => Token::TYPE_BOOL,
143-
'flags' => 0,
144-
'html' => 'class="sql-atom"',
145-
'cli' => "\x1b[36m",
146-
'function' => 'strtoupper',
147-
),
148-
array(
149-
'type' => Token::TYPE_NUMBER,
150-
'flags' => 0,
151-
'html' => 'class="sql-number"',
152-
'cli' => "\x1b[92m",
153-
'function' => 'strtolower',
154-
),
155-
array(
156-
'type' => Token::TYPE_STRING,
157-
'flags' => 0,
158-
'html' => 'class="sql-string"',
159-
'cli' => "\x1b[91m",
160-
'function' => '',
161-
),
162-
array(
163-
'type' => Token::TYPE_SYMBOL,
164-
'flags' => 0,
165-
'html' => 'class="sql-variable"',
166-
'cli' => "\x1b[36m",
167-
'function' => '',
168-
),
169-
)
170-
),
61+
/**
62+
* The specified formatting options are merged with the default values.
63+
*
64+
* @param array $options
65+
* @return array
66+
*/
67+
private function getMergedOptions(array $options)
68+
{
69+
$options = array_merge(
70+
$this->getDefaultOptions(),
17171
$options
17272
);
17373

174-
if (is_null($this->options['line_ending'])) {
175-
$this->options['line_ending'] = $this->options['type'] == 'html' ? '<br/>' : "\n";
74+
$options['formats'] = self::mergeFormats($this->getDefaultFormats(), @$options['formats'] ?: array());
75+
76+
if (is_null($options['line_ending'])) {
77+
$options['line_ending'] = $options['type'] === 'html' ? '<br/>' : "\n";
17678
}
17779

17880
// `parts_newline` requires `clause_newline`
179-
$this->options['parts_newline'] &= $this->options['clause_newline'];
81+
$options['parts_newline'] &= $options['clause_newline'];
82+
83+
return $options;
84+
}
85+
86+
/**
87+
* The default formatting options.
88+
*
89+
* @return array
90+
*/
91+
protected function getDefaultOptions()
92+
{
93+
return array(
94+
/**
95+
* The format of the result.
96+
*
97+
* @var string The type ('text', 'cli' or 'html')
98+
*/
99+
'type' => php_sapi_name() === 'cli' ? 'cli' : 'text',
100+
101+
/**
102+
* The line ending used.
103+
* By default, for text this is "\n" and for HTML this is "<br/>".
104+
*
105+
* @var string
106+
*/
107+
'line_ending' => NULL,
108+
109+
/**
110+
* The string used for indentation.
111+
*
112+
* @var string
113+
*/
114+
'indentation' => ' ',
115+
116+
/**
117+
* Whether comments should be removed or not.
118+
*
119+
* @var bool
120+
*/
121+
'remove_comments' => false,
122+
123+
/**
124+
* Whether each clause should be on a new line.
125+
*
126+
* @var bool
127+
*/
128+
'clause_newline' => true,
129+
130+
/**
131+
* Whether each part should be on a new line.
132+
* Parts are delimited by brackets and commas.
133+
*
134+
* @var bool
135+
*/
136+
'parts_newline' => true,
137+
138+
/**
139+
* Whether each part of each clause should be indented.
140+
*
141+
* @var bool
142+
*/
143+
'indent_parts' => true,
144+
);
145+
}
146+
147+
/**
148+
* The styles used for HTML formatting.
149+
* array($type, $flags, $span, $callback)
150+
*
151+
* @return array
152+
*/
153+
protected function getDefaultFormats()
154+
{
155+
return array(
156+
array(
157+
'type' => Token::TYPE_KEYWORD,
158+
'flags' => Token::FLAG_KEYWORD_RESERVED,
159+
'html' => 'class="sql-reserved"',
160+
'cli' => "\x1b[35m",
161+
'function' => 'strtoupper',
162+
),
163+
array(
164+
'type' => Token::TYPE_KEYWORD,
165+
'flags' => 0,
166+
'html' => 'class="sql-keyword"',
167+
'cli' => "\x1b[95m",
168+
'function' => 'strtoupper',
169+
),
170+
array(
171+
'type' => Token::TYPE_COMMENT,
172+
'flags' => 0,
173+
'html' => 'class="sql-comment"',
174+
'cli' => "\x1b[37m",
175+
'function' => '',
176+
),
177+
array(
178+
'type' => Token::TYPE_BOOL,
179+
'flags' => 0,
180+
'html' => 'class="sql-atom"',
181+
'cli' => "\x1b[36m",
182+
'function' => 'strtoupper',
183+
),
184+
array(
185+
'type' => Token::TYPE_NUMBER,
186+
'flags' => 0,
187+
'html' => 'class="sql-number"',
188+
'cli' => "\x1b[92m",
189+
'function' => 'strtolower',
190+
),
191+
array(
192+
'type' => Token::TYPE_STRING,
193+
'flags' => 0,
194+
'html' => 'class="sql-string"',
195+
'cli' => "\x1b[91m",
196+
'function' => '',
197+
),
198+
array(
199+
'type' => Token::TYPE_SYMBOL,
200+
'flags' => 0,
201+
'html' => 'class="sql-variable"',
202+
'cli' => "\x1b[36m",
203+
'function' => '',
204+
),
205+
);
206+
}
207+
208+
private static function mergeFormats(array $formats, array $newFormats)
209+
{
210+
$added = array();
211+
212+
foreach ($formats as $i => $original) {
213+
foreach ($newFormats as $j => $new) {
214+
if (isset($new['type'])
215+
&& $new['type'] === $original['type']
216+
&& (
217+
(
218+
isset($new['flags'])
219+
&& $original['flags'] === $new['flags']
220+
)
221+
|| (
222+
!isset($new['flags'])
223+
&& $original['flags'] == 0
224+
)
225+
)
226+
) {
227+
$formats[$i] = array(
228+
'type' => $original['type'],
229+
'flags' => isset($new['flags']) ? $new['flags'] : 0,
230+
'html' => isset($new['html']) ? $new['html'] : '',
231+
'cli' => isset($new['cli']) ? $new['cli'] : '',
232+
'function' => isset($new['function']) ? $new['function'] : '',
233+
);
234+
235+
$added[] = $j;
236+
}
237+
}
238+
}
239+
240+
foreach ($newFormats as $j => $new) {
241+
if (!in_array($j, $added) && isset($new['type'])) {
242+
$formats[] = array(
243+
'type' => $new['type'],
244+
'flags' => isset($new['flags']) ? $new['flags'] : 0,
245+
'html' => isset($new['html']) ? $new['html'] : '',
246+
'cli' => isset($new['cli']) ? $new['cli'] : '',
247+
'function' => isset($new['function']) ? $new['function'] : '',
248+
);
249+
}
250+
}
251+
252+
return $formats;
180253
}
181254

182255
/**

0 commit comments

Comments
 (0)