1 | <?php |
||
2 | |||
3 | namespace NovaResourceDynamicExport; |
||
4 | |||
5 | use NovaResourceDynamicExport\Export\CustomExport; |
||
6 | |||
7 | class CustomResourcesExport |
||
8 | { |
||
9 | /** |
||
10 | * @var array<CustomResourcesExport> |
||
11 | */ |
||
12 | public static array $exports = []; |
||
13 | |||
14 | 10 | public static function use(string|CustomExport|array $export): static |
|
15 | { |
||
16 | 10 | if (is_array($export)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
17 | foreach ($export as $exportItem) { |
||
18 | static::use($exportItem); |
||
19 | } |
||
20 | |||
21 | return new static; |
||
22 | } |
||
23 | |||
24 | 10 | if (!is_string($export)) { |
|
25 | $export = $export::class; |
||
26 | } |
||
27 | |||
28 | 10 | if (!is_subclass_of($export, CustomExport::class)) { |
|
29 | throw new \Exception('Custom export should be subclass of ' . CustomExport::class); |
||
30 | } |
||
31 | |||
32 | 10 | static::$exports[] = $export; |
|
33 | |||
34 | 10 | return new static; |
|
35 | } |
||
36 | |||
37 | 3 | public static function options(): array |
|
38 | { |
||
39 | 3 | $exportsList = []; |
|
40 | 3 | $exports = static::$exports; |
|
41 | 3 | if(!empty($exports)) { |
|
42 | /** @var CustomExport $export */ |
||
43 | 3 | foreach ($exports as $export) { |
|
44 | 3 | $exportsList[$export::key()] = $export::name(); |
|
45 | } |
||
46 | } |
||
47 | |||
48 | 3 | return $exportsList; |
|
49 | } |
||
50 | |||
51 | 1 | public static function findByKey(string $key): ?CustomExport |
|
52 | { |
||
53 | 1 | $customExports = static::$exports; |
|
54 | 1 | if(!empty($customExports)) { |
|
55 | /** @var CustomExport $export */ |
||
56 | 1 | foreach ($customExports as $export) { |
|
57 | 1 | if($export::key() != $key) { |
|
58 | continue; |
||
59 | } |
||
60 | |||
61 | 1 | return new $export; |
|
62 | } |
||
63 | } |
||
64 | |||
65 | return null; |
||
66 | } |
||
67 | } |
||
68 |