DeepGettable.php
3 weeks ago
DependencyResolvable.php
3 weeks ago
Dispatchable.php
3 weeks ago
HasConstants.php
3 weeks ago
DeepGettable.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Trait that retrieves nested array values using dot-notation keys. |
| 5 | * Walks each segment recursively and returns a default when any level is missing or empty. |
| 6 | * Shared by classes that need deep array access without pulling in a full Arr helper. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Concerns |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Concerns; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | trait DeepGettable |
| 16 | { |
| 17 | /** |
| 18 | * Return the nested setting values by key recursively. |
| 19 | * |
| 20 | * @param array $settings The settings. |
| 21 | * @param mixed $keys The keys. |
| 22 | * @param mixed $default The default. |
| 23 | * |
| 24 | * @return mixed|null |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | protected function deep_get(array $settings, $keys, $default = null) |
| 29 | { |
| 30 | $keys = \is_array($keys) ? $keys : \explode('.', $keys); |
| 31 | if (empty($keys) || empty($settings)) { |
| 32 | return $default; |
| 33 | } |
| 34 | $parent_key = $keys[0]; |
| 35 | $child_keys = \array_slice($keys, 1); |
| 36 | if (empty($child_keys)) { |
| 37 | return $settings[$parent_key] ?? $default; |
| 38 | } |
| 39 | if (empty($settings[$parent_key])) { |
| 40 | return $default; |
| 41 | } |
| 42 | return $this->deep_get($settings[$parent_key], $child_keys, $default); |
| 43 | } |
| 44 | } |
| 45 |