Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CBOR\OtherObject;
6
7 use function array_key_exists;
8 use InvalidArgumentException;
9
10 final class OtherObjectManager implements OtherObjectManagerInterface
11 {
12 /**
13 * @param class-string<OtherObjectInterface>[] $classes
14 */
15 public function __construct(
16 private array $classes = [],
17 ) {
18 }
19
20 /**
21 * @param class-string<OtherObjectInterface>[] $classes
22 */
23 public static function create(array $classes = []): self
24 {
25 return new self($classes);
26 }
27
28 /**
29 * @param class-string<OtherObjectInterface> $class
30 */
31 public function add(string $class): self
32 {
33 foreach ($class::supportedAdditionalInformation() as $ai) {
34 if ($ai < 0) {
35 throw new InvalidArgumentException('Invalid additional information.');
36 }
37 $this->classes[$ai] = $class;
38 }
39
40 return $this;
41 }
42
43 /**
44 * @return class-string<OtherObjectInterface>
45 */
46 public function getClassForValue(int $value): string
47 {
48 return array_key_exists($value, $this->classes) ? $this->classes[$value] : GenericObject::class;
49 }
50
51 public function createObjectForValue(int $value, ?string $data): OtherObjectInterface
52 {
53 $class = $this->getClassForValue($value);
54
55 return $class::createFromLoadedData($value, $data);
56 }
57 }