Completed
Branch MetaCharacters (da2f1d)
by Josh
01:29
created

PrintableAscii::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 4
nop 1
crap 3
1
<?php
2
3
/**
4
* @package   s9e\RegexpBuilder
5
* @copyright Copyright (c) 2016-2018 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
	* @var string 'x' for lowercase hexadecimal symbols, 'X' for uppercase
14
	*/
15
	protected $hexCase;
16
17
	/**
18
	* Escape given ASCII codepoint
19
	*
20
	* @param  integer $cp
21
	* @return string
22
	*/
23 6
	protected function escapeAscii($cp)
24
	{
25 6
		return '\\x' . sprintf('%02' . $this->hexCase, $cp);
26
	}
27
28
	/**
29
	* Escape given control code
30
	*
31
	* @param  integer $cp
32
	* @return string
33
	*/
34 6
	protected function escapeControlCode($cp)
35
	{
36 6
		$table = [9 => '\\t', 10 => '\\n', 13 => '\\r'];
37
38 6
		return (isset($table[$cp])) ? $table[$cp] : $this->escapeAscii($cp);
39
	}
40
41
	/**
42
	* Output the representation of a unicode character
43
	*
44
	* @param  integer $cp Unicode codepoint
45
	* @return string
46
	*/
47
	abstract protected function escapeUnicode($cp);
48
49
	/**
50
	* {@inheritdoc}
51
	*/
52 32
	protected function outputValidValue($value)
53
	{
54 32
		if ($value < 32)
55
		{
56 6
			return $this->escapeControlCode($value);
57
		}
58
59 26
		if ($value < 127)
60
		{
61 6
			return chr($value);
62
		}
63
64 20
		return ($value > 255) ? $this->escapeUnicode($value) : $this->escapeAscii($value);
65
	}
66
67
	/**
68
	* {@inheritdoc}
69
	*/
70 34
	protected function setOptions(array $options)
71
	{
72 34
		$this->hexCase = (isset($options['case']) && $options['case'] === 'lower') ? 'x' : 'X';
73
	}
74
}