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 input\Option[] A map of Option names to their Definitions. |
21
|
|
|
*/ |
22
|
|
|
protected $items = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var input\Option[] A map of Option shortcuts to their Definitions. |
26
|
|
|
*/ |
27
|
|
|
protected $shortcuts = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function __construct($options = null) |
33
|
|
|
{ |
34
|
|
|
$this->setCollectedType(input\Option::class); |
35
|
|
|
|
36
|
|
|
parent::__construct($options); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function replace($items) : core\collections\interfaces\Collection |
43
|
|
|
{ |
44
|
|
|
$this->items = []; |
45
|
|
|
$this->shortcuts = []; |
46
|
|
|
|
47
|
|
|
foreach ($this->extractItems($items) as $item) { |
48
|
|
|
$this->add(is_array($item) ? $this->unpack($item) : $item); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function add(core\interfaces\Named $option) : core\collections\interfaces\NamedObjectSet |
58
|
|
|
{ |
59
|
|
|
parent::add($option); |
60
|
|
|
|
61
|
|
|
// If the Option has a shortcut... |
62
|
|
|
/* @var input\Option $option */ |
63
|
|
|
if ($shortcut = $option->getShortcut()) { |
64
|
|
|
if (isset($this->shortcuts[$shortcut])) { |
65
|
|
|
throw new core\collections\exceptions\KeyAlreadyExists($this, $shortcut, $option, "An option with this shortcut [$shortcut] has already been defined."); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->shortcuts[$shortcut] = $option; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function remove(string $name): core\collections\interfaces\NamedObjectSet |
78
|
|
|
{ |
79
|
|
|
if (!isset($this->items[$name])) { |
80
|
|
|
return $this; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($shortcut = $this->items[$name]->getShortcut()) { |
84
|
|
|
unset($this->shortcuts[$shortcut]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return parent::remove($name); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the map of Option shortcuts to their Definitions. |
92
|
|
|
* |
93
|
|
|
* @return input\Option[] |
94
|
|
|
*/ |
95
|
|
|
public function getShortcuts() : array |
96
|
|
|
{ |
97
|
|
|
return $this->shortcuts; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the Option matching a given shortcut name. |
102
|
|
|
* |
103
|
|
|
* @param string $shortcut The name of the shortcut. |
104
|
|
|
* @return input\Option The Option matching the shortcut name. |
105
|
|
|
* @throws core\collections\exceptions\KeyNotExists When the given shortcut is not defined. |
106
|
|
|
*/ |
107
|
|
|
public function getByShortcut(string $shortcut) : input\Option |
108
|
|
|
{ |
109
|
|
|
if (!isset($this->shortcuts[$shortcut])) { |
110
|
|
|
throw new core\collections\exceptions\KeyNotExists($this, $shortcut, "The short option [$shortcut] is not defined."); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->shortcuts[$shortcut]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Unpacks a sequence of Option constructor arguments into an Option instance. |
118
|
|
|
* |
119
|
|
|
* @see \nyx\console\input\Option::__construct() |
120
|
|
|
* |
121
|
|
|
* @param array $definition The arguments to unpack. The order must match the constructor's signature. |
122
|
|
|
* @return input\Option |
123
|
|
|
*/ |
124
|
|
|
protected function unpack(array $definition) : input\Option |
125
|
|
|
{ |
126
|
|
|
// If the 4th argument is an integer, we are going to assume it's one of the input\Value |
127
|
|
|
// class constants defining the mode and attempt to instantiate a input\Value with such. |
128
|
|
|
if (isset($definition[3]) && is_int($definition[3])) { |
129
|
|
|
$definition[3] = new input\Value($definition[3]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return new input\Option(...$definition); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.