Total Complexity | 6 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
17 | final class Context |
||
18 | { |
||
19 | /** |
||
20 | * @var object[] |
||
21 | */ |
||
22 | private array $objects = []; |
||
23 | |||
24 | /** |
||
25 | * @param iterable<object> $objects |
||
26 | */ |
||
27 | public function __construct(iterable $objects = []) |
||
28 | { |
||
29 | foreach ($objects as $object) { |
||
30 | $this->add($object); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Adds an object to the context. |
||
36 | */ |
||
37 | public function add(object $object): self |
||
38 | { |
||
39 | array_unshift($this->objects, $object); |
||
40 | |||
41 | return $this; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Returns the object matching the specified class. |
||
46 | * |
||
47 | * @template T of object |
||
48 | * |
||
49 | * @param class-string<T> $class |
||
50 | * |
||
51 | * @return T |
||
52 | * |
||
53 | * @throws NotInContext |
||
54 | */ |
||
55 | public function get(string $class): object |
||
64 | } |
||
65 | } |
||
66 |