arrayparser.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC;
  26. /**
  27. * Class ArrayParser
  28. *
  29. * @package OC
  30. */
  31. class ArrayParser {
  32. const TYPE_NUM = 1;
  33. const TYPE_BOOL = 2;
  34. const TYPE_STRING = 3;
  35. const TYPE_ARRAY = 4;
  36. /**
  37. * @param string $string
  38. * @return array|bool|int|null|string
  39. */
  40. public function parsePHP($string) {
  41. $string = $this->stripPHPTags($string);
  42. $string = $this->stripAssignAndReturn($string);
  43. return $this->parse($string);
  44. }
  45. /**
  46. * @param string $string
  47. * @return string
  48. */
  49. private function stripPHPTags($string) {
  50. $string = trim($string);
  51. if (substr($string, 0, 5) === '<?php') {
  52. $string = substr($string, 5);
  53. }
  54. if (substr($string, -2) === '?>') {
  55. $string = substr($string, 0, -2);
  56. }
  57. return $string;
  58. }
  59. /**
  60. * @param string $string
  61. * @return string
  62. */
  63. private function stripAssignAndReturn($string) {
  64. $string = trim($string);
  65. if (substr($string, 0, 6) === 'return') {
  66. $string = substr($string, 6);
  67. }
  68. if (substr($string, 0, 1) === '$') {
  69. list(, $string) = explode('=', $string, 2);
  70. }
  71. return $string;
  72. }
  73. /**
  74. * @param string $string
  75. * @return array|bool|int|null|string
  76. */
  77. private function parse($string) {
  78. $string = trim($string);
  79. $string = trim($string, ';');
  80. switch ($this->getType($string)) {
  81. case self::TYPE_NUM:
  82. return $this->parseNum($string);
  83. case self::TYPE_BOOL:
  84. return $this->parseBool($string);
  85. case self::TYPE_STRING:
  86. return $this->parseString($string);
  87. case self::TYPE_ARRAY:
  88. return $this->parseArray($string);
  89. }
  90. return null;
  91. }
  92. /**
  93. * @param string $string
  94. * @return int
  95. */
  96. private function getType($string) {
  97. $string = strtolower($string);
  98. $first = substr($string, 0, 1);
  99. $last = substr($string, -1, 1);
  100. $arrayFirst = substr($string, 0, 5);
  101. if (($first === '"' or $first === "'") and ($last === '"' or $last === "'")) {
  102. return self::TYPE_STRING;
  103. } elseif ($string === 'false' or $string === 'true') {
  104. return self::TYPE_BOOL;
  105. } elseif ($arrayFirst === 'array' and $last === ')') {
  106. return self::TYPE_ARRAY;
  107. } else {
  108. return self::TYPE_NUM;
  109. }
  110. }
  111. /**
  112. * @param string $string
  113. * @return string
  114. */
  115. private function parseString($string) {
  116. return substr($string, 1, -1);
  117. }
  118. /**
  119. * @param string $string
  120. * @return int
  121. */
  122. private function parseNum($string) {
  123. return intval($string);
  124. }
  125. /**
  126. * @param string $string
  127. * @return bool
  128. */
  129. private function parseBool($string) {
  130. $string = strtolower($string);
  131. return $string === 'true';
  132. }
  133. /**
  134. * @param string $string
  135. * @return array
  136. */
  137. private function parseArray($string) {
  138. $body = substr($string, 5);
  139. $body = trim($body);
  140. $body = substr($body, 1, -1);
  141. $items = $this->splitArray($body);
  142. $result = [];
  143. $lastKey = -1;
  144. foreach ($items as $item) {
  145. $item = trim($item);
  146. if ($item) {
  147. if (strpos($item, '=>')) {
  148. list($key, $value) = explode('=>', $item, 2);
  149. $key = $this->parse($key);
  150. $value = $this->parse($value);
  151. } else {
  152. $key = ++$lastKey;
  153. $value = $item;
  154. }
  155. if (is_numeric($key)) {
  156. $lastKey = $key;
  157. }
  158. $result[$key] = $value;
  159. }
  160. }
  161. return $result;
  162. }
  163. /**
  164. * @param string $body
  165. * @return array
  166. * @throws \UnexpectedValueException
  167. */
  168. private function splitArray($body) {
  169. $inSingleQuote = false; //keep track if we are inside quotes
  170. $inDoubleQuote = false;
  171. $bracketDepth = 0; //keep track if we are inside brackets
  172. $parts = [];
  173. $start = 0;
  174. $escaped = false; //keep track if we are after an escape character
  175. $skips = []; //keep track of the escape characters we need to remove from the result
  176. if (substr($body, -1, 1) !== ',') {
  177. $body .= ',';
  178. }
  179. $bodyLength = strlen($body);
  180. for ($i = 0; $i < $bodyLength; $i++) {
  181. $char = substr($body, $i, 1);
  182. if ($char === '\\') {
  183. if ($escaped) {
  184. array_unshift($skips, $i - 1);
  185. }
  186. $escaped = !$escaped;
  187. } else {
  188. if ($char === '"' and !$inSingleQuote) {
  189. if ($escaped) {
  190. array_unshift($skips, $i - 1);
  191. } else {
  192. $inDoubleQuote = !$inDoubleQuote;
  193. }
  194. } elseif ($char === "'" and !$inDoubleQuote) {
  195. if ($escaped) {
  196. array_unshift($skips, $i - 1);
  197. } else {
  198. $inSingleQuote = !$inSingleQuote;
  199. }
  200. } elseif (!$inDoubleQuote and !$inSingleQuote) {
  201. if ($char === '(') {
  202. $bracketDepth++;
  203. } elseif ($char === ')') {
  204. if ($bracketDepth <= 0) {
  205. throw new \UnexpectedValueException();
  206. } else {
  207. $bracketDepth--;
  208. }
  209. } elseif ($bracketDepth === 0 and $char === ',') {
  210. $part = substr($body, $start, $i - $start);
  211. foreach ($skips as $skip) {
  212. $part = substr($part, 0, $skip - $start) . substr($part, $skip - $start + 1);
  213. }
  214. $parts[] = $part;
  215. $start = $i + 1;
  216. $skips = [];
  217. }
  218. }
  219. $escaped = false;
  220. }
  221. }
  222. return $parts;
  223. }
  224. }