1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Commands\Wrappers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Commands\CommandInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
23
|
|
|
use function assert; |
24
|
|
|
use function is_string; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Limoncello\Commands |
28
|
|
|
*/ |
29
|
|
|
class DataArgumentWrapper |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $data; |
35
|
3 |
|
|
36
|
|
|
/** |
37
|
3 |
|
* @param array $data |
38
|
|
|
*/ |
39
|
3 |
|
public function __construct(array $data) |
40
|
|
|
{ |
41
|
|
|
$this->data = $data; |
42
|
|
|
|
43
|
|
|
assert(!($this->isRequired() && $this->isOptional())); |
44
|
|
|
} |
45
|
3 |
|
|
46
|
|
|
/** |
47
|
3 |
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getName(): string |
50
|
|
|
{ |
51
|
|
|
return $this->getData()[CommandInterface::ARGUMENT_NAME]; |
52
|
|
|
} |
53
|
3 |
|
|
54
|
|
|
/** |
55
|
3 |
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getDescription(): string |
58
|
|
|
{ |
59
|
|
|
return $this->getData()[CommandInterface::ARGUMENT_DESCRIPTION] ?? ''; |
60
|
|
|
} |
61
|
3 |
|
|
62
|
|
|
/** |
63
|
3 |
|
* @return string|null |
64
|
|
|
*/ |
65
|
3 |
|
public function getDefault(): ?string |
66
|
|
|
{ |
67
|
3 |
|
$value = $this->getData()[CommandInterface::ARGUMENT_DEFAULT] ?? null; |
68
|
|
|
|
69
|
|
|
assert(is_string($value) === true || $value === null); |
70
|
|
|
|
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
3 |
|
|
74
|
|
|
/** |
75
|
3 |
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function isRequired(): bool |
78
|
|
|
{ |
79
|
|
|
return ($this->getModeValue() & CommandInterface::ARGUMENT_MODE__REQUIRED) > 0; |
80
|
|
|
} |
81
|
3 |
|
|
82
|
|
|
/** |
83
|
3 |
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function isOptional(): bool |
86
|
|
|
{ |
87
|
|
|
return ($this->getModeValue() & CommandInterface::ARGUMENT_MODE__OPTIONAL) > 0; |
88
|
|
|
} |
89
|
3 |
|
|
90
|
|
|
/** |
91
|
3 |
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function isArray(): bool |
94
|
|
|
{ |
95
|
|
|
return ($this->getModeValue() & CommandInterface::ARGUMENT_MODE__IS_ARRAY) > 0; |
96
|
|
|
} |
97
|
3 |
|
|
98
|
|
|
/** |
99
|
3 |
|
* @return int|null |
100
|
|
|
*/ |
101
|
3 |
|
public function getMode(): ?int |
102
|
3 |
|
{ |
103
|
3 |
|
$mode = null; |
104
|
|
|
|
105
|
3 |
|
$this->isRequired() === false ?: $mode = (int)$mode | InputArgument::REQUIRED; |
106
|
|
|
$this->isOptional() === false ?: $mode = (int)$mode | InputArgument::OPTIONAL; |
107
|
|
|
$this->isArray() === false ?: $mode = (int)$mode | InputArgument::IS_ARRAY; |
108
|
|
|
|
109
|
|
|
return $mode; |
110
|
|
|
} |
111
|
3 |
|
|
112
|
|
|
/** |
113
|
3 |
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
protected function getModeValue(): int |
116
|
|
|
{ |
117
|
|
|
return $this->getData()[CommandInterface::ARGUMENT_MODE] ?? 0; |
118
|
|
|
} |
119
|
3 |
|
|
120
|
|
|
/** |
121
|
3 |
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function getData(): array |
124
|
|
|
{ |
125
|
|
|
return $this->data; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|