Byte   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLengthInBits() 0 2 1
A validateString() 0 2 1
A decodeSegment() 0 14 3
A write() 0 16 2
1
<?php
2
/**
3
 * Class Byte
4
 *
5
 * @created      25.11.2015
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2015 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\QRCode\Data;
12
13
use chillerlan\QRCode\Common\{BitBuffer, Mode};
14
15
use function chr, ord;
16
17
/**
18
 * 8-bit Byte mode, ISO-8859-1 or UTF-8
19
 *
20
 * ISO/IEC 18004:2000 Section 8.3.4
21
 * ISO/IEC 18004:2000 Section 8.4.4
22
 */
23
final class Byte extends QRDataModeAbstract{
24
25
	/**
26
	 * @inheritDoc
27
	 */
28
	public const DATAMODE = Mode::BYTE;
29
30
	/**
31
	 * @inheritDoc
32
	 */
33
	public function getLengthInBits():int{
34
		return ($this->getCharCount() * 8);
35
	}
36
37
	/**
38
	 * @inheritDoc
39
	 */
40
	public static function validateString(string $string):bool{
41
		return $string !== '';
42
	}
43
44
	/**
45
	 * @inheritDoc
46
	 */
47
	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
48
		$len = $this->getCharCount();
49
50
		$bitBuffer
51
			->put(self::DATAMODE, 4)
52
			->put($len, $this::getLengthBits($versionNumber))
53
		;
54
55
		$i = 0;
56
57
		while($i < $len){
58
			$bitBuffer->put(ord($this->data[$i]), 8);
59
			$i++;
60
		}
61
62
		return $this;
63
	}
64
65
	/**
66
	 * @inheritDoc
67
	 *
68
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
69
	 */
70
	public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
71
		$length = $bitBuffer->read(self::getLengthBits($versionNumber));
72
73
		if($bitBuffer->available() < (8 * $length)){
74
			throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
75
		}
76
77
		$readBytes = '';
78
79
		for($i = 0; $i < $length; $i++){
80
			$readBytes .= chr($bitBuffer->read(8));
81
		}
82
83
		return $readBytes;
84
	}
85
86
}
87