Passed
Push — develop ( 73bc77...71bbc5 )
by nguereza
14:43 queued 18s
created

Parameter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 170
rs 10
c 1
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A isVariadic() 0 3 1
A getDefault() 0 7 2
A getAttributeName() 0 3 1
A filter() 0 9 2
A __construct() 0 17 1
A getName() 0 3 1
A getRaw() 0 3 1
A getFilter() 0 3 1
A isRequired() 0 3 1
1
<?php
2
3
/**
4
 * Platine Console
5
 *
6
 * Platine Console is a powerful library with support of custom
7
 * style to build command line interface applications
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Console
12
 * Copyright (c) 2017-2020 Jitendra Adhikari
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file Parameter.php
35
 *
36
 *  The Input Parameter class
37
 *
38
 *  @package    Platine\Console\Input
39
 *  @author Platine Developers Team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   https://www.platine-php.com
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Console\Input;
50
51
use Platine\Console\Util\Helper;
52
53
/**
54
 * @class Parameter
55
 * @package Platine\Console\Input
56
 */
57
abstract class Parameter
58
{
59
    /**
60
     * The name of the parameter
61
     * @var string
62
     */
63
    protected string $name;
64
65
    /**
66
     * The raw data of the parameter
67
     * @var string
68
     */
69
    protected string $raw;
70
71
    /**
72
     * The description of the parameter
73
     * @var string
74
     */
75
    protected string $description;
76
77
    /**
78
     * The parameter default value
79
     * @var mixed
80
     */
81
    protected $default = null;
82
83
    /**
84
     * The sanitizer/filter callback
85
     * @var callable|null
86
     */
87
    protected $filter = null;
88
89
    /**
90
     * Whether the parameter is required
91
     * @var bool
92
     */
93
    protected bool $required = false;
94
95
    /**
96
     * Whether the parameter is variadic
97
     * @var bool
98
     */
99
    protected bool $variadic = false;
100
101
    /**
102
     * Create new instance
103
     * @param string $raw
104
     * @param string $description
105
     * @param mixed $default
106
     * @param bool $required
107
     * @param bool $variadic
108
     * @param callable|null $filter
109
     */
110
    public function __construct(
111
        string $raw,
112
        string $description = '',
113
        $default = null,
114
        bool $required = false,
115
        bool $variadic = false,
116
        ?callable $filter = null
117
    ) {
118
        $this->raw = $raw;
119
        $this->description = $description;
120
        $this->default = $default;
121
        $this->filter = $filter;
122
        $this->variadic = $variadic;
123
124
        $this->required = $required;
125
126
        $this->parse($raw);
127
    }
128
129
    /**
130
     * Parse raw string representation of parameter.
131
     * @param string $raw
132
     * @return void
133
     */
134
    abstract public function parse(string $raw): void;
135
136
    /**
137
     * Return the parameter name
138
     * @return string
139
     */
140
    public function getName(): string
141
    {
142
        return $this->name;
143
    }
144
145
    /**
146
     * Return the parameter raw value
147
     * @return string
148
     */
149
    public function getRaw(): string
150
    {
151
        return $this->raw;
152
    }
153
154
    /**
155
     * Return the parameter description
156
     * @return string
157
     */
158
    public function getDescription(): string
159
    {
160
        return $this->description;
161
    }
162
163
    /**
164
     * Return the parameter default value
165
     * @return mixed
166
     */
167
    public function getDefault()
168
    {
169
        if ($this->isVariadic()) {
170
            return (array) $this->default;
171
        }
172
173
        return $this->default;
174
    }
175
176
    /**
177
     * Return the parameter filter/sanitizer callback
178
     * @return callable|null
179
     */
180
    public function getFilter(): ?callable
181
    {
182
        return $this->filter;
183
    }
184
185
    /**
186
     * Whether the parameter is required
187
     * @return bool
188
     */
189
    public function isRequired(): bool
190
    {
191
        return $this->required;
192
    }
193
194
    /**
195
     * Whether the parameter is variadic
196
     * @return bool
197
     */
198
    public function isVariadic(): bool
199
    {
200
        return $this->variadic;
201
    }
202
203
    /**
204
     * Run the filter/sanitizer/validation callback for
205
     * the given value
206
     * @param mixed $raw
207
     * @return mixed
208
     */
209
    public function filter($raw)
210
    {
211
        if ($this->filter !== null) {
212
            $callback = $this->filter;
213
214
            return ($callback)($raw);
215
        }
216
217
        return $raw;
218
    }
219
220
    /**
221
     * Return normalized name of this parameter
222
     * @return string
223
     */
224
    public function getAttributeName(): string
225
    {
226
        return Helper::toCamelCase($this->name);
227
    }
228
}
229