konecnyjakub /
mytester
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace MyTester\Config; |
||
| 5 | |||
| 6 | use MyTester\ResultsFormatters\Helper as ResultsHelper; |
||
| 7 | use ValueError; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @author Jakub Konečný |
||
| 11 | * @internal |
||
| 12 | */ |
||
| 13 | final readonly class CliArgumentsConfigAdapter implements ConfigAdapter |
||
| 14 | { |
||
| 15 | public const string ARGUMENT_PATH = "path"; |
||
| 16 | public const string ARGUMENT_COLORS = "--colors"; |
||
| 17 | public const string ARGUMENT_NO_PHPT = "--noPhpt"; |
||
| 18 | public const string ARGUMENT_RESULTS = "--results"; |
||
| 19 | public const string ARGUMENT_FILTER_ONLY_GROUPS = "--filterOnlyGroups"; |
||
| 20 | public const string ARGUMENT_FILTER_EXCEPT_GROUPS = "--filterExceptGroups"; |
||
| 21 | public const string ARGUMENT_FILTER_EXCEPT_FOLDERS = "--filterExceptFolders"; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param array{path?: string, "--colors"?: bool, "--results": string[], "--filterOnlyGroups": string, "--filterExceptGroups": string,"--filterExceptFolders": string, "--noPhpt"?: bool} $parsedOptions |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | */ |
||
| 26 | 1 | public function __construct(private array $parsedOptions) |
|
| 27 | { |
||
| 28 | 1 | } |
|
| 29 | |||
| 30 | public function getPriority(): int |
||
| 31 | { |
||
| 32 | 1 | return PHP_INT_MAX; |
|
| 33 | } |
||
| 34 | |||
| 35 | public function getUseColors(): ?bool |
||
| 36 | { |
||
| 37 | 1 | return isset($this->parsedOptions[self::ARGUMENT_COLORS]) ? true : null; |
|
| 38 | } |
||
| 39 | |||
| 40 | public function getIncludePhptTests(): ?bool |
||
| 41 | { |
||
| 42 | 1 | return isset($this->parsedOptions[self::ARGUMENT_NO_PHPT]) ? false : null; |
|
| 43 | } |
||
| 44 | |||
| 45 | public function getPath(): ?string |
||
| 46 | { |
||
| 47 | 1 | return $this->parsedOptions[self::ARGUMENT_PATH] ?? null; |
|
| 48 | } |
||
| 49 | |||
| 50 | public function getOnlyGroups(): array |
||
| 51 | { |
||
| 52 | 1 | return $this->getArrayFromList($this->parsedOptions[self::ARGUMENT_FILTER_ONLY_GROUPS]); |
|
| 53 | } |
||
| 54 | |||
| 55 | public function getExcludedGroups(): array |
||
| 56 | { |
||
| 57 | 1 | return $this->getArrayFromList($this->parsedOptions[self::ARGUMENT_FILTER_EXCEPT_GROUPS]); |
|
| 58 | } |
||
| 59 | |||
| 60 | public function getExcludedFolders(): array |
||
| 61 | { |
||
| 62 | 1 | return $this->getArrayFromList($this->parsedOptions[self::ARGUMENT_FILTER_EXCEPT_FOLDERS]); |
|
| 63 | } |
||
| 64 | |||
| 65 | public function getResultsFormatters(): array |
||
| 66 | { |
||
| 67 | 1 | $resultsFormatters = []; |
|
| 68 | 1 | foreach ($this->parsedOptions[self::ARGUMENT_RESULTS] as $results) { |
|
| 69 | 1 | $results = explode(":", $results, 2); |
|
| 70 | 1 | if (!array_key_exists($results[0], ResultsHelper::$availableFormatters)) { |
|
| 71 | 1 | throw new ValueError("Unknown results formatter " . $results[0]); |
|
| 72 | } |
||
| 73 | /** @var \MyTester\ResultsFormatter $resultsFormatter */ |
||
| 74 | 1 | $resultsFormatter = new ResultsHelper::$availableFormatters[$results[0]](); |
|
| 75 | 1 | if (isset($results[1])) { |
|
| 76 | 1 | $resultsFormatter->setOutputFileName($results[1]); |
|
| 77 | } |
||
| 78 | 1 | $resultsFormatters[] = $resultsFormatter; |
|
| 79 | } |
||
| 80 | 1 | return $resultsFormatters; |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return string[] |
||
| 85 | */ |
||
| 86 | private function getArrayFromList(string $value): array |
||
| 87 | { |
||
| 88 | 1 | if ($value === "") { |
|
| 89 | 1 | return []; |
|
| 90 | } |
||
| 91 | 1 | if (!str_contains($value, ",")) { |
|
| 92 | 1 | return [$value]; |
|
| 93 | } |
||
| 94 | 1 | return explode(",", $value); |
|
| 95 | } |
||
| 96 | } |
||
| 97 |