|
1
|
|
|
<?php namespace nyx\console; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Input |
|
5
|
|
|
* |
|
6
|
|
|
* Base class for concrete Input formats. |
|
7
|
|
|
* |
|
8
|
|
|
* @version 0.1.0 |
|
9
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
10
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
11
|
|
|
* @link https://github.com/unyx/nyx |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class Input implements interfaces\Input |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var interfaces\input\Tokens The raw, unmapped and not validated input Tokens. |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $raw; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var input\parameter\values\Arguments The Input Arguments collection. |
|
22
|
|
|
*/ |
|
23
|
|
|
private $arguments; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var input\parameter\values\Options The Input Options collection. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $options; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Parses the raw Tokens into usable Input Arguments and Options. |
|
32
|
|
|
* |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract protected function parse() : Input; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function bind(input\Definition $definition) : interfaces\Input |
|
41
|
|
|
{ |
|
42
|
|
|
$this->arguments = new input\parameter\values\Arguments($definition->arguments()); |
|
43
|
|
|
$this->options = new input\parameter\values\Options($definition->options()); |
|
44
|
|
|
|
|
45
|
|
|
$this->parse(); |
|
46
|
|
|
|
|
47
|
|
|
$this->arguments->finalize(); |
|
48
|
|
|
$this->options->finalize(); |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function raw() : interfaces\input\Tokens |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->raw; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function arguments() : ?input\parameter\values\Arguments |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->arguments; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function options() : ?input\parameter\values\Options |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->options; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Magic getter. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $property The name of the property whose value is being requested. |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
* @throws \DomainException When the requested property does not exist or is not accessible. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __get(string $property) |
|
85
|
|
|
{ |
|
86
|
|
|
// Limit magic access to a predefined list. Request access through the actual getters. |
|
87
|
|
|
// This adds some overhead, but hurr durr console hurr durr magic method, anyways. |
|
88
|
|
|
if (in_array($property, ['raw', 'arguments', 'options'])) { |
|
89
|
|
|
return $this->$property(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
throw new \DomainException("The property [$property] does not exist or is not accessible."); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Magic isset. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $property The name of the property whose existence we are checking. |
|
99
|
|
|
* @return bool True when the property is set, false otherwise and when it is not accessible. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function __isset(string $property) : bool |
|
102
|
|
|
{ |
|
103
|
|
|
// Limit magic access to a predefined list. |
|
104
|
|
|
if (in_array($property, ['raw', 'arguments', 'options'])) { |
|
105
|
|
|
return isset($this->$property); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|