AlphaNum::write()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 2
dl 0
loc 19
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class AlphaNum
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 array_flip, ceil, str_split;
16
17
/**
18
 * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
19
 *
20
 * ISO/IEC 18004:2000 Section 8.3.3
21
 * ISO/IEC 18004:2000 Section 8.4.3
22
 */
23
final class AlphaNum extends QRDataModeAbstract{
24
25
	/**
26
	 * ISO/IEC 18004:2000 Table 5
27
	 *
28
	 * @var int[]
29
	 */
30
	private const CHAR_TO_ORD = [
31
		'0' =>  0, '1' =>  1, '2' =>  2, '3' =>  3, '4' =>  4, '5' =>  5, '6' =>  6, '7' =>  7,
32
		'8' =>  8, '9' =>  9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15,
33
		'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23,
34
		'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31,
35
		'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, ' ' => 36, '$' => 37, '%' => 38, '*' => 39,
36
		'+' => 40, '-' => 41, '.' => 42, '/' => 43, ':' => 44,
37
	];
38
39
	/**
40
	 * @inheritDoc
41
	 */
42
	public const DATAMODE = Mode::ALPHANUM;
43
44
	/**
45
	 * @inheritDoc
46
	 */
47
	public function getLengthInBits():int{
48
		return (int)ceil($this->getCharCount() * (11 / 2));
49
	}
50
51
	/**
52
	 * @inheritDoc
53
	 */
54
	public static function validateString(string $string):bool{
55
56
		if($string === ''){
57
			return false;
58
		}
59
60
		foreach(str_split($string) as $chr){
61
			if(!isset(self::CHAR_TO_ORD[$chr])){
62
				return false;
63
			}
64
		}
65
66
		return true;
67
	}
68
69
	/**
70
	 * @inheritDoc
71
	 */
72
	public function write(BitBuffer $bitBuffer, int $versionNumber):QRDataModeInterface{
73
		$len = $this->getCharCount();
74
75
		$bitBuffer
76
			->put(self::DATAMODE, 4)
77
			->put($len, $this::getLengthBits($versionNumber))
78
		;
79
80
		// encode 2 characters in 11 bits
81
		for($i = 0; ($i + 1) < $len; $i += 2){
82
			$bitBuffer->put((self::CHAR_TO_ORD[$this->data[$i]] * 45 + self::CHAR_TO_ORD[$this->data[($i + 1)]]), 11);
83
		}
84
85
		// encode a remaining character in 6 bits
86
		if($i < $len){
87
			$bitBuffer->put(self::CHAR_TO_ORD[$this->data[$i]], 6);
88
		}
89
90
		return $this;
91
	}
92
93
	/**
94
	 * @inheritDoc
95
	 *
96
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
97
	 */
98
	public static function decodeSegment(BitBuffer $bitBuffer, int $versionNumber):string{
99
		$length  = $bitBuffer->read(self::getLengthBits($versionNumber));
100
		$charmap = array_flip(self::CHAR_TO_ORD);
101
102
		// @todo
103
		$toAlphaNumericChar = function(int $ord) use ($charmap):string{
104
105
			if(isset($charmap[$ord])){
106
				return $charmap[$ord];
107
			}
108
109
			throw new QRCodeDataException('invalid character value: '.$ord);
110
		};
111
112
		$result = '';
113
		// Read two characters at a time
114
		while($length > 1){
115
116
			if($bitBuffer->available() < 11){
117
				throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
118
			}
119
120
			$nextTwoCharsBits = $bitBuffer->read(11);
121
			$result           .= $toAlphaNumericChar((int)($nextTwoCharsBits / 45));
122
			$result           .= $toAlphaNumericChar($nextTwoCharsBits % 45);
123
			$length           -= 2;
124
		}
125
126
		if($length === 1){
127
			// special case: one character left
128
			if($bitBuffer->available() < 6){
129
				throw new QRCodeDataException('not enough bits available'); // @codeCoverageIgnore
130
			}
131
132
			$result .= $toAlphaNumericChar($bitBuffer->read(6));
133
		}
134
135
		return $result;
136
	}
137
138
}
139