1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace Shopware\Core\Framework\Struct; |
||
4 | |||
5 | trait ExtendableTrait |
||
6 | { |
||
7 | /** |
||
8 | * Contains an array of extension structs. |
||
9 | * |
||
10 | * @var Struct[] |
||
11 | */ |
||
12 | protected $extensions = []; |
||
13 | |||
14 | /** |
||
15 | * Adds a new extension struct into the class storage. |
||
16 | * The passed name is used as unique identifier and has to be stored too. |
||
17 | */ |
||
18 | public function addExtension(string $name, ?Struct $extension): void |
||
19 | { |
||
20 | $this->extensions[$name] = $extension; |
||
21 | } |
||
22 | |||
23 | 498 | /** |
|
24 | * @param Struct[] $extensions |
||
25 | 498 | */ |
|
26 | 498 | public function addExtensions(array $extensions): void |
|
27 | { |
||
28 | foreach ($extensions as $key => $extension) { |
||
29 | $this->addExtension($key, $extension); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Returns a single extension struct element of this class. |
||
35 | * The passed name is used as unique identifier. |
||
36 | */ |
||
37 | public function getExtension(string $name): ?Struct |
||
38 | { |
||
39 | return $this->extensions[$name] ?? null; |
||
40 | } |
||
41 | |||
42 | 327 | public function getExtensionOfType(string $name, string $type): ?Struct |
|
43 | { |
||
44 | 327 | if ($this->hasExtensionOfType($name, $type)) { |
|
45 | 167 | return $this->getExtension($name); |
|
46 | } |
||
47 | |||
48 | 303 | return null; |
|
49 | } |
||
50 | |||
51 | 25 | /** |
|
52 | * Helper function which checks if an associated |
||
53 | 25 | * extension exists. |
|
54 | 5 | */ |
|
55 | public function hasExtension(string $name): bool |
||
56 | { |
||
57 | 25 | return isset($this->extensions[$name]); |
|
58 | } |
||
59 | |||
60 | public function hasExtensionOfType(string $name, string $type): bool |
||
61 | { |
||
62 | return $this->hasExtension($name) && \get_class($this->getExtension($name)) === $type; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
63 | } |
||
64 | 33 | ||
65 | /** |
||
66 | 33 | * Returns all stored extension structures of this class. |
|
67 | * The array has to be an associated array with name and extension instance. |
||
68 | * |
||
69 | 25 | * @return Struct[] |
|
70 | */ |
||
71 | 25 | public function getExtensions(): array |
|
72 | { |
||
73 | return $this->extensions; |
||
74 | } |
||
75 | |||
76 | public function setExtensions(array $extensions): void |
||
77 | { |
||
78 | $this->extensions = $extensions; |
||
79 | } |
||
80 | 248 | ||
81 | public function removeExtension(string $name): void |
||
82 | 248 | { |
|
83 | if (isset($this->extensions[$name])) { |
||
84 | unset($this->extensions[$name]); |
||
85 | 80 | } |
|
86 | } |
||
87 | } |
||
88 |