| @@ -1,0 +1,86 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.html | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Utility class for doing all sorts of odds and ends with arrays. | |
| 16 | + * | |
| 17 | + * @since 10.1.16 | |
| 18 | + */ | |
| 19 | +final class ArrayHelper | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Private constructor to prevent instantiation of this class. | |
| 23 | + */ | |
| 24 | + private function __construct() | |
| 25 | + { | |
| 26 | + | |
| 27 | + } | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Utility function to map an array to a string. | |
| 31 | + * | |
| 32 | + * @param array $array The array to map. | |
| 33 | + * @param string $innerGlue The glue (optional, defaults to '=') between the key and the value. | |
| 34 | + * @param string $outerGlue The glue (optional, defaults to ' ') between array elements. | |
| 35 | + * @param boolean $keepOuterKey True if final key should be kept. | |
| 36 | + * | |
| 37 | + * @return string | |
| 38 | + */ | |
| 39 | + public static function toString(array $array, $innerGlue = '=', $outerGlue = ' ', $keepOuterKey = false) | |
| 40 | + { | |
| 41 | + $output = array(); | |
| 42 | + | |
| 43 | + foreach ($array as $key => $item) | |
| 44 | + { | |
| 45 | + if (is_array($item)) | |
| 46 | + { | |
| 47 | + if ($keepOuterKey) | |
| 48 | + { | |
| 49 | + $output[] = $key; | |
| 50 | + } | |
| 51 | + | |
| 52 | + // This is value is an array, go and do it again! | |
| 53 | + $output[] = static::toString($item, $innerGlue, $outerGlue, $keepOuterKey); | |
| 54 | + } | |
| 55 | + else | |
| 56 | + { | |
| 57 | + $output[] = $key . $innerGlue . '"' . htmlspecialchars($item, ENT_COMPAT, 'UTF-8') . '"'; | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + return implode($outerGlue, $output); | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Method to determine if an array is an associative array. | |
| 66 | + * | |
| 67 | + * @param array $array An array to test. | |
| 68 | + * | |
| 69 | + * @return boolean | |
| 70 | + */ | |
| 71 | + public static function isAssociative($array) | |
| 72 | + { | |
| 73 | + if (is_array($array)) | |
| 74 | + { | |
| 75 | + foreach (array_keys($array) as $k => $v) | |
| 76 | + { | |
| 77 | + if ($k !== $v) | |
| 78 | + { | |
| 79 | + return true; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + return false; | |
| 85 | + } | |
| 86 | +} | |