Passed
Push — v5 ( 700af4...22f167 )
by smiley
01:39
created

ECI::parseValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 23
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class ECI
4
 *
5
 * @filesource   ECI.php
6
 * @created      20.11.2020
7
 * @package      chillerlan\QRCode\Data
8
 * @author       smiley <[email protected]>
9
 * @copyright    2020 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Data;
14
15
use chillerlan\QRCode\Common\{BitBuffer, Mode};
16
17
/**
18
 * Adds an ECI Designator
19
 *
20
 * Please note that you have to take care for the correct data encoding when adding with QRCode::add*Segment()
21
 */
22
final class ECI extends QRDataModeAbstract{
23
24
	protected static int $datamode = Mode::DATA_ECI;
25
26
	/**
27
	 * The current ECI encoding id
28
	 */
29
	protected int $encoding;
30
31
	/**
32
	 * @inheritDoc
33
	 * @noinspection PhpMissingParentConstructorInspection
34
	 */
35
	public function __construct(int $encoding){
36
		$this->encoding = $encoding;
37
	}
38
39
	/**
40
	 * @inheritDoc
41
	 */
42
	public function getLengthInBits():int{
43
		return 8;
44
	}
45
46
		/**
47
	 * @inheritDoc
48
	 */
49
	public function write(BitBuffer $bitBuffer, int $versionNumber):void{
50
		$bitBuffer
51
			->put($this::$datamode, 4)
52
			->put($this->encoding, 8)
53
		;
54
	}
55
56
	/**
57
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
58
	 */
59
	public static function parseValue(BitBuffer $bitBuffer):int{
60
		$firstByte = $bitBuffer->read(8);
61
62
		if(($firstByte & 0x80) === 0){
63
			// just one byte
64
			return $firstByte & 0x7f;
65
		}
66
67
		if(($firstByte & 0xc0) === 0x80){
68
			// two bytes
69
			$secondByte = $bitBuffer->read(8);
70
71
			return (($firstByte & 0x3f) << 8) | $secondByte;
72
		}
73
74
		if(($firstByte & 0xe0) === 0xC0){
75
			// three bytes
76
			$secondThirdBytes = $bitBuffer->read(16);
77
78
			return (($firstByte & 0x1f) << 16) | $secondThirdBytes;
79
		}
80
81
		throw new QRCodeDataException('error decoding ECI value');
82
	}
83
84
	/**
85
	 * @codeCoverageIgnore Unused, but required as per interface
86
	 */
87
	public static function validateString(string $string):bool{
88
		return true;
89
	}
90
91
	/**
92
	 * @codeCoverageIgnore Unused, but required as per interface
93
	 */
94
	public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
95
		return '';
96
	}
97
98
}
99