Debian Codegraph @

lib/3rdParty/composer/spomky-labs/cbor-php/src/OtherObject/SimpleObject.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CBOR\OtherObject;
6
7 use CBOR\Normalizable;
8 use CBOR\OtherObject as Base;
9 use CBOR\Utils;
10 use function chr;
11 use InvalidArgumentException;
12 use function ord;
13 use function strlen;
14
15 final class SimpleObject extends Base implements Normalizable
16 {
17 public static function supportedAdditionalInformation(): array
18 {
19 return [...range(0, 19), 24];
20 }
21
22 public static function create(int $value): self|FalseObject|TrueObject|NullObject|UndefinedObject
23 {
24 switch (true) {
25 case $value >= 0 && $value <= 19:
26 return new self($value, null);
27 case $value === 20:
28 return FalseObject::create();
29 case $value === 21:
30 return TrueObject::create();
31 case $value === 22:
32 return NullObject::create();
33 case $value === 23:
34 return UndefinedObject::create();
35 case $value <= 31:
36 throw new InvalidArgumentException('Invalid simple value. Shall be between 32 and 255.');
37 case $value <= 255:
38 return new self(24, chr($value));
39 default:
40 throw new InvalidArgumentException('The value is not a valid simple value.');
41 }
42 }
43
44 public static function createFromLoadedData(int $additionalInformation, ?string $data): Base
45 {
46 if ($additionalInformation === 24) {
47 if ($data === null) {
48 throw new InvalidArgumentException('Invalid simple value. Content data is missing.');
49 }
50 if (strlen($data) !== 1) {
51 throw new InvalidArgumentException('Invalid simple value. Content data is too long.');
52 }
53 if (ord($data) < 32) {
54 throw new InvalidArgumentException('Invalid simple value. Content data must be between 32 and 255.');
55 }
56 } elseif ($additionalInformation < 20) {
57 if ($data !== null) {
58 throw new InvalidArgumentException('Invalid simple value. Content data should not be present.');
59 }
60 }
61
62 return new self($additionalInformation, $data);
63 }
64
65 public function normalize(): int
66 {
67 if ($this->data === null) {
68 return $this->getAdditionalInformation();
69 }
70
71 return Utils::binToInt($this->data);
72 }
73 }