arrayparser.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @author Robin Appelman
  4. * @copyright 2013 Robin Appelman icewind@owncloud.com
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public
  17. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. namespace OC;
  21. class SyntaxException extends \Exception {
  22. }
  23. class ArrayParser {
  24. const TYPE_NUM = 1;
  25. const TYPE_BOOL = 2;
  26. const TYPE_STRING = 3;
  27. const TYPE_ARRAY = 4;
  28. function parsePHP($string) {
  29. $string = $this->stripPHPTags($string);
  30. $string = $this->stripAssignAndReturn($string);
  31. return $this->parse($string);
  32. }
  33. function stripPHPTags($string) {
  34. $string = trim($string);
  35. if (substr($string, 0, 5) === '<?php') {
  36. $string = substr($string, 5);
  37. }
  38. if (substr($string, -2) === '?>') {
  39. $string = substr($string, 0, -2);
  40. }
  41. return $string;
  42. }
  43. function stripAssignAndReturn($string) {
  44. $string = trim($string);
  45. if (substr($string, 0, 6) === 'return') {
  46. $string = substr($string, 6);
  47. }
  48. if (substr($string, 0, 1) === '$') {
  49. list(, $string) = explode('=', $string, 2);
  50. }
  51. return $string;
  52. }
  53. function parse($string) {
  54. $string = trim($string);
  55. $string = trim($string, ';');
  56. switch ($this->getType($string)) {
  57. case self::TYPE_NUM:
  58. return $this->parseNum($string);
  59. case self::TYPE_BOOL:
  60. return $this->parseBool($string);
  61. case self::TYPE_STRING:
  62. return $this->parseString($string);
  63. case self::TYPE_ARRAY:
  64. return $this->parseArray($string);
  65. }
  66. return null;
  67. }
  68. function getType($string) {
  69. $string = strtolower($string);
  70. $first = substr($string, 0, 1);
  71. $last = substr($string, -1, 1);
  72. $arrayFirst = substr($string, 0, 5);
  73. if (($first === '"' or $first === "'") and ($last === '"' or $last === "'")) {
  74. return self::TYPE_STRING;
  75. } elseif ($string === 'false' or $string === 'true') {
  76. return self::TYPE_BOOL;
  77. } elseif ($arrayFirst === 'array' and $last === ')') {
  78. return self::TYPE_ARRAY;
  79. } else {
  80. return self::TYPE_NUM;
  81. }
  82. }
  83. function parseString($string) {
  84. return substr($string, 1, -1);
  85. }
  86. function parseNum($string) {
  87. return intval($string);
  88. }
  89. function parseBool($string) {
  90. $string = strtolower($string);
  91. return $string === 'true';
  92. }
  93. function parseArray($string) {
  94. $body = substr($string, 5);
  95. $body = trim($body);
  96. $body = substr($body, 1, -1);
  97. $items = $this->splitArray($body);
  98. $result = array();
  99. $lastKey = -1;
  100. foreach ($items as $item) {
  101. $item = trim($item);
  102. if ($item) {
  103. if (strpos($item, '=>')) {
  104. list($key, $value) = explode('=>', $item, 2);
  105. $key = $this->parse($key);
  106. $value = $this->parse($value);
  107. } else {
  108. $key = ++$lastKey;
  109. $value = $item;
  110. }
  111. if (is_numeric($key)) {
  112. $lastKey = $key;
  113. }
  114. $result[$key] = $value;
  115. }
  116. }
  117. return $result;
  118. }
  119. function splitArray($body) {
  120. $inSingleQuote = false;//keep track if we are inside quotes
  121. $inDoubleQuote = false;
  122. $bracketDepth = 0;//keep track if we are inside brackets
  123. $parts = array();
  124. $start = 0;
  125. $escaped = false;//keep track if we are after an escape character
  126. $skips = array();//keep track of the escape characters we need to remove from the result
  127. if (substr($body, -1, 1) !== ',') {
  128. $body .= ',';
  129. }
  130. for ($i = 0; $i < strlen($body); $i++) {
  131. $char = substr($body, $i, 1);
  132. if ($char === '\\') {
  133. if ($escaped) {
  134. array_unshift($skips, $i - 1);
  135. }
  136. $escaped = !$escaped;
  137. } else {
  138. if ($char === '"' and !$inSingleQuote) {
  139. if ($escaped) {
  140. array_unshift($skips, $i - 1);
  141. } else {
  142. $inDoubleQuote = !$inDoubleQuote;
  143. }
  144. } elseif ($char === "'" and !$inDoubleQuote) {
  145. if ($escaped) {
  146. array_unshift($skips, $i - 1);
  147. } else {
  148. $inSingleQuote = !$inSingleQuote;
  149. }
  150. } elseif (!$inDoubleQuote and !$inSingleQuote) {
  151. if ($char === '(') {
  152. $bracketDepth++;
  153. } elseif ($char === ')') {
  154. if ($bracketDepth <= 0) {
  155. throw new SyntaxException;
  156. } else {
  157. $bracketDepth--;
  158. }
  159. } elseif ($bracketDepth === 0 and $char === ',') {
  160. $part = substr($body, $start, $i - $start);
  161. foreach ($skips as $skip) {
  162. $part = substr($part, 0, $skip - $start) . substr($part, $skip - $start + 1);
  163. }
  164. $parts[] = $part;
  165. $start = $i + 1;
  166. $skips = array();
  167. }
  168. }
  169. $escaped = false;
  170. }
  171. }
  172. return $parts;
  173. }
  174. }