Created
May 11, 2014 20:16
-
-
Save mageekguy/82c64af991e3b9da8149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace jobs\script\configuration\cli; | |
use | |
jobs\world | |
; | |
class parser | |
{ | |
private $callable = null; | |
function __construct(callable $callable) | |
{ | |
$this->callable = $callable; | |
} | |
function parse(array $values) | |
{ | |
$option = null; | |
$arguments = []; | |
foreach ($values as $value) | |
{ | |
if (static::isOption($value) === false) | |
{ | |
$arguments[] = $value; | |
} | |
else | |
{ | |
if ($option !== null) | |
{ | |
$this->callCallable($option, $arguments); | |
} | |
$option = $value; | |
$arguments = []; | |
} | |
} | |
if ($option !== null) | |
{ | |
$this->callCallable($option, $arguments); | |
} | |
return $this; | |
} | |
private function callCallable($option, $arguments) | |
{ | |
call_user_func_array($this->callable, array($option, $arguments)); | |
return $this; | |
} | |
private static function isOption($value) | |
{ | |
return preg_match('/^(\+{1,}|-{1,})[a-z][-_a-z0-9]*/i', $value) === 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment