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

BitBuffer::putBit()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class BitBuffer
4
 *
5
 * @filesource   BitBuffer.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\Helpers;
14
15
/**
16
 * @property int[] $buffer
17
 * @property int   $length
18
 */
19
class BitBuffer{
20
21
	/**
22
	 * @var  int[]
23
	 */
24
	public $buffer = [];
25
26
	/**
27
	 * @var int
28
	 */
29
	public $length = 0;
30
31
	/**
32
	 * @return \chillerlan\QRCode\Helpers\BitBuffer
33
	 */
34
	public function clear():BitBuffer{
35
		$this->buffer = [];
36
		$this->length = 0;
37
38
		return $this;
39
	}
40
41
	/**
42
	 * @param int $num
43
	 * @param int $length
44
	 *
45
	 * @return \chillerlan\QRCode\Helpers\BitBuffer
46
	 */
47
	public function put(int $num, int $length):BitBuffer{
48
49
		for($i = 0; $i < $length; $i++){
50
			$this->putBit(($num >> ($length - $i - 1))&1 === 1);
51
		}
52
53
		return $this;
54
	}
55
56
	/**
57
	 * @param bool $bit
58
	 *
59
	 * @return \chillerlan\QRCode\Helpers\BitBuffer
60
	 */
61
	public function putBit(bool $bit):BitBuffer{
62
		$bufIndex = floor($this->length / 8);
63
64
		if(count($this->buffer) <= $bufIndex){
65
			$this->buffer[] = 0;
66
		}
67
68
		if($bit === true){
69
			$this->buffer[(int)$bufIndex] |= (0x80 >> ($this->length % 8));
70
		}
71
72
		$this->length++;
73
74
		return $this;
75
	}
76
77
}
78