arrayparser.php 5.3 KB

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