1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Cli |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Cli; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Responsible for accessing I/O streams that allow access to PHP's own input and output streams. |
13
|
|
|
* |
14
|
|
|
* @package Cli |
15
|
|
|
* @author Gjero Krsteski <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Std |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var resource |
21
|
|
|
*/ |
22
|
|
|
private $handle; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $stream |
26
|
|
|
*/ |
27
|
|
|
public function __construct($stream = 'php://stdin') |
28
|
|
|
{ |
29
|
|
|
$this->handle = fopen($stream, 'r'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function value() |
36
|
|
|
{ |
37
|
|
|
return substr(fgets($this->handle, 1024), 0, -1); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __destruct() |
41
|
|
|
{ |
42
|
|
|
fclose($this->handle); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Allow direct access to the corresponding input stream of the PHP process. |
47
|
|
|
* |
48
|
|
|
* @param string $prompt |
49
|
|
|
* @param string $validation A regex pattern |
50
|
|
|
* |
51
|
|
|
* <code> |
52
|
|
|
* |
53
|
|
|
* Have a look at the examples for $validation: |
54
|
|
|
* |
55
|
|
|
* Regular Expression | Will match... |
56
|
|
|
* ------------------------------------------------------------- |
57
|
|
|
* .* | Not empty |
58
|
|
|
* foo | The string "foo" |
59
|
|
|
* ^foo | "foo" at the start of a string |
60
|
|
|
* foo$ | "foo" at the end of a string |
61
|
|
|
* ^foo$ | "foo" when it is alone on a string |
62
|
|
|
* [abc] | a, b, or c |
63
|
|
|
* [a-z] | Any lowercase letter |
64
|
|
|
* [^A-Z] | Any character that is not a uppercase letter |
65
|
|
|
* (gif|jpg) | Matches either "gif" or "jpeg" |
66
|
|
|
* [a-z]+ | One or more lowercase letters |
67
|
|
|
* [0-9\.\-] | Аny number, dot, or minus sign |
68
|
|
|
* |
69
|
|
|
* </code> |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function read($prompt, $validation = "/.*/") |
74
|
|
|
{ |
75
|
|
|
$value = ''; |
76
|
|
|
|
77
|
|
|
while (true) { |
78
|
|
|
|
79
|
|
|
$this->view("Please enter a " . $prompt . ":"); |
80
|
|
|
|
81
|
|
|
$value = $this->value(); |
82
|
|
|
|
83
|
|
|
if ($this->valid($validation, $value)) { |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->view("[ Value format for " . $prompt . " is invalid! ]"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $validation A regex pattern |
95
|
|
|
* @param string $value |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function valid($validation, $value) |
100
|
|
|
{ |
101
|
|
|
return strlen($value) > 0 && preg_match($validation, $value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $prompt |
106
|
|
|
*/ |
107
|
|
|
public function view($prompt) |
108
|
|
|
{ |
109
|
|
|
echo $prompt . "\n"; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|