Use proper error type and clean-up code
This commit is contained in:
parent
d5e884d629
commit
4eb118dd33
|
@ -21,6 +21,11 @@
|
||||||
|
|
||||||
namespace OC;
|
namespace OC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ArrayParser
|
||||||
|
*
|
||||||
|
* @package OC
|
||||||
|
*/
|
||||||
class ArrayParser {
|
class ArrayParser {
|
||||||
const TYPE_NUM = 1;
|
const TYPE_NUM = 1;
|
||||||
const TYPE_BOOL = 2;
|
const TYPE_BOOL = 2;
|
||||||
|
@ -31,7 +36,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return array|bool|int|null|string
|
* @return array|bool|int|null|string
|
||||||
*/
|
*/
|
||||||
function parsePHP($string) {
|
public function parsePHP($string) {
|
||||||
$string = $this->stripPHPTags($string);
|
$string = $this->stripPHPTags($string);
|
||||||
$string = $this->stripAssignAndReturn($string);
|
$string = $this->stripAssignAndReturn($string);
|
||||||
return $this->parse($string);
|
return $this->parse($string);
|
||||||
|
@ -41,7 +46,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function stripPHPTags($string) {
|
private function stripPHPTags($string) {
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if (substr($string, 0, 5) === '<?php') {
|
if (substr($string, 0, 5) === '<?php') {
|
||||||
$string = substr($string, 5);
|
$string = substr($string, 5);
|
||||||
|
@ -56,7 +61,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function stripAssignAndReturn($string) {
|
private function stripAssignAndReturn($string) {
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if (substr($string, 0, 6) === 'return') {
|
if (substr($string, 0, 6) === 'return') {
|
||||||
$string = substr($string, 6);
|
$string = substr($string, 6);
|
||||||
|
@ -71,7 +76,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return array|bool|int|null|string
|
* @return array|bool|int|null|string
|
||||||
*/
|
*/
|
||||||
function parse($string) {
|
private function parse($string) {
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
$string = trim($string, ';');
|
$string = trim($string, ';');
|
||||||
switch ($this->getType($string)) {
|
switch ($this->getType($string)) {
|
||||||
|
@ -91,7 +96,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
function getType($string) {
|
private function getType($string) {
|
||||||
$string = strtolower($string);
|
$string = strtolower($string);
|
||||||
$first = substr($string, 0, 1);
|
$first = substr($string, 0, 1);
|
||||||
$last = substr($string, -1, 1);
|
$last = substr($string, -1, 1);
|
||||||
|
@ -111,7 +116,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function parseString($string) {
|
private function parseString($string) {
|
||||||
return substr($string, 1, -1);
|
return substr($string, 1, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +124,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
function parseNum($string) {
|
private function parseNum($string) {
|
||||||
return intval($string);
|
return intval($string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +132,7 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function parseBool($string) {
|
private function parseBool($string) {
|
||||||
$string = strtolower($string);
|
$string = strtolower($string);
|
||||||
return $string === 'true';
|
return $string === 'true';
|
||||||
}
|
}
|
||||||
|
@ -136,12 +141,12 @@ class ArrayParser {
|
||||||
* @param string $string
|
* @param string $string
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function parseArray($string) {
|
private function parseArray($string) {
|
||||||
$body = substr($string, 5);
|
$body = substr($string, 5);
|
||||||
$body = trim($body);
|
$body = trim($body);
|
||||||
$body = substr($body, 1, -1);
|
$body = substr($body, 1, -1);
|
||||||
$items = $this->splitArray($body);
|
$items = $this->splitArray($body);
|
||||||
$result = array();
|
$result = [];
|
||||||
$lastKey = -1;
|
$lastKey = -1;
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
$item = trim($item);
|
$item = trim($item);
|
||||||
|
@ -167,15 +172,16 @@ class ArrayParser {
|
||||||
/**
|
/**
|
||||||
* @param string $body
|
* @param string $body
|
||||||
* @return array
|
* @return array
|
||||||
|
* @throws \UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
function splitArray($body) {
|
private function splitArray($body) {
|
||||||
$inSingleQuote = false;//keep track if we are inside quotes
|
$inSingleQuote = false; //keep track if we are inside quotes
|
||||||
$inDoubleQuote = false;
|
$inDoubleQuote = false;
|
||||||
$bracketDepth = 0;//keep track if we are inside brackets
|
$bracketDepth = 0; //keep track if we are inside brackets
|
||||||
$parts = array();
|
$parts = [];
|
||||||
$start = 0;
|
$start = 0;
|
||||||
$escaped = false;//keep track if we are after an escape character
|
$escaped = false; //keep track if we are after an escape character
|
||||||
$skips = array();//keep track of the escape characters we need to remove from the result
|
$skips = []; //keep track of the escape characters we need to remove from the result
|
||||||
if (substr($body, -1, 1) !== ',') {
|
if (substr($body, -1, 1) !== ',') {
|
||||||
$body .= ',';
|
$body .= ',';
|
||||||
}
|
}
|
||||||
|
@ -206,7 +212,7 @@ class ArrayParser {
|
||||||
$bracketDepth++;
|
$bracketDepth++;
|
||||||
} elseif ($char === ')') {
|
} elseif ($char === ')') {
|
||||||
if ($bracketDepth <= 0) {
|
if ($bracketDepth <= 0) {
|
||||||
throw new UnexpectedValueException();
|
throw new \UnexpectedValueException();
|
||||||
} else {
|
} else {
|
||||||
$bracketDepth--;
|
$bracketDepth--;
|
||||||
}
|
}
|
||||||
|
@ -217,7 +223,7 @@ class ArrayParser {
|
||||||
}
|
}
|
||||||
$parts[] = $part;
|
$parts[] = $part;
|
||||||
$start = $i + 1;
|
$start = $i + 1;
|
||||||
$skips = array();
|
$skips = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$escaped = false;
|
$escaped = false;
|
||||||
|
|
Loading…
Reference in New Issue