|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Frame\Value; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
|
7
|
|
|
Transport\Frame\Value, |
|
8
|
|
|
Exception\OutOfRangeValue, |
|
9
|
|
|
Exception\StringNotOfExpectedLength |
|
10
|
|
|
}; |
|
11
|
|
|
use Innmind\Math\{ |
|
12
|
|
|
Algebra\Integer, |
|
13
|
|
|
DefinitionSet\Set, |
|
14
|
|
|
DefinitionSet\Range |
|
15
|
|
|
}; |
|
16
|
|
|
use Innmind\Immutable\Str; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Same as unsigned shortshort |
|
20
|
|
|
*/ |
|
21
|
|
|
final class UnsignedOctet implements Value |
|
22
|
|
|
{ |
|
23
|
|
|
private static $definitionSet; |
|
24
|
|
|
|
|
25
|
|
|
private $value; |
|
26
|
|
|
private $original; |
|
27
|
|
|
|
|
28
|
280 |
|
public function __construct(Integer $octet) |
|
29
|
|
|
{ |
|
30
|
280 |
|
if (!self::definitionSet()->contains($octet)) { |
|
31
|
6 |
|
throw new OutOfRangeValue($octet, self::definitionSet()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
274 |
|
$this->original = $octet; |
|
35
|
274 |
|
} |
|
36
|
|
|
|
|
37
|
170 |
|
public static function fromString(Str $string): Value |
|
38
|
|
|
{ |
|
39
|
170 |
|
$string = $string->toEncoding('ASCII'); |
|
40
|
|
|
|
|
41
|
170 |
|
if ($string->length() !== 1) { |
|
42
|
2 |
|
throw new StringNotOfExpectedLength($string, 1); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
168 |
|
[, $octet] = unpack('C', (string) $string); |
|
46
|
|
|
|
|
47
|
168 |
|
return new self(new Integer($octet)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
104 |
|
public static function cut(Str $string): Str |
|
51
|
|
|
{ |
|
52
|
104 |
|
return $string->toEncoding('ASCII')->substring(0, 1); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
172 |
|
public function original(): Integer |
|
56
|
|
|
{ |
|
57
|
172 |
|
return $this->original; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
266 |
|
public function __toString(): string |
|
61
|
|
|
{ |
|
62
|
266 |
|
return $this->value ?? $this->value = chr($this->original->value()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
280 |
|
public static function definitionSet(): Set |
|
66
|
|
|
{ |
|
67
|
280 |
|
return self::$definitionSet ?? self::$definitionSet = Range::inclusive( |
|
68
|
|
|
new Integer(0), |
|
69
|
280 |
|
new Integer(255) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|