Completed
Push — master ( c96444...b7de72 )
by Josh
135:49 queued 132:43
created

Utf8::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3
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
use InvalidArgumentException;
11
12
class Utf8 extends BaseImplementation
13
{
14
	/** {@inheritdoc} */
15
	protected $maxValue = 0x10FFFF;
16
17
	/**
18
	* {@inheritdoc}
19
	*/
20 9
	protected function outputValidValue($value)
21
	{
22 9
		if ($value < 0x80)
23
		{
24 6
			return chr($value);
25
		}
26 3
		if ($value < 0x800)
27
		{
28 1
			return chr(0xC0 | ($value >> 6)) . chr(0x80 | ($value & 0x3F));
29
		}
30 2
		if ($value < 0x10000)
31
		{
32 1
			return chr(0xE0 | ($value >> 12))
33 1
			     . chr(0x80 | (($value >> 6) & 0x3F))
34 1
			     . chr(0x80 | ($value & 0x3F));
35
		}
36 1
		return chr(0xF0 | ($value >> 18))
37 1
		     . chr(0x80 | (($value >> 12) & 0x3F))
38 1
		     . chr(0x80 | (($value >> 6) & 0x3F))
39 1
		     . chr(0x80 | ($value & 0x3F));
40
	}
41
42
	/**
43
	* {@inheritdoc}
44
	*/
45 12
	protected function validate($value)
46
	{
47 12
		if ($value >= 0xD800 && $value <= 0xDFFF)
48
		{
49 2
			throw new InvalidArgumentException(sprintf('Surrogate 0x%X is not a valid UTF-8 character', $value));
50
		}
51
52 10
		parent::validate($value);
53
	}
54
}