alps-asd /
app-state-diagram
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Koriym\AppStateDiagram; |
||
| 6 | |||
| 7 | use SimpleXMLElement; |
||
| 8 | |||
| 9 | use function array_values; |
||
| 10 | use function explode; |
||
| 11 | use function filter_var; |
||
| 12 | use function is_string; |
||
| 13 | use function property_exists; |
||
| 14 | |||
| 15 | use const FILTER_VALIDATE_INT; |
||
| 16 | |||
| 17 | /** @psalm-immutable */ |
||
| 18 | final class Option |
||
| 19 | { |
||
| 20 | /** @var bool */ |
||
| 21 | public $watch; |
||
| 22 | |||
| 23 | /** @var list<string> */ |
||
| 24 | public $and; |
||
| 25 | |||
| 26 | /** @var list<string> */ |
||
| 27 | public $or; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | public $color; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | public $mode; |
||
| 34 | |||
| 35 | /** @var int */ |
||
| 36 | public $port; |
||
| 37 | |||
| 38 | /** @param array<string, string|bool> $options */ |
||
| 39 | public function __construct(array $options, ?SimpleXMLElement $filter, ?int $port) |
||
| 40 | { |
||
| 41 | $this->watch = isset($options['w']) || isset($options['watch']); |
||
| 42 | $this->and = $this->parseAndTag($options, $filter); |
||
| 43 | $this->or = $this->parseOrTag($options, $filter); |
||
| 44 | $this->color = $this->parseColor($options, $filter); |
||
| 45 | $this->mode = $this->getMode($options); |
||
| 46 | $this->port = $this->getPort($options, $port); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param array<string, string|bool> $options |
||
| 51 | * |
||
| 52 | * @return list<string> |
||
| 53 | */ |
||
| 54 | private function parseAndTag(array $options, ?SimpleXMLElement $filter): array |
||
| 55 | { |
||
| 56 | if (isset($options['and-tag']) && is_string($options['and-tag'])) { |
||
| 57 | return explode(',', $options['and-tag']); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 58 | } |
||
| 59 | |||
| 60 | /** @var list<string> */ // phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat |
||
| 61 | |||
| 62 | return $filter instanceof SimpleXMLElement && property_exists($filter, 'and') ? array_values((array) $filter->and) : []; |
||
|
0 ignored issues
–
show
|
|||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param array<string, string|bool> $options |
||
| 67 | * |
||
| 68 | * @return list<string> |
||
| 69 | */ |
||
| 70 | private function parseOrTag(array $options, ?SimpleXMLElement $filter): array |
||
| 71 | { |
||
| 72 | if (isset($options['or-tag']) && is_string($options['or-tag'])) { |
||
| 73 | return explode(',', $options['or-tag']); |
||
|
0 ignored issues
–
show
|
|||
| 74 | } |
||
| 75 | |||
| 76 | /** @var list<string> */ // phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat |
||
| 77 | |||
| 78 | return $filter instanceof SimpleXMLElement && property_exists($filter, 'or') ? array_values((array) $filter->or) : []; |
||
|
0 ignored issues
–
show
|
|||
| 79 | } |
||
| 80 | |||
| 81 | /** @param array<string, string|bool> $options */ |
||
| 82 | private function parseColor(array $options, ?SimpleXMLElement $filter): string |
||
| 83 | { |
||
| 84 | if (isset($options['color']) && is_string($options['color'])) { |
||
| 85 | return $options['color']; |
||
| 86 | } |
||
| 87 | |||
| 88 | return $filter instanceof SimpleXMLElement && property_exists($filter, 'color') ? (string) $filter->color : ''; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** @param array<string, string|bool> $options */ |
||
| 92 | private function getMode(array $options): string |
||
| 93 | { |
||
| 94 | $isMarkdown = isset($options['mode']) && $options['mode'] === DumpDocs::MODE_MARKDOWN; |
||
| 95 | |||
| 96 | return $isMarkdown ? DumpDocs::MODE_MARKDOWN : DumpDocs::MODE_HTML; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** @param array<string, string|bool> $options */ |
||
| 100 | private function getPort(array $options, ?int $port): int |
||
| 101 | { |
||
| 102 | $value = $options['port'] ?? $port; |
||
| 103 | if ($value === null) { |
||
| 104 | return 3000; |
||
| 105 | } |
||
| 106 | |||
| 107 | return filter_var( |
||
| 108 | $value, |
||
| 109 | FILTER_VALIDATE_INT, |
||
| 110 | [ |
||
| 111 | 'options' => [ |
||
| 112 | 'default' => 3000, |
||
| 113 | 'min_range' => 1024, |
||
| 114 | 'max_range' => 49151, |
||
| 115 | ], |
||
| 116 | ] |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | } |
||
| 120 |