Passed
Push — v5 ( 043137...387bee )
by smiley
09:56
created

BitBuffer::getBuffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
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\Common
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Common;
14
15
use function count, floor;
16
17
/**
18
 * Holds the raw binary data
19
 */
20
final class BitBuffer{
21
22
	/**
23
	 * The buffer content
24
	 *
25
	 * @var int[]
26
	 */
27
	protected array $buffer = [];
28
29
	/**
30
	 * Length of the content (bits)
31
	 */
32
	protected int $length = 0;
33
34
	/**
35
	 * clears the buffer
36
	 */
37
	public function clear():BitBuffer{
38
		$this->buffer = [];
39
		$this->length = 0;
40
41
		return $this;
42
	}
43
44
	/**
45
	 * appends a sequence of bits
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
	 * appends a single bit
58
	 */
59
	public function putBit(bool $bit):BitBuffer{
60
		$bufIndex = floor($this->length / 8);
61
62
		if(count($this->buffer) <= $bufIndex){
63
			$this->buffer[] = 0;
64
		}
65
66
		if($bit === true){
67
			$this->buffer[(int)$bufIndex] |= (0x80 >> ($this->length % 8));
68
		}
69
70
		$this->length++;
71
72
		return $this;
73
	}
74
75
	/**
76
	 * returns the current buffer length
77
	 */
78
	public function getLength():int{
79
		return $this->length;
80
	}
81
82
	/**
83
	 * returns the buffer content
84
	 */
85
	public function getBuffer():array{
86
		return $this->buffer;
87
	}
88
89
}
90