You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arrayparser.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /**
  29. * @param string $string
  30. * @return array|bool|int|null|string
  31. */
  32. function parsePHP($string) {
  33. $string = $this->stripPHPTags($string);
  34. $string = $this->stripAssignAndReturn($string);
  35. return $this->parse($string);
  36. }
  37. /**
  38. * @param string $string
  39. * @return string
  40. */
  41. function stripPHPTags($string) {
  42. $string = trim($string);
  43. if (substr($string, 0, 5) === '<?php') {
  44. $string = substr($string, 5);
  45. }
  46. if (substr($string, -2) === '?>') {
  47. $string = substr($string, 0, -2);
  48. }
  49. return $string;
  50. }
  51. /**
  52. * @param string $string
  53. * @return string
  54. */
  55. function stripAssignAndReturn($string) {
  56. $string = trim($string);
  57. if (substr($string, 0, 6) === 'return') {
  58. $string = substr($string, 6);
  59. }
  60. if (substr($string, 0, 1) === '$') {
  61. list(, $string) = explode('=', $string, 2);
  62. }
  63. return $string;
  64. }
  65. /**
  66. * @param string $string
  67. * @return array|bool|int|null|string
  68. */
  69. function parse($string) {
  70. $string = trim($string);
  71. $string = trim($string, ';');
  72. switch ($this->getType($string)) {
  73. case self::TYPE_NUM:
  74. return $this->parseNum($string);
  75. case self::TYPE_BOOL:
  76. return $this->parseBool($string);
  77. case self::TYPE_STRING:
  78. return $this->parseString($string);
  79. case self::TYPE_ARRAY:
  80. return $this->parseArray($string);
  81. }
  82. return null;
  83. }
  84. /**
  85. * @param string $string
  86. * @return int
  87. */
  88. function getType($string) {
  89. $string = strtolower($string);
  90. $first = substr($string, 0, 1);
  91. $last = substr($string, -1, 1);
  92. $arrayFirst = substr($string, 0, 5);
  93. if (($first === '"' or $first === "'") and ($last === '"' or $last === "'")) {
  94. return self::TYPE_STRING;
  95. } elseif ($string === 'false' or $string === 'true') {
  96. return self::TYPE_BOOL;
  97. } elseif ($arrayFirst === 'array' and $last === ')') {
  98. return self::TYPE_ARRAY;
  99. } else {
  100. return self::TYPE_NUM;
  101. }
  102. }
  103. /**
  104. * @param string $string
  105. * @return string
  106. */
  107. function parseString($string) {
  108. return substr($string, 1, -1);
  109. }
  110. /**
  111. * @param string $string
  112. * @return int
  113. */
  114. function parseNum($string) {
  115. return intval($string);
  116. }
  117. /**
  118. * @param string $string
  119. * @return bool
  120. */
  121. function parseBool($string) {
  122. $string = strtolower($string);
  123. return $string === 'true';
  124. }
  125. /**
  126. * @param string $string
  127. * @return array
  128. */
  129. function parseArray($string) {
  130. $body = substr($string, 5);
  131. $body = trim($body);
  132. $body = substr($body, 1, -1);
  133. $items = $this->splitArray($body);
  134. $result = array();
  135. $lastKey = -1;
  136. foreach ($items as $item) {
  137. $item = trim($item);
  138. if ($item) {
  139. if (strpos($item, '=>')) {
  140. list($key, $value) = explode('=>', $item, 2);
  141. $key = $this->parse($key);
  142. $value = $this->parse($value);
  143. } else {
  144. $key = ++$lastKey;
  145. $value = $item;
  146. }
  147. if (is_numeric($key)) {
  148. $lastKey = $key;
  149. }
  150. $result[$key] = $value;
  151. }
  152. }
  153. return $result;
  154. }
  155. /**
  156. * @param string $body
  157. * @return array
  158. */
  159. function splitArray($body) {
  160. $inSingleQuote = false;//keep track if we are inside quotes
  161. $inDoubleQuote = false;
  162. $bracketDepth = 0;//keep track if we are inside brackets
  163. $parts = array();
  164. $start = 0;
  165. $escaped = false;//keep track if we are after an escape character
  166. $skips = array();//keep track of the escape characters we need to remove from the result
  167. if (substr($body, -1, 1) !== ',') {
  168. $body .= ',';
  169. }
  170. for ($i = 0; $i < strlen($body); $i++) {
  171. $char = substr($body, $i, 1);
  172. if ($char === '\\') {
  173. if ($escaped) {
  174. array_unshift($skips, $i - 1);
  175. }
  176. $escaped = !$escaped;
  177. } else {
  178. if ($char === '"' and !$inSingleQuote) {
  179. if ($escaped) {
  180. array_unshift($skips, $i - 1);
  181. } else {
  182. $inDoubleQuote = !$inDoubleQuote;
  183. }
  184. } elseif ($char === "'" and !$inDoubleQuote) {
  185. if ($escaped) {
  186. array_unshift($skips, $i - 1);
  187. } else {
  188. $inSingleQuote = !$inSingleQuote;
  189. }
  190. } elseif (!$inDoubleQuote and !$inSingleQuote) {
  191. if ($char === '(') {
  192. $bracketDepth++;
  193. } elseif ($char === ')') {
  194. if ($bracketDepth <= 0) {
  195. throw new SyntaxException;
  196. } else {
  197. $bracketDepth--;
  198. }
  199. } elseif ($bracketDepth === 0 and $char === ',') {
  200. $part = substr($body, $start, $i - $start);
  201. foreach ($skips as $skip) {
  202. $part = substr($part, 0, $skip - $start) . substr($part, $skip - $start + 1);
  203. }
  204. $parts[] = $part;
  205. $start = $i + 1;
  206. $skips = array();
  207. }
  208. }
  209. $escaped = false;
  210. }
  211. }
  212. return $parts;
  213. }
  214. }