|
1
|
|
|
<?php namespace nyx\console\input\sources\tokens; |
|
2
|
|
|
|
|
3
|
|
|
// External dependencies |
|
4
|
|
|
use nyx\core; |
|
5
|
|
|
|
|
6
|
|
|
// Internal dependencies |
|
7
|
|
|
use nyx\console\interfaces; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Argv Input Tokens |
|
11
|
|
|
* |
|
12
|
|
|
* This Tokens collection is able to resolve Input Options by name, as the name is contained in the argv data, |
|
13
|
|
|
* but will not be able to do the same for Input Arguments, as their names are unknown until the input gets |
|
14
|
|
|
* bound to a Definition. |
|
15
|
|
|
* |
|
16
|
|
|
* @version 0.1.0 |
|
17
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
18
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
19
|
|
|
* @link https://github.com/unyx/nyx |
|
20
|
|
|
*/ |
|
21
|
|
|
class Argv extends core\collections\Map implements interfaces\input\Tokens |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function get($parameter, $default = null) |
|
27
|
|
|
{ |
|
28
|
|
|
foreach ($this->items as $token) { |
|
29
|
|
|
|
|
30
|
|
|
if ($token === $parameter || 0 === strpos($token, $parameter . '=')) { |
|
31
|
|
|
if (false !== $pos = strpos($parameter, '=')) { |
|
32
|
|
|
return substr($token, $pos + 1); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $token; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $default; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function has($parameter) : bool |
|
46
|
|
|
{ |
|
47
|
|
|
return in_array($parameter, $this->items); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function remove($parameter) : core\collections\interfaces\Map |
|
55
|
|
|
{ |
|
56
|
|
|
if (false !== $key = array_search($parameter, $this->items)) { |
|
57
|
|
|
unset($this->items[$key]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getArguments() : array |
|
68
|
|
|
{ |
|
69
|
|
|
$arguments = []; |
|
70
|
|
|
|
|
71
|
|
|
foreach ($this->items as $token) { |
|
72
|
|
|
if ($token[0] !== '-') { |
|
73
|
|
|
$arguments[] = $token; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $arguments; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getOptions() : array |
|
84
|
|
|
{ |
|
85
|
|
|
$options = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($this->items as $token) { |
|
88
|
|
|
if ($token[0] === '-' && $token !== '--') { |
|
89
|
|
|
$options[] = $token; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $options; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.