Issues (224)

src/RequirementChecker/Requirement.php (1 issue)

Labels
Severity
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\RequirementChecker;
16
17
/**
18
 * @private
19
 */
20
final readonly class Requirement
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 20 at column 6
Loading history...
21
{
22
    public function __construct(
23
        public RequirementType $type,
24
        public string $condition,
25
        public ?string $source,
26
        public string $message,
27
        public string $helpMessage,
28
    ) {
29
    }
30
31
    public static function forPHP(string $requiredPhpVersion, ?string $packageName): self
32
    {
33
        return new self(
34
            RequirementType::PHP,
35
            $requiredPhpVersion,
36
            $packageName,
37
            null === $packageName
38
                ? sprintf(
39
                    'This application requires a PHP version matching "%s".',
40
                    $requiredPhpVersion,
41
                )
42
                : sprintf(
43
                    'The package "%s" requires a PHP version matching "%s".',
44
                    $packageName,
45
                    $requiredPhpVersion,
46
                ),
47
            null === $packageName
48
                ? sprintf(
49
                    'This application requires a PHP version matching "%s".',
50
                    $requiredPhpVersion,
51
                )
52
                : sprintf(
53
                    'The package "%s" requires a PHP version matching "%s".',
54
                    $packageName,
55
                    $requiredPhpVersion,
56
                ),
57
        );
58
    }
59
60
    public static function forRequiredExtension(string $extension, ?string $packageName): self
61
    {
62
        return new self(
63
            RequirementType::EXTENSION,
64
            $extension,
65
            $packageName,
66
            null === $packageName
67
                ? sprintf(
68
                    'This application requires the extension "%s".',
69
                    $extension,
70
                )
71
                : sprintf(
72
                    'The package "%s" requires the extension "%s".',
73
                    $packageName,
74
                    $extension,
75
                ),
76
            null === $packageName
77
                ? sprintf(
78
                    'This application requires the extension "%s". You either need to enable it or request the application to be shipped with a polyfill for this extension.',
79
                    $extension,
80
                )
81
                : sprintf(
82
                    'The package "%s" requires the extension "%s". You either need to enable it or request the application to be shipped with a polyfill for this extension.',
83
                    $packageName,
84
                    $extension,
85
                ),
86
        );
87
    }
88
89
    public static function forConflictingExtension(string $extension, ?string $packageName): self
90
    {
91
        return new self(
92
            RequirementType::EXTENSION_CONFLICT,
93
            $extension,
94
            $packageName,
95
            null === $packageName
96
                ? sprintf(
97
                    'This application conflicts with the extension "%s".',
98
                    $extension,
99
                )
100
                : sprintf(
101
                    'The package "%s" conflicts with the extension "%s".',
102
                    $packageName,
103
                    $extension,
104
                ),
105
            null === $packageName
106
                ? sprintf(
107
                    'This application conflicts with the extension "%s". You need to disable it in order to run this application.',
108
                    $extension,
109
                )
110
                : sprintf(
111
                    'The package "%s" conflicts with the extension "%s". You need to disable it in order to run this application.',
112
                    $packageName,
113
                    $extension,
114
                ),
115
        );
116
    }
117
118
    public static function fromArray(array $value): self
119
    {
120
        return new self(
121
            RequirementType::from($value['type']),
122
            $value['condition'],
123
            $value['source'] ?? null,
124
            $value['message'],
125
            $value['helpMessage'],
126
        );
127
    }
128
129
    public function toArray(): array
130
    {
131
        return [
132
            'type' => $this->type->value,
133
            'condition' => $this->condition,
134
            'source' => $this->source,
135
            'message' => $this->message,
136
            'helpMessage' => $this->helpMessage,
137
        ];
138
    }
139
}
140