1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the box project. |
||
7 | * |
||
8 | * (c) Kevin Herrera <[email protected]> |
||
9 | * Théo Fidry <[email protected]> |
||
10 | * |
||
11 | * This source file is subject to the MIT license that is bundled |
||
12 | * with this source code in the file LICENSE. |
||
13 | */ |
||
14 | |||
15 | namespace KevinGH\Box\Composer\Package; |
||
16 | |||
17 | use function array_filter; |
||
18 | use function array_key_exists; |
||
19 | use function array_keys; |
||
20 | use function array_map; |
||
21 | use function array_values; |
||
22 | |||
23 | /** |
||
24 | * @private |
||
25 | */ |
||
26 | final readonly class PackageInfo |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
27 | { |
||
28 | public function __construct(private array $packageInfo) |
||
29 | { |
||
30 | } |
||
31 | |||
32 | public function getName(): string |
||
33 | { |
||
34 | return $this->packageInfo['name']; |
||
35 | } |
||
36 | |||
37 | public function getRequiredPhpVersion(): ?string |
||
38 | { |
||
39 | return $this->packageInfo['require']['php'] ?? null; |
||
40 | } |
||
41 | |||
42 | public function hasRequiredPhpVersion(): bool |
||
43 | { |
||
44 | return null !== $this->getRequiredPhpVersion(); |
||
45 | } |
||
46 | |||
47 | public function getRequiredExtensions(): Extensions |
||
48 | { |
||
49 | return self::parseExtensions($this->packageInfo['require'] ?? []); |
||
50 | } |
||
51 | |||
52 | public function getPolyfilledExtensions(): Extensions |
||
53 | { |
||
54 | if (array_key_exists('provide', $this->packageInfo)) { |
||
55 | return self::parseExtensions($this->packageInfo['provide']); |
||
56 | } |
||
57 | |||
58 | // TODO: remove the following code in 5.0. |
||
59 | $packageName = $this->packageInfo['name']; |
||
60 | $polyfilledExtension = Extension::tryToParsePolyfill($packageName); |
||
61 | |||
62 | return new Extensions( |
||
63 | null === $polyfilledExtension |
||
64 | ? [] |
||
65 | : [$polyfilledExtension], |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | public function getConflictingExtensions(): Extensions |
||
70 | { |
||
71 | return self::parseExtensions($this->packageInfo['conflict'] ?? []); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param array<string, string> $constraints |
||
76 | */ |
||
77 | public static function parseExtensions(array $constraints): Extensions |
||
78 | { |
||
79 | return new Extensions( |
||
80 | array_values( |
||
81 | array_filter( |
||
82 | array_map( |
||
83 | Extension::tryToParse(...), |
||
84 | array_keys($constraints), |
||
85 | ), |
||
86 | static fn (?Extension $extension) => null !== $extension, |
||
87 | ), |
||
88 | ), |
||
89 | ); |
||
90 | } |
||
91 | } |
||
92 |