|
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
|
|
|
final class SignedLongInteger implements Value |
|
20
|
|
|
{ |
|
21
|
|
|
private static $definitionSet; |
|
22
|
|
|
|
|
23
|
|
|
private $value; |
|
24
|
|
|
private $original; |
|
25
|
|
|
|
|
26
|
16 |
|
public function __construct(Integer $value) |
|
27
|
|
|
{ |
|
28
|
16 |
|
if (!self::definitionSet()->contains($value)) { |
|
29
|
2 |
|
throw new OutOfRangeValue($value, self::definitionSet()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
14 |
|
$this->value = pack('l', $value->value()); |
|
33
|
14 |
|
$this->original = $value; |
|
34
|
14 |
|
} |
|
35
|
|
|
|
|
36
|
7 |
|
public static function fromString(Str $string): Value |
|
37
|
|
|
{ |
|
38
|
7 |
|
$string = $string->toEncoding('ASCII'); |
|
39
|
|
|
|
|
40
|
7 |
|
if ($string->length() !== 4) { |
|
41
|
1 |
|
throw new StringNotOfExpectedLength($string, 4); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
[, $value] = unpack('l', (string) $string); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
6 |
|
return new self(new Integer($value)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
6 |
|
public static function cut(Str $string): Str |
|
50
|
|
|
{ |
|
51
|
6 |
|
return $string->toEncoding('ASCII')->substring(0, 4); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
10 |
|
public function original(): Integer |
|
55
|
|
|
{ |
|
56
|
10 |
|
return $this->original; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
13 |
|
public function __toString(): string |
|
60
|
|
|
{ |
|
61
|
13 |
|
return $this->value; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
16 |
|
public static function definitionSet(): Set |
|
65
|
|
|
{ |
|
66
|
16 |
|
return self::$definitionSet ?? self::$definitionSet = Range::inclusive( |
|
67
|
1 |
|
new Integer(-2147483648), |
|
68
|
1 |
|
new Integer(2147483647) |
|
69
|
16 |
|
)->intersect(new Integers); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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.