Completed
Push — master ( 693e1e...7ea7b3 )
by smiley
03:06
created

AlphaNum::getCharCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class AlphaNum
4
 *
5
 * @filesource   AlphaNum.php
6
 * @created      25.11.2015
7
 * @package      chillerlan\QRCode\Data
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Data;
14
15
use chillerlan\QRCode\QRCode;
16
17
/**
18
 * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
19
 */
20
class AlphaNum extends QRDataAbstract{
21
22
	const CHAR_MAP = [
23
		'0', '1', '2', '3', '4', '5', '6', '7',
24
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
25
		'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
26
		'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
27
		'W', 'X', 'Y', 'Z', ' ', '$', '%', '*',
28
		'+', '-', '.', '/', ':',
29
	];
30
31
	/**
32
	 * @var int
33
	 */
34
	protected $datamode = QRCode::DATA_ALPHANUM;
35
36
	/**
37
	 * @var array
38
	 */
39
	protected $lengthBits = [9, 11, 13];
40
41
	/**
42
	 * @inheritdoc
43
	 */
44
	protected function write(string $data){
45
46
		for($i = 0; $i + 1 < $this->strlen; $i += 2){
47
			$this->bitBuffer->put($this->getCharCode($data[$i]) * 45 + $this->getCharCode($data[$i + 1]), 11);
48
		}
49
50
		if($i < $this->strlen){
51
			$this->bitBuffer->put($this->getCharCode($data[$i]), 6);
52
		}
53
54
	}
55
56
	/**
57
	 * @param string $chr
58
	 *
59
	 * @return int
60
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
61
	 */
62
	protected function getCharCode(string $chr):int {
63
		$i = array_search($chr, self::CHAR_MAP);
64
65
		if($i !== false){
66
			return $i;
67
		}
68
69
		throw new QRCodeDataException('illegal char: "'.$chr.'" ['.ord($chr).']');
70
	}
71
72
}
73