Utility.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. App::uses('Sanitize', 'Utility');
  3. App::uses('Router', 'Routing');
  4. /**
  5. * Main class for all app-wide utility methods
  6. *
  7. * @author Mark Scherer
  8. * @license http://opensource.org/licenses/mit-license.php MIT
  9. */
  10. class Utility {
  11. /**
  12. * Clean implementation of inArray to avoid false positives.
  13. *
  14. * in_array itself has some PHP flaws regarding cross-type comparison:
  15. * - in_array('50x', array(40, 50, 60)) would be true!
  16. * - in_array(50, array('40x', '50x', '60x')) would be true!
  17. *
  18. * @param mixed $needle
  19. * @param array $haystack
  20. * @return bool Success
  21. */
  22. public static function inArray($needle, $haystack) {
  23. $strict = !is_numeric($needle);
  24. return in_array((string)$needle, $haystack, $strict);
  25. }
  26. /**
  27. * Tokenizes a string using $separator.
  28. *
  29. * Options
  30. * - clean: true/false (defaults to true and removes empty tokens and whitespace)
  31. *
  32. * @param string $data The data to tokenize
  33. * @param string $separator The token to split the data on.
  34. * @param array $options
  35. * @return array
  36. */
  37. public static function tokenize($data, $separator = ',', array $options = []) {
  38. $defaults = [
  39. 'clean' => true,
  40. 'callback' => null
  41. ];
  42. $options += $defaults;
  43. if (empty($data)) {
  44. return [];
  45. }
  46. $tokens = explode($separator, $data);
  47. $tokens = array_map('trim', $tokens);
  48. if ($options['callback']) {
  49. foreach ($tokens as $key => $token) {
  50. $tokens[$key] = $options['callback']($token);
  51. }
  52. }
  53. if (empty($tokens) || !$options['clean']) {
  54. return $tokens;
  55. }
  56. foreach ($tokens as $key => $token) {
  57. if ($token === '') {
  58. unset($tokens[$key]);
  59. }
  60. }
  61. return $tokens;
  62. }
  63. /**
  64. * Multibyte analogue of preg_match_all() function. Only that this returns the result.
  65. * By default this works properly with UTF8 strings.
  66. *
  67. * Do not forget to use preg_quote() first on strings that could potentially contain
  68. * unescaped characters.
  69. *
  70. * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
  71. *
  72. * Example: /some(.*)pattern/u
  73. *
  74. * @param string $pattern The pattern to use.
  75. * @param string $subject The string to match.
  76. * @param int $flags
  77. * @param int $offset
  78. * @return array Result
  79. */
  80. public static function pregMatchAll($pattern, $subject, $flags = PREG_SET_ORDER, $offset = null) {
  81. $pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
  82. preg_match_all($pattern, $subject, $matches, $flags, $offset);
  83. return $matches;
  84. }
  85. /**
  86. * Multibyte analogue of preg_match() function. Only that this returns the result.
  87. * By default this works properly with UTF8 strings.
  88. *
  89. * Do not forget to use preg_quote() first on strings that could potentially contain
  90. * unescaped characters.
  91. *
  92. * Note that you still need to add the u modifier (for UTF8) to your pattern yourself.
  93. *
  94. * Example: /some(.*)pattern/u
  95. *
  96. * @param string $pattern The pattern to use.
  97. * @param string $subject The string to match.
  98. * @param int $flags
  99. * @param int $offset
  100. * @return array Result
  101. */
  102. public static function pregMatch($pattern, $subject, $flags = null, $offset = null) {
  103. $pattern = substr($pattern, 0, 1) . '(*UTF8)' . substr($pattern, 1);
  104. preg_match($pattern, $subject, $matches, $flags, $offset);
  105. return $matches;
  106. }
  107. /**
  108. * Multibyte analogue of str_split() function.
  109. * By default this works properly with UTF8 strings.
  110. *
  111. * @param string $text
  112. * @param int $length
  113. * @return array Result
  114. */
  115. public static function strSplit($str, $length = 1) {
  116. if ($length < 1) {
  117. return false;
  118. }
  119. $result = [];
  120. $c = mb_strlen($str);
  121. for ($i = 0; $i < $c; $i += $length) {
  122. $result[] = mb_substr($str, $i, $length);
  123. }
  124. return $result;
  125. }
  126. /**
  127. * Get the current IP address.
  128. *
  129. * @param bool $safe
  130. * @return string IP address
  131. */
  132. public static function getClientIp($safe = true) {
  133. if (!$safe && env('HTTP_X_FORWARDED_FOR')) {
  134. $ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
  135. } elseif (!$safe && env('HTTP_CLIENT_IP')) {
  136. $ipaddr = env('HTTP_CLIENT_IP');
  137. } else {
  138. $ipaddr = env('REMOTE_ADDR');
  139. }
  140. return trim($ipaddr);
  141. }
  142. /**
  143. * Get the current referrer if available.
  144. *
  145. * @param bool $full (defaults to false and leaves the url untouched)
  146. * @return string referer (local or foreign)
  147. */
  148. public static function getReferer($full = false) {
  149. $ref = env('HTTP_REFERER');
  150. $forwarded = env('HTTP_X_FORWARDED_HOST');
  151. if ($forwarded) {
  152. $ref = $forwarded;
  153. }
  154. if (empty($ref)) {
  155. return $ref;
  156. }
  157. if ($full) {
  158. $ref = Router::url($ref, $full);
  159. }
  160. return $ref;
  161. }
  162. /**
  163. * Remove unnessary stuff + add http:// for external urls
  164. * TODO: protocol to lower!
  165. *
  166. * @param string $url
  167. * @return string Cleaned Url
  168. */
  169. public static function cleanUrl($url, $headerRedirect = false) {
  170. if ($url === '' || $url === 'http://' || $url === 'http://www' || $url === 'http://www.') {
  171. $url = '';
  172. } else {
  173. $url = static::autoPrefixUrl($url, 'http://');
  174. }
  175. if ($headerRedirect && !empty($url)) {
  176. $headers = static::getHeaderFromUrl($url);
  177. if ($headers !== false) {
  178. $headerString = implode("\n", $headers);
  179. if ((bool)preg_match('#^HTTP/.*\s+[(301)]+\s#i', $headerString)) {
  180. foreach ($headers as $header) {
  181. if (mb_strpos($header, 'Location:') === 0) {
  182. $url = trim(hDec(mb_substr($header, 9))); // rawurldecode/urldecode ?
  183. }
  184. }
  185. }
  186. }
  187. }
  188. $length = mb_strlen($url);
  189. while (!empty($url) && mb_strrpos($url, '/') === $length - 1) {
  190. $url = mb_substr($url, 0, $length - 1);
  191. $length--;
  192. }
  193. return $url;
  194. }
  195. /**
  196. * A more robust wrapper around for file_exists() which easily
  197. * fails to return true for existent remote files.
  198. * Per default it allows http/https images to be looked up via urlExists()
  199. * for a better result.
  200. *
  201. * @param string $file File
  202. * @return bool Success
  203. */
  204. public static function fileExists($file, $pattern = '~^https?://~i') {
  205. if (!preg_match($pattern, $file)) {
  206. return file_exists($file);
  207. }
  208. return static::urlExists($file);
  209. }
  210. /**
  211. * file_exists() does not always work with URLs.
  212. * So if you check on strpos(http) === 0 you can use this
  213. * to check for URLs instead.
  214. *
  215. * @param string $url Absolute URL
  216. * @return bool Success
  217. */
  218. public static function urlExists($url) {
  219. $headers = @get_headers($url);
  220. if ($headers && preg_match('|\b200\b|', $headers[0])) {
  221. return true;
  222. }
  223. return false;
  224. }
  225. /**
  226. * Utility::getMimeType()
  227. *
  228. * @param string $file File
  229. * @return string Mime type
  230. */
  231. public static function getMimeType($file) {
  232. if (!function_exists('finfo_open')) {
  233. throw new InternalErrorException('finfo_open() required - please enable');
  234. }
  235. // Treat non local files differently
  236. $pattern = '~^https?://~i';
  237. if (preg_match($pattern, $file)) {
  238. $headers = @get_headers($file);
  239. if (!preg_match("|\b200\b|", $headers[0])) {
  240. return '';
  241. }
  242. foreach ($headers as $header) {
  243. if (strpos($header, 'Content-Type:') === 0) {
  244. return trim(substr($header, 13));
  245. }
  246. }
  247. return '';
  248. }
  249. $finfo = finfo_open(FILEINFO_MIME);
  250. $mimetype = finfo_file($finfo, $file);
  251. if (($pos = strpos($mimetype, ';')) !== false) {
  252. $mimetype = substr($mimetype, 0, $pos);
  253. }
  254. return $mimetype;
  255. }
  256. /**
  257. * Parse headers from a specific URL content.
  258. *
  259. * @param string $url
  260. * @return mixed array of headers or FALSE on failure
  261. */
  262. public static function getHeaderFromUrl($url) {
  263. $url = @parse_url($url);
  264. if (empty($url)) {
  265. return false;
  266. }
  267. $url = array_map('trim', $url);
  268. $url['port'] = (!isset($url['port'])) ? '' : (':' . (int)$url['port']);
  269. $path = (isset($url['path'])) ? $url['path'] : '';
  270. if (empty($path)) {
  271. $path = '/';
  272. }
  273. $path .= (isset($url['query'])) ? "?$url[query]" : '';
  274. $defaults = [
  275. 'http' => [
  276. 'header' => "Accept: text/html\r\n" .
  277. "Connection: Close\r\n" .
  278. "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64)\r\n",
  279. ]
  280. ];
  281. stream_context_get_default($defaults);
  282. if (isset($url['host']) && $url['host'] !== gethostbyname($url['host'])) {
  283. $url = "$url[scheme]://$url[host]$url[port]$path";
  284. $headers = get_headers($url);
  285. if (is_array($headers)) {
  286. return $headers;
  287. }
  288. }
  289. return false;
  290. }
  291. /**
  292. * Add protocol prefix if necessary (and possible)
  293. *
  294. * @param string $url
  295. */
  296. public static function autoPrefixUrl($url, $prefix = null) {
  297. if ($prefix === null) {
  298. $prefix = 'http://';
  299. }
  300. if (($pos = strpos($url, '.')) !== false) {
  301. if (strpos(substr($url, 0, $pos), '//') === false) {
  302. $url = $prefix . $url;
  303. }
  304. }
  305. return $url;
  306. }
  307. /**
  308. * Encode strings with base64_encode and also
  309. * replace chars base64 uses that would mess up the url.
  310. *
  311. * Do not use this for querystrings. Those will escape automatically.
  312. * This is only useful for named or passed params.
  313. *
  314. * @deprecated Use query strings instead
  315. * @param string $string Unsafe string
  316. * @return string Encoded string
  317. */
  318. public static function urlEncode($string) {
  319. return str_replace(['/', '='], ['-', '_'], base64_encode($string));
  320. }
  321. /**
  322. * Decode strings with base64_encode and also
  323. * replace back chars base64 uses that would mess up the url.
  324. *
  325. * Do not use this for querystrings. Those will escape automatically.
  326. * This is only useful for named or passed params.
  327. *
  328. * @deprecated Use query strings instead
  329. * @param string $string Safe string
  330. * @return string Decoded string
  331. */
  332. public static function urlDecode($string) {
  333. return base64_decode(str_replace(['-', '_'], ['/', '='], $string));
  334. }
  335. /**
  336. * Returns true only if all values are true.
  337. * //TODO: maybe move to bootstrap?
  338. *
  339. * @param array $array
  340. * @return bool Result
  341. */
  342. public static function logicalAnd($array) {
  343. if (empty($array)) {
  344. return false;
  345. }
  346. foreach ($array as $result) {
  347. if (!$result) {
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. /**
  354. * Returns true if at least one value is true.
  355. * //TODO: maybe move to bootstrap?
  356. *
  357. * @param array $array
  358. * @return bool Result
  359. *
  360. */
  361. public static function logicalOr($array) {
  362. foreach ($array as $result) {
  363. if ($result) {
  364. return true;
  365. }
  366. }
  367. return false;
  368. }
  369. /**
  370. * On non-transaction db connections it will return a deep array of bools instead of bool.
  371. * So we need to call this method inside the modified saveAll() method to return the expected single bool there, too.
  372. *
  373. * @param array
  374. * @return bool
  375. */
  376. public static function isValidSaveAll($array) {
  377. if (empty($array)) {
  378. return false;
  379. }
  380. $ret = true;
  381. foreach ($array as $key => $val) {
  382. if (is_array($val)) {
  383. $ret = $ret & Utility::logicalAnd($val);
  384. } else {
  385. $ret = $ret & $val;
  386. }
  387. }
  388. return (bool)$ret;
  389. }
  390. /**
  391. * Convenience function for automatic casting in form methods etc.
  392. * //TODO: maybe move to bootstrap?
  393. *
  394. * @param mixed $value
  395. * @param string $type
  396. * @return safe value for DB query, or NULL if type was not a valid one
  397. */
  398. public static function typeCast($value, $type) {
  399. switch ($type) {
  400. case 'int':
  401. $value = (int)$value;
  402. break;
  403. case 'float':
  404. $value = (float)$value;
  405. break;
  406. case 'double':
  407. $value = (double)$value;
  408. break;
  409. case 'array':
  410. $value = (array)$value;
  411. break;
  412. case 'bool':
  413. $value = (bool)$value;
  414. break;
  415. case 'string':
  416. $value = (string)$value;
  417. break;
  418. default:
  419. return null;
  420. }
  421. return $value;
  422. }
  423. /**
  424. * Trim recursivly
  425. *
  426. */
  427. public static function trimDeep($value) {
  428. $value = is_array($value) ? array_map('self::trimDeep', $value) : trim($value);
  429. return $value;
  430. }
  431. /**
  432. * H() recursivly
  433. *
  434. */
  435. public static function specialcharsDeep($value) {
  436. $value = is_array($value) ? array_map('self::specialcharsDeep', $value) : htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  437. return $value;
  438. }
  439. /**
  440. * Main deep method
  441. *
  442. */
  443. public static function deep($function, $value) {
  444. $value = is_array($value) ? array_map('self::' . $function, $value) : $function($value);
  445. return $value;
  446. }
  447. /**
  448. * Counts the dimensions of an array. If $all is set to false (which is the default) it will
  449. * only consider the dimension of the first element in the array.
  450. *
  451. * @param array $array Array to count dimensions on
  452. * @param bool $all Set to true to count the dimension considering all elements in array
  453. * @param int $count Start the dimension count at this number
  454. * @return int The number of dimensions in $array
  455. */
  456. public static function countDim($array, $all = false, $count = 0) {
  457. if ($all) {
  458. $depth = [$count];
  459. if (is_array($array) && reset($array) !== false) {
  460. foreach ($array as $value) {
  461. $depth[] = static::countDim($value, true, $count + 1);
  462. }
  463. }
  464. $return = max($depth);
  465. } else {
  466. if (is_array(reset($array))) {
  467. $return = static::countDim(reset($array)) + 1;
  468. } else {
  469. $return = 1;
  470. }
  471. }
  472. return $return;
  473. }
  474. /**
  475. * Expands the values of an array of strings into a deep array.
  476. * Opposite of flattenList().
  477. *
  478. * It needs at least a single separator to be present in all values
  479. * as the key would otherwise be undefined. If data can contain such key-less
  480. * rows, use $undefinedKey to avoid an exception being thrown. But it will
  481. * effectivly collide with other values in that same key then.
  482. *
  483. * So `Some.Deep.Value` becomes `array('Some' => array('Deep' => array('Value')))`.
  484. *
  485. * @param array $data
  486. * @param string $separator
  487. * @param string $undefinedKey
  488. * @return array
  489. */
  490. public static function expandList(array $data, $separator = '.', $undefinedKey = null) {
  491. $result = [];
  492. foreach ($data as $value) {
  493. $keys = explode($separator, $value);
  494. $value = array_pop($keys);
  495. $keys = array_reverse($keys);
  496. if (!isset($keys[0])) {
  497. if ($undefinedKey === null) {
  498. throw new RuntimeException('Key-less values are not supported without $undefinedKey.');
  499. }
  500. $keys[0] = $undefinedKey;
  501. }
  502. $child = [$keys[0] => [$value]];
  503. array_shift($keys);
  504. foreach ($keys as $k) {
  505. $child = [
  506. $k => $child
  507. ];
  508. }
  509. $result = Set::merge($result, $child);
  510. }
  511. return $result;
  512. }
  513. /**
  514. * Flattens a deep array into an array of strings.
  515. * Opposite of expandList().
  516. *
  517. * So `array('Some' => array('Deep' => array('Value')))` becomes `Some.Deep.Value`.
  518. *
  519. * Note that primarily only string should be used.
  520. * However, boolean values are casted to int and thus
  521. * both boolean and integer values also supported.
  522. *
  523. * @param array $data
  524. * @return array
  525. */
  526. public static function flattenList(array $data, $separator = '.') {
  527. $result = [];
  528. $stack = [];
  529. $path = null;
  530. reset($data);
  531. while (!empty($data)) {
  532. $key = key($data);
  533. $element = $data[$key];
  534. unset($data[$key]);
  535. if (is_array($element) && !empty($element)) {
  536. if (!empty($data)) {
  537. $stack[] = [$data, $path];
  538. }
  539. $data = $element;
  540. reset($data);
  541. $path .= $key . $separator;
  542. } else {
  543. if (is_bool($element)) {
  544. $element = (int)$element;
  545. }
  546. $result[] = $path . $element;
  547. }
  548. if (empty($data) && !empty($stack)) {
  549. list($data, $path) = array_pop($stack);
  550. reset($data);
  551. }
  552. }
  553. return $result;
  554. }
  555. /**
  556. * Force-flattens an array.
  557. *
  558. * Careful with this method. It can lose information.
  559. * The keys will not be changed, thus possibly overwrite each other.
  560. *
  561. * //TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
  562. *
  563. * @param array $array to flatten
  564. * @param bool $perserveKeys
  565. * @return array
  566. */
  567. public static function arrayFlatten($array, $preserveKeys = false) {
  568. if ($preserveKeys) {
  569. return static::_arrayFlatten($array);
  570. }
  571. if (!$array) {
  572. return [];
  573. }
  574. $result = [];
  575. foreach ($array as $key => $value) {
  576. if (is_array($value)) {
  577. $result = array_merge($result, static::arrayFlatten($value));
  578. } else {
  579. $result[$key] = $value;
  580. }
  581. }
  582. return $result;
  583. }
  584. /**
  585. * Force-flattens an array and preserves the keys.
  586. *
  587. * Careful with this method. It can lose information.
  588. * The keys will not be changed, thus possibly overwrite each other.
  589. *
  590. * //TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
  591. *
  592. * @param array $a
  593. * @param array $f
  594. * @return array
  595. */
  596. protected static function _arrayFlatten($a, $f = []) {
  597. if (!$a) {
  598. return [];
  599. }
  600. foreach ($a as $k => $v) {
  601. if (is_array($v)) {
  602. $f = static::_arrayFlatten($v, $f);
  603. } else {
  604. $f[$k] = $v;
  605. }
  606. }
  607. return $f;
  608. }
  609. /**
  610. * Similar to array_shift but on the keys of the array
  611. * like array_shift() only for keys and not values
  612. *
  613. * @param array $keyValuePairs
  614. * @return string key
  615. */
  616. public static function arrayShiftKeys(&$array) {
  617. foreach ($array as $key => $value) {
  618. unset($array[$key]);
  619. return $key;
  620. }
  621. }
  622. protected static $_counterStartTime;
  623. /**
  624. * Returns microtime as float value
  625. * (to be subtracted right away)
  626. *
  627. * @param int $precision
  628. * @return float
  629. */
  630. public static function microtime($precision = 8) {
  631. return round(microtime(true), $precision);
  632. }
  633. /**
  634. * @return void
  635. */
  636. public static function startClock() {
  637. static::$_counterStartTime = static::microtime();
  638. }
  639. /**
  640. * @param int $precision
  641. * @param bool $restartClock
  642. * @return float
  643. */
  644. public static function returnElapsedTime($precision = 8, $restartClock = false) {
  645. $startTime = static::$_counterStartTime;
  646. if ($restartClock) {
  647. static::startClock();
  648. }
  649. return static::calcElapsedTime($startTime, static::microtime(), $precision);
  650. }
  651. /**
  652. * Returns microtime as float value
  653. * (to be subtracted right away)
  654. *
  655. * @param int $start
  656. * @param int $end
  657. * @param int $precision
  658. * @return float
  659. */
  660. public static function calcElapsedTime($start, $end, $precision = 8) {
  661. $elapsed = $end - $start;
  662. return round($elapsed, $precision);
  663. }
  664. /**
  665. * Returns pretty JSON
  666. *
  667. * @link https://github.com/ndejong/pretty_json/blob/master/pretty_json.php
  668. * @param string $json The original JSON string
  669. * @param string $ind The string to indent with
  670. * @return string
  671. */
  672. public static function prettyJson($json, $indString = "\t") {
  673. // Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
  674. $tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);
  675. $indent = 0;
  676. $result = '';
  677. $quotemarksCounter = 0;
  678. $nextTokenUsePrefix = true;
  679. foreach ($tokens as $token) {
  680. $quotemarksCounter = $quotemarksCounter + (count(explode('"', $token)) - 1);
  681. if ($token === '') {
  682. continue;
  683. }
  684. if ($nextTokenUsePrefix) {
  685. $prefix = str_repeat($indString, $indent);
  686. } else {
  687. $prefix = null;
  688. }
  689. // Determine if the quote marks are open or closed
  690. if ($quotemarksCounter & 1) {
  691. // odd - thus quotemarks open
  692. $nextTokenUsePrefix = false;
  693. $newLine = null;
  694. } else {
  695. // even - thus quotemarks closed
  696. $nextTokenUsePrefix = true;
  697. $newLine = "\n";
  698. }
  699. if ($token === "{" || $token === "[") {
  700. $indent++;
  701. $result .= $token . $newLine;
  702. } elseif ($token === "}" || $token === "]") {
  703. $indent--;
  704. if ($indent >= 0) {
  705. $prefix = str_repeat($indString, $indent);
  706. }
  707. if ($nextTokenUsePrefix) {
  708. $result .= $newLine . $prefix . $token;
  709. } else {
  710. $result .= $newLine . $token;
  711. }
  712. } elseif ($token === ",") {
  713. $result .= $token . $newLine;
  714. } else {
  715. $result .= $prefix . $token;
  716. }
  717. }
  718. return str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
  719. }
  720. }