Completed
Push — master ( ef42ef...514bca )
by smiley
06:10
created

AlphaNum   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 44
rs 10
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
use function array_search, ord, sprintf;
18
19
/**
20
 * Alphanumeric mode: 0 to 9, A to Z, space, $ % * + - . / :
21
 */
22
class AlphaNum extends QRDataAbstract{
23
24
	protected int $datamode = QRCode::DATA_ALPHANUM;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
25
26
	protected array $lengthBits = [9, 11, 13];
27
28
	/**
29
	 * @inheritdoc
30
	 */
31
	protected function write(string $data):void{
32
33
		for($i = 0; $i + 1 < $this->strlen; $i += 2){
34
			$this->bitBuffer->put($this->getCharCode($data[$i]) * 45 + $this->getCharCode($data[$i + 1]), 11);
35
		}
36
37
		if($i < $this->strlen){
38
			$this->bitBuffer->put($this->getCharCode($data[$i]), 6);
39
		}
40
41
	}
42
43
	/**
44
	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
45
	 */
46
	protected function getCharCode(string $chr):int{
47
		$i = array_search($chr, $this::ALPHANUM_CHAR_MAP);
48
49
		if($i !== false){
50
			return $i;
51
		}
52
53
		throw new QRCodeDataException(sprintf('illegal char: "%s" [%d]', $chr, ord($chr)));
54
	}
55
56
}
57