Parameter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A optional() 0 3 1
A default() 0 7 2
A variadic() 0 3 1
A filter() 0 9 2
A attributeName() 0 3 1
A required() 0 3 1
A raw() 0 3 1
A desc() 0 3 1
A name() 0 3 1
A __construct() 0 11 1
1
<?php
2
3
/*
4
 * This file is part of the PHP-CLI package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Cli\Input;
13
14
use Ahc\Cli\Helper\InflectsString;
15
16
/**
17
 * Cli Parameter.
18
 *
19
 * @author  Jitendra Adhikari <[email protected]>
20
 * @license MIT
21
 *
22
 * @link    https://github.com/adhocore/cli
23
 */
24
abstract class Parameter
25
{
26
    use InflectsString;
27
28
    /** @var string */
29
    protected $name;
30
31
    /** @var string */
32
    protected $raw;
33
34
    /** @var string */
35
    protected $desc;
36
37
    /** @var mixed */
38
    protected $default;
39
40
    /** @var callable The sanitizer/filter callback */
41
    protected $filter;
42
43
    /** @var bool */
44
    protected $required = false;
45
46
    /** @var bool */
47
    protected $optional = false;
48
49
    /** @var bool */
50
    protected $variadic = false;
51
52
    public function __construct(string $raw, string $desc = '', $default = null, callable $filter = null)
53
    {
54
        $this->raw      = $raw;
55
        $this->desc     = $desc;
56
        $this->default  = $default;
57
        $this->filter   = $filter;
58
        $this->required = \strpos($raw, '<') !== false;
59
        $this->optional = \strpos($raw, '[') !== false;
60
        $this->variadic = \strpos($raw, '...') !== false;
61
62
        $this->parse($raw);
63
    }
64
65
    /**
66
     * Parse raw string representation of parameter.
67
     *
68
     * @param string $raw
69
     *
70
     * @return void
71
     */
72
    abstract protected function parse(string $raw);
73
74
    /**
75
     * Get raw definition.
76
     *
77
     * @return string
78
     */
79
    public function raw(): string
80
    {
81
        return $this->raw;
82
    }
83
84
    /**
85
     * Get name.
86
     *
87
     * @return string
88
     */
89
    public function name(): string
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * Get description.
96
     *
97
     * @return string
98
     */
99
    public function desc(): string
100
    {
101
        return $this->desc;
102
    }
103
104
    /**
105
     * Get normalized name.
106
     *
107
     * @return string
108
     */
109
    public function attributeName(): string
110
    {
111
        return $this->toCamelCase($this->name);
112
    }
113
114
    /**
115
     * Check this param is required.
116
     *
117
     * @return bool
118
     */
119
    public function required(): bool
120
    {
121
        return $this->required;
122
    }
123
124
    /**
125
     * Check this param is optional.
126
     *
127
     * @return bool
128
     */
129
    public function optional(): bool
130
    {
131
        return $this->optional;
132
    }
133
134
    /**
135
     * Check this param is variadic.
136
     *
137
     * @return bool
138
     */
139
    public function variadic(): bool
140
    {
141
        return $this->variadic;
142
    }
143
144
    /**
145
     * Gets default value.
146
     *
147
     * @return mixed
148
     */
149
    public function default()
150
    {
151
        if ($this->variadic()) {
152
            return (array) $this->default;
153
        }
154
155
        return $this->default;
156
    }
157
158
    /**
159
     * Run the filter/sanitizer/validato callback for this prop.
160
     *
161
     * @param mixed $raw
162
     *
163
     * @return mixed
164
     */
165
    public function filter($raw)
166
    {
167
        if ($this->filter) {
168
            $callback = $this->filter;
169
170
            return $callback($raw);
171
        }
172
173
        return $raw;
174
    }
175
}
176