Total Complexity | 6 |
Total Lines | 59 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
15 | final class Bits implements Value |
||
16 | { |
||
17 | private $value; |
||
18 | private $original; |
||
19 | |||
20 | 92 | public function __construct(bool $first, bool ...$bits) |
|
21 | { |
||
22 | 92 | $this->original = Stream::of('bool', $first, ...$bits); |
|
23 | 92 | } |
|
24 | |||
25 | 58 | public static function fromStream(Readable $stream): Value |
|
26 | { |
||
27 | 58 | return new self( |
|
28 | ...$stream |
||
29 | 58 | ->read(1) |
|
30 | 58 | ->toEncoding('ASCII') |
|
31 | 58 | ->chunk() |
|
32 | 58 | ->reduce( |
|
33 | 58 | new Seq, |
|
34 | 58 | static function(Seq $bits, Str $bit): Seq { |
|
35 | 58 | return (new Str(decbin(ord((string) $bit)))) |
|
36 | 58 | ->chunk() |
|
37 | 58 | ->reduce( |
|
38 | 58 | $bits, |
|
39 | 58 | static function(Seq $bits, Str $bit): Seq { |
|
40 | 58 | return $bits->add((int) (string) $bit); |
|
41 | 58 | } |
|
42 | ); |
||
43 | 58 | } |
|
44 | ) |
||
45 | 58 | ->map(static function(int $bit): bool { |
|
46 | 58 | return (bool) $bit; |
|
47 | 58 | }) |
|
48 | 58 | ->reverse() |
|
49 | ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return StreamInterface<bool> |
||
54 | */ |
||
55 | 44 | public function original(): StreamInterface |
|
56 | { |
||
57 | 44 | return $this->original; |
|
58 | } |
||
59 | |||
60 | 91 | public function __toString(): string |
|
74 | } |
||
75 | } |
||
76 |