|
1
|
|
|
<?php namespace nyx\console\input\parameter\definitions; |
|
2
|
|
|
|
|
3
|
|
|
// External dependencies |
|
4
|
|
|
use nyx\core; |
|
5
|
|
|
|
|
6
|
|
|
// Internal dependencies |
|
7
|
|
|
use nyx\console\input; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Input Option Definitions |
|
11
|
|
|
* |
|
12
|
|
|
* @version 0.1.0 |
|
13
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
14
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
15
|
|
|
* @link https://github.com/unyx/nyx |
|
16
|
|
|
*/ |
|
17
|
|
|
class Options extends input\parameter\Definitions |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array A map of Option shortcuts to their actual names. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $shortcuts = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($options = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setCollectedType(input\Option::class); |
|
30
|
|
|
|
|
31
|
|
|
parent::__construct($options); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function replace($items) : core\collections\interfaces\Collection |
|
38
|
|
|
{ |
|
39
|
|
|
$this->items = []; |
|
40
|
|
|
$this->shortcuts = []; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($this->extractItems($items) as $item) { |
|
43
|
|
|
|
|
44
|
|
|
// Allow for defining Option instances without explicitly instantiating them |
|
45
|
|
|
// when creating Input Definitions. |
|
46
|
|
|
if (is_array($item)) { |
|
47
|
|
|
$item = new input\Option(...$item); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->add($item); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function add(core\interfaces\Named $option) : core\collections\interfaces\NamedObjectSet |
|
60
|
|
|
{ |
|
61
|
|
|
parent::add($option); |
|
62
|
|
|
|
|
63
|
|
|
// If the Option has a shortcut... |
|
64
|
|
|
/* @var input\Option $option */ |
|
65
|
|
|
if ($shortcut = $option->getShortcut()) { |
|
66
|
|
|
$this->shortcuts[$shortcut] = $option->getName(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the map of Option shortcuts to their actual names. |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getShortcuts() : array |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->shortcuts; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the Option matching a given shortcut name. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $shortcut The name of the shortcut. |
|
86
|
|
|
* @return input\Option |
|
87
|
|
|
* @throws \OutOfBoundsException When the given shortcut is not defined. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getByShortcut(string $shortcut) : input\Option |
|
90
|
|
|
{ |
|
91
|
|
|
if (!isset($this->shortcuts[$shortcut])) { |
|
92
|
|
|
throw new \OutOfBoundsException("The short option [$shortcut] is not defined."); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->get($this->shortcuts[$shortcut]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: