1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Youtubedl; |
4
|
|
|
use Exception; |
5
|
|
|
use Youtubedl\Option\IPv4; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @method Option setOutput(string $output) |
9
|
|
|
* @method Option getListExtractors() |
10
|
|
|
* @method Option getExtractorDescriptions() |
11
|
|
|
* @method Option setUserAgent(string $userAgent) |
12
|
|
|
* @method Option dumpUserAgent() |
13
|
|
|
*/ |
14
|
|
|
class Option |
15
|
|
|
{ |
16
|
|
|
protected $options = []; |
17
|
|
|
|
18
|
|
|
public function __call($method, $args) |
19
|
|
|
{ |
20
|
|
|
$cleanMethod = lcfirst(preg_replace('/get|set/', null, $method)); |
21
|
|
|
if (preg_match_all('/[A-Z]/', $cleanMethod, $uppers)) { |
22
|
|
|
if (!is_array($uppers)) { |
23
|
|
|
throw new \Exception('$uppers must be an array'); |
24
|
|
|
} |
25
|
|
|
foreach (current($uppers) as $key => $upper) { |
26
|
|
|
$cleanMethod = str_replace($upper, '-'.strtolower($upper), $cleanMethod); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
$this->options[$cleanMethod] = current($args) ? '"'.current($args).'"' : null; |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function __toString() |
35
|
|
|
{ |
36
|
|
|
return $this->formatAll(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
public function setAsIPv4() |
43
|
|
|
{ |
44
|
|
|
$this->options[] = (new IPv4()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws Exception |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function format() |
52
|
|
|
{ |
53
|
|
|
throw new Exception('Must be implemented by the child classes'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
* @throws Exception |
59
|
|
|
*/ |
60
|
|
|
private function formatAll() |
61
|
|
|
{ |
62
|
|
|
$output = ''; |
63
|
|
|
foreach ($this->options as $key => $option) { |
64
|
|
|
if ($option instanceof Option) { |
65
|
|
|
$output .= $option->format(); |
66
|
|
|
} else { |
67
|
|
|
$output .= "--{$key} {$option} "; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $output; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|