Completed
Push — master ( f68f3b...859d26 )
by Josh
02:07
created

PrintableAscii::outputValidValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 6
nc 4
nop 1
crap 4
1
<?php
2
3
/**
4
* @package   s9e\RegexpBuilder
5
* @copyright Copyright (c) 2016 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\RegexpBuilder\Output;
9
10
abstract class PrintableAscii extends BaseImplementation
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 17
	protected function outputValidValue($value)
16
	{
17 17
		if ($value < 32)
18
		{
19 6
			return $this->escapeControlCode($value);
20
		}
21
22 11
		if ($value < 127)
23
		{
24 6
			return chr($value);
25
		}
26
27 5
		return ($value > 255) ? $this->escapeUnicode($value) : $this->escapeAscii($value);
28
	}
29
30
	/**
31
	* Escape given ASCII codepoint
32
	*
33
	* @param  integer $cp
34
	* @return string
35
	*/
36 2
	protected function escapeAscii($cp)
37
	{
38 2
		return '\\x' . sprintf('%02X', $cp);
39
	}
40
41
	/**
42
	* Escape given control code
43
	*
44
	* @param  integer $cp
45
	* @return string
46
	*/
47 6
	protected function escapeControlCode($cp)
48
	{
49 6
		$table = [9 => '\\t', 10 => '\\n', 13 => '\\r'];
50
51 6
		return (isset($table[$cp])) ? $table[$cp] : $this->escapeAscii($cp);
52
	}
53
54
	/**
55
	* Output the representation of a unicode character
56
	*
57
	* @param  integer $cp Unicode codepoint
58
	* @return string
59
	*/
60
	abstract protected function escapeUnicode($cp);
61
}