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
|
|
|
DefinitionSet\Integers |
16
|
|
|
}; |
17
|
|
|
use Innmind\Immutable\Str; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Same as shortshort |
21
|
|
|
*/ |
22
|
|
|
final class SignedOctet implements Value |
23
|
|
|
{ |
24
|
|
|
private static $definitionSet; |
25
|
|
|
|
26
|
|
|
private $value; |
27
|
|
|
private $original; |
28
|
|
|
|
29
|
11 |
|
public function __construct(Integer $octet) |
30
|
|
|
{ |
31
|
11 |
|
if (!self::definitionSet()->contains($octet)) { |
32
|
2 |
|
throw new OutOfRangeValue($octet, self::definitionSet()); |
33
|
|
|
} |
34
|
|
|
|
35
|
9 |
|
$this->value = pack('c', $octet->value()); |
36
|
9 |
|
$this->original = $octet; |
37
|
9 |
|
} |
38
|
|
|
|
39
|
6 |
|
public static function fromString(Str $string): Value |
40
|
|
|
{ |
41
|
6 |
|
$string = $string->toEncoding('ASCII'); |
42
|
|
|
|
43
|
6 |
|
if ($string->length() !== 1) { |
44
|
1 |
|
throw new StringNotOfExpectedLength($string, 1); |
45
|
|
|
} |
46
|
|
|
|
47
|
5 |
|
[, $value] = unpack('c', (string) $string); |
|
|
|
|
48
|
|
|
|
49
|
5 |
|
return new self(new Integer($value)); |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
public static function cut(Str $string): Str |
53
|
|
|
{ |
54
|
5 |
|
return $string->toEncoding('ASCII')->substring(0, 1); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
public function original(): Integer |
58
|
|
|
{ |
59
|
6 |
|
return $this->original; |
60
|
|
|
} |
61
|
|
|
|
62
|
10 |
|
public function __toString(): string |
63
|
|
|
{ |
64
|
10 |
|
return $this->value; |
65
|
|
|
} |
66
|
|
|
|
67
|
11 |
|
public static function definitionSet(): Set |
68
|
|
|
{ |
69
|
11 |
|
return self::$definitionSet ?? self::$definitionSet = Range::inclusive( |
70
|
|
|
new Integer(-128), |
71
|
|
|
new Integer(127) |
72
|
11 |
|
)->intersect(new Integers); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.