Image
2 days ago
Select
2 days ago
Arrays.php
2 days ago
AvailableTranslations.php
2 days ago
Creatable.php
2 days ago
Dashicon.php
2 days ago
Date.php
2 days ago
Html.php
2 days ago
Icon.php
2 days ago
Image.php
2 days ago
LocalFile.php
2 days ago
Mbstring.php
2 days ago
MediaIdResolver.php
2 days ago
Menu.php
2 days ago
Network.php
2 days ago
Post.php
2 days ago
Strings.php
2 days ago
Taxonomy.php
2 days ago
User.php
2 days ago
UserRoles.php
2 days ago
WpDateFormat.php
2 days ago
Arrays.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper; |
| 6 | |
| 7 | use AC\Formatter\ImplodeRecursive; |
| 8 | use AC\Type\Value; |
| 9 | |
| 10 | class Arrays extends Creatable |
| 11 | { |
| 12 | public function add_nested_value(array $keys, $value, array $result = []): array |
| 13 | { |
| 14 | $key = array_shift($keys); |
| 15 | |
| 16 | if ($keys) { |
| 17 | $value = $this->add_nested_value( |
| 18 | $keys, |
| 19 | $value, |
| 20 | isset($result[$key]) && is_array($result[$key]) ? $result[$key] : [] |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | $result[$key] = $value; |
| 25 | |
| 26 | return $result; |
| 27 | } |
| 28 | |
| 29 | public function get_nested_value(array $array, array $keys) |
| 30 | { |
| 31 | foreach ($keys as $key) { |
| 32 | if (! isset($array[$key])) { |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | $array = $array[$key]; |
| 37 | } |
| 38 | |
| 39 | return $array; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @deprecated 7.0 |
| 44 | */ |
| 45 | public function implode_recursive(string $glue, array $pieces): string |
| 46 | { |
| 47 | _deprecated_function(__METHOD__, '7.0', ImplodeRecursive::class); |
| 48 | |
| 49 | $value = (new ImplodeRecursive($glue))->format(new Value($pieces)); |
| 50 | |
| 51 | return (string)$value; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Indents any object as long as it has a unique id and that of its parent. |
| 56 | */ |
| 57 | public function indent( |
| 58 | array $array, |
| 59 | int $parentId = 0, |
| 60 | string $parentKey = 'post_parent', |
| 61 | string $selfKey = 'ID', |
| 62 | string $childrenKey = 'children' |
| 63 | ): array { |
| 64 | $indent = []; |
| 65 | |
| 66 | $i = 0; |
| 67 | foreach ($array as $v) { |
| 68 | if ($v->$parentKey == $parentId) { |
| 69 | $indent[$i] = $v; |
| 70 | $indent[$i]->$childrenKey = $this->indent($array, $v->$selfKey, $parentKey, $selfKey); |
| 71 | |
| 72 | $i++; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $indent; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Remove empty values from array |
| 81 | */ |
| 82 | public function filter(array $array): array |
| 83 | { |
| 84 | return array_filter($array, [Strings::create(), 'is_not_empty']); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Insert element into array at specific position |
| 89 | */ |
| 90 | public function insert(array $array, array $insert, $position): array |
| 91 | { |
| 92 | $new = []; |
| 93 | foreach ($array as $key => $value) { |
| 94 | $new[$key] = $value; |
| 95 | if ($key === $position) { |
| 96 | $new = array_merge($new, $insert); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $new; |
| 101 | } |
| 102 | |
| 103 | public function is_associative($array): bool |
| 104 | { |
| 105 | _deprecated_function(__METHOD__, '7.0'); |
| 106 | |
| 107 | if (! is_array($array)) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | foreach ($array as $key => $value) { |
| 112 | if (is_string($key)) { |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @deprecated 7.0 |
| 122 | */ |
| 123 | public function get_duplicates(): array |
| 124 | { |
| 125 | _deprecated_function(__METHOD__, '7.0'); |
| 126 | |
| 127 | return []; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @deprecated 7.0 |
| 132 | */ |
| 133 | public function get_integers_from_mixed(): array |
| 134 | { |
| 135 | _deprecated_function(__METHOD__, '7.0'); |
| 136 | |
| 137 | return []; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @deprecated 7.0 |
| 142 | */ |
| 143 | public function key_replace(): array |
| 144 | { |
| 145 | _deprecated_function(__METHOD__, '7.0'); |
| 146 | |
| 147 | return []; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 |