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

PrintableAscii   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 65
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A escapeAscii() 0 4 1
A escapeControlCode() 0 6 2
escapeUnicode() 0 1 ?
A outputValidValue() 0 14 4
A setOptions() 0 4 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
}