arrayparser.php 5.5 KB

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