Permalink
Browse files

Add ArgvParser, StringIterator

Shamelessly stolen from http://stackoverflow.com/a/18229461
  • Loading branch information...
stringerbell committed May 29, 2016
1 parent bcea1da commit 2ebf40ba2a853c4d9472059e7b5eeda67c2ab4b7
Showing with 129 additions and 0 deletions.
  1. +89 −0 src/App/Utility/ArgvParser.php
  2. +40 −0 src/App/Utility/StringIterator.php
@@ -0,0 +1,89 @@
<?php
namespace App\Utility;
// stolen from http://stackoverflow.com/a/18229461
class ArgvParser extends StringIterator
{
const TOKEN_DOUBLE_QUOTE = '"';
const TOKEN_SINGLE_QUOTE = "'";
const TOKEN_SPACE = ' ';
const TOKEN_ESCAPE = '\\';
public function parse()
{
$this->rewind();
$args = [];
while ($this->valid()) {
switch ($this->current()) {
case self::TOKEN_DOUBLE_QUOTE:
case self::TOKEN_SINGLE_QUOTE:
$args[] = $this->QUOTED($this->current());
break;
case self::TOKEN_SPACE:
$this->next();
break;
default:
$args[] = $this->UNQUOTED();
}
}
return $args;
}
private function QUOTED($enclosure)
{
$this->next();
$result = '';
while ($this->valid()) {
if ($this->current() == self::TOKEN_ESCAPE) {
$this->next();
if ($this->valid() && $this->current() == $enclosure) {
$result .= $enclosure;
} elseif ($this->valid()) {
$result .= self::TOKEN_ESCAPE;
if ($this->current() != self::TOKEN_ESCAPE) {
$result .= $this->current();
}
}
} elseif ($this->current() == $enclosure) {
$this->next();
break;
} else {
$result .= $this->current();
}
$this->next();
}
return $result;
}
private function UNQUOTED()
{
$result = '';
while ($this->valid()) {
if ($this->current() == self::TOKEN_SPACE) {
$this->next();
break;
} else {
$result .= $this->current();
}
$this->next();
}
return $result;
}
public static function parseString($input)
{
$parser = new self($input);
return $parser->parse();
}
}
@@ -0,0 +1,40 @@
<?php
namespace App\Utility;
class StringIterator implements \Iterator
{
private $string;
private $current;
public function __construct($string)
{
$this->string = $string;
}
public function current()
{
return $this->string[$this->current];
}
public function next()
{
++$this->current;
}
public function key()
{
return $this->current;
}
public function valid()
{
return $this->current < strlen($this->string);
}
public function rewind()
{
$this->current = 0;
}
}

0 comments on commit 2ebf40b

Please sign in to comment.