Buffer::setStream()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
namespace JsonStreamParser;
4
5
/**
6
 * @author  Stefan Pöhner <[email protected]>
7
 * @license MIT
8
 *
9
 * @package JsonStreamParser
10
 */
11
class Buffer
12
{
13
	/**
14
	 * @var resource
15
	 */
16
	private $stream;
17
18
	/**
19
	 * @var int
20
	 */
21
	private $size;
22
23
	/**
24
	 * @param resource $stream
25
	 */
26
	public function setStream($stream)
27
	{
28
		$this->stream = $stream;
29
	}
30
31
	/**
32
	 * @param int $size
33
	 */
34
	public function setSize(int $size)
35
	{
36
		$this->size = $size;
37
	}
38
39
	/**
40
	 * Get the next character and advance the cursor.
41
	 *
42
	 * @return \Generator
43
	 */
44
	public function get(): \Generator
45
	{
46
		while (!feof($this->stream)) {
47
			$chunk = fread($this->stream, $this->size);
48
49
			$chunk  = preg_split("//u", $chunk, -1, PREG_SPLIT_NO_EMPTY);
50
			$length = count($chunk);
51
52
			for ($i = 0; $i < $length; $i++) {
53
				yield $chunk[$i];
54
			}
55
		}
56
	}
57
}
58