Skip to content

Commit 9aacab7

Browse files
authored
Merge pull request MyIntervals#242 from oliverklee/cleanup/split
Move the `OutputFormatter` class to a separate file
2 parents 5ced3f4 + 59384d9 commit 9aacab7

File tree

2 files changed

+159
-156
lines changed

2 files changed

+159
-156
lines changed

src/OutputFormat.php

Lines changed: 0 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Sabberworm\CSS;
44

5-
use Sabberworm\CSS\Parsing\OutputException;
6-
75
/**
86
* Class OutputFormat
97
*
@@ -203,157 +201,3 @@ public static function createPretty()
203201
return $format;
204202
}
205203
}
206-
207-
class OutputFormatter
208-
{
209-
private $oFormat;
210-
211-
public function __construct(OutputFormat $oFormat)
212-
{
213-
$this->oFormat = $oFormat;
214-
}
215-
216-
public function space($sName, $sType = null)
217-
{
218-
$sSpaceString = $this->oFormat->get("Space$sName");
219-
// If $sSpaceString is an array, we have multple values configured
220-
// depending on the type of object the space applies to
221-
if (is_array($sSpaceString)) {
222-
if ($sType !== null && isset($sSpaceString[$sType])) {
223-
$sSpaceString = $sSpaceString[$sType];
224-
} else {
225-
$sSpaceString = reset($sSpaceString);
226-
}
227-
}
228-
return $this->prepareSpace($sSpaceString);
229-
}
230-
231-
public function spaceAfterRuleName()
232-
{
233-
return $this->space('AfterRuleName');
234-
}
235-
236-
public function spaceBeforeRules()
237-
{
238-
return $this->space('BeforeRules');
239-
}
240-
241-
public function spaceAfterRules()
242-
{
243-
return $this->space('AfterRules');
244-
}
245-
246-
public function spaceBetweenRules()
247-
{
248-
return $this->space('BetweenRules');
249-
}
250-
251-
public function spaceBeforeBlocks()
252-
{
253-
return $this->space('BeforeBlocks');
254-
}
255-
256-
public function spaceAfterBlocks()
257-
{
258-
return $this->space('AfterBlocks');
259-
}
260-
261-
public function spaceBetweenBlocks()
262-
{
263-
return $this->space('BetweenBlocks');
264-
}
265-
266-
public function spaceBeforeSelectorSeparator()
267-
{
268-
return $this->space('BeforeSelectorSeparator');
269-
}
270-
271-
public function spaceAfterSelectorSeparator()
272-
{
273-
return $this->space('AfterSelectorSeparator');
274-
}
275-
276-
public function spaceBeforeListArgumentSeparator($sSeparator)
277-
{
278-
return $this->space('BeforeListArgumentSeparator', $sSeparator);
279-
}
280-
281-
public function spaceAfterListArgumentSeparator($sSeparator)
282-
{
283-
return $this->space('AfterListArgumentSeparator', $sSeparator);
284-
}
285-
286-
public function spaceBeforeOpeningBrace()
287-
{
288-
return $this->space('BeforeOpeningBrace');
289-
}
290-
291-
/**
292-
* Runs the given code, either swallowing or passing exceptions, depending on the bIgnoreExceptions setting.
293-
*/
294-
public function safely($cCode)
295-
{
296-
if ($this->oFormat->get('IgnoreExceptions')) {
297-
// If output exceptions are ignored, run the code with exception guards
298-
try {
299-
return $cCode();
300-
} catch (OutputException $e) {
301-
return null;
302-
} //Do nothing
303-
} else {
304-
// Run the code as-is
305-
return $cCode();
306-
}
307-
}
308-
309-
/**
310-
* Clone of the implode function but calls ->render with the current output format instead of __toString()
311-
*/
312-
public function implode($sSeparator, $aValues, $bIncreaseLevel = false)
313-
{
314-
$sResult = '';
315-
$oFormat = $this->oFormat;
316-
if ($bIncreaseLevel) {
317-
$oFormat = $oFormat->nextLevel();
318-
}
319-
$bIsFirst = true;
320-
foreach ($aValues as $mValue) {
321-
if ($bIsFirst) {
322-
$bIsFirst = false;
323-
} else {
324-
$sResult .= $sSeparator;
325-
}
326-
if ($mValue instanceof \Sabberworm\CSS\Renderable) {
327-
$sResult .= $mValue->render($oFormat);
328-
} else {
329-
$sResult .= $mValue;
330-
}
331-
}
332-
return $sResult;
333-
}
334-
335-
public function removeLastSemicolon($sString)
336-
{
337-
if ($this->oFormat->get('SemicolonAfterLastRule')) {
338-
return $sString;
339-
}
340-
$sString = explode(';', $sString);
341-
if (count($sString) < 2) {
342-
return $sString[0];
343-
}
344-
$sLast = array_pop($sString);
345-
$sNextToLast = array_pop($sString);
346-
array_push($sString, $sNextToLast . $sLast);
347-
return implode(';', $sString);
348-
}
349-
350-
private function prepareSpace($sSpaceString)
351-
{
352-
return str_replace("\n", "\n" . $this->indent(), $sSpaceString);
353-
}
354-
355-
private function indent()
356-
{
357-
return str_repeat($this->oFormat->sIndentation, $this->oFormat->level());
358-
}
359-
}

