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