src/OutputFormatter.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS;
4+
5+
use Sabberworm\CSS\Parsing\OutputException;
6+
7+
class OutputFormatter
8+
{
9+
private $oFormat;
10+
11+
public function __construct(OutputFormat $oFormat)
12+
{
13+
$this->oFormat = $oFormat;
14+
}
15+
16+
public function space($sName, $sType = null)
17+
{
18+
$sSpaceString = $this->oFormat->get("Space$sName");
19+
// If $sSpaceString is an array, we have multiple values configured
20+
// depending on the type of object the space applies to
21+
if (is_array($sSpaceString)) {
22+
if ($sType !== null && isset($sSpaceString[$sType])) {
23+
$sSpaceString = $sSpaceString[$sType];
24+
} else {
25+
$sSpaceString = reset($sSpaceString);
26+
}
27+
}
28+
return $this->prepareSpace($sSpaceString);
29+
}
30+
31+
public function spaceAfterRuleName()
32+
{
33+
return $this->space('AfterRuleName');
34+
}
35+
36+
public function spaceBeforeRules()
37+
{
38+
return $this->space('BeforeRules');
39+
}
40+
41+
public function spaceAfterRules()
42+
{
43+
return $this->space('AfterRules');
44+
}
45+
46+
public function spaceBetweenRules()
47+
{
48+
return $this->space('BetweenRules');
49+
}
50+
51+
public function spaceBeforeBlocks()
52+
{
53+
return $this->space('BeforeBlocks');
54+
}
55+
56+
public function spaceAfterBlocks()
57+
{
58+
return $this->space('AfterBlocks');
59+
}
60+
61+
public function spaceBetweenBlocks()
62+
{
63+
return $this->space('BetweenBlocks');
64+
}
65+
66+
public function spaceBeforeSelectorSeparator()
67+
{
68+
return $this->space('BeforeSelectorSeparator');
69+
}
70+
71+
public function spaceAfterSelectorSeparator()
72+
{
73+
return $this->space('AfterSelectorSeparator');
74+
}
75+
76+
public function spaceBeforeListArgumentSeparator($sSeparator)
77+
{
78+
return $this->space('BeforeListArgumentSeparator', $sSeparator);
79+
}
80+
81+
public function spaceAfterListArgumentSeparator($sSeparator)
82+
{
83+
return $this->space('AfterListArgumentSeparator', $sSeparator);
84+
}
85+
86+
public function spaceBeforeOpeningBrace()
87+
{
88+
return $this->space('BeforeOpeningBrace');
89+
}
90+
91+
/**
92+
* Runs the given code, either swallowing or passing exceptions, depending on the bIgnoreExceptions setting.
93+
*/
94+
public function safely($cCode)
95+
{
96+
if ($this->oFormat->get('IgnoreExceptions')) {
97+
// If output exceptions are ignored, run the code with exception guards
98+
try {
99+
return $cCode();
100+
} catch (OutputException $e) {
101+
return null;
102+
} //Do nothing
103+
} else {
104+
// Run the code as-is
105+
return $cCode();
106+
}
107+
}
108+
109+
/**
110+
* Clone of the implode function but calls ->render with the current output format instead of ` __toString()
111+
*/
112+
public function implode($sSeparator, $aValues, $bIncreaseLevel = false)
113+
{
114+
$sResult = '';
115+
$oFormat = $this->oFormat;
116+
if ($bIncreaseLevel) {
117+
$oFormat = $oFormat->nextLevel();
118+
}
119+
$bIsFirst = true;
120+
foreach ($aValues as $mValue) {
121+
if ($bIsFirst) {
122+
$bIsFirst = false;
123+
} else {
124+
$sResult .= $sSeparator;
125+
}
126+
if ($mValue instanceof \Sabberworm\CSS\Renderable) {
127+
$sResult .= $mValue->render($oFormat);
128+
} else {
129+
$sResult .= $mValue;
130+
}
131+
}
132+
return $sResult;
133+
}
134+
135+
public function removeLastSemicolon($sString)
136+
{
137+
if ($this->oFormat->get('SemicolonAfterLastRule')) {
138+
return $sString;
139+
}
140+
$sString = explode(';', $sString);
141+
if (count($sString) < 2) {
142+
return $sString[0];
143+
}
144+
$sLast = array_pop($sString);
145+
$sNextToLast = array_pop($sString);
146+
array_push($sString, $sNextToLast . $sLast);
147+
return implode(';', $sString);
148+
}
149+
150+
private function prepareSpace($sSpaceString)
151+
{
152+
return str_replace("\n", "\n" . $this->indent(), $sSpaceString);
153+
}
154+
155+
private function indent()
156+
{
157+
return str_repeat($this->oFormat->sIndentation, $this->oFormat->level());
158+
}
159+
}

0 commit comments

Comments
 (0)