← All changes
|
vendor/wpfluent/framework/src/WPFluent/Foundation/Config.php
+44
-3
1.21
→
2.1.0
View file →
| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | protected $data = []; |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * Construct the Config instance |
| 17 | - * @param null | |
| 17 | + * @param array $data | |
| 18 | 18 | */ |
| 19 | 19 | public function __construct($data) |
| 20 | 20 | { |
| 21 | 21 | $this->data = $data; |
| @@ -30,9 +30,10 @@ | ||
| 30 | 30 | return $this->get(); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | - * Retrieve specific item from config array | |
| 34 | + * Retrieve specific item from config array. | |
| 35 | + * | |
| 35 | 36 | * @param string $key |
| 36 | 37 | * @param string $default |
| 37 | 38 | * @return mixed |
| 38 | 39 | */ |
| @@ -37,17 +38,57 @@ | ||
| 37 | 38 | * @return mixed |
| 38 | 39 | */ |
| 39 | 40 | public function get($key = null, $default = null) |
| 40 | 41 | { |
| 42 | + $key = $this->resolveKey($key); | |
| 43 | + | |
| 41 | 44 | return $key ? Arr::get($this->data, $key, $default) : $this->data; |
| 42 | 45 | } |
| 43 | 46 | |
| 44 | 47 | /** |
| 48 | + * Get only specific items from the config array. | |
| 49 | + * | |
| 50 | + * @param string|array $key ($key1, $key2 or [$key1, $key2]) | |
| 51 | + * @return array | |
| 52 | + */ | |
| 53 | + public function only($key) | |
| 54 | + { | |
| 55 | + $keys = array_map(function ($key) { | |
| 56 | + return $this->resolveKey($key); | |
| 57 | + }, is_Array($key) ? $key : func_get_args()); | |
| 58 | + | |
| 59 | + $result = []; | |
| 60 | + | |
| 61 | + foreach ($keys as $key) { | |
| 62 | + $result[] = Arr::get($this->data, $key); | |
| 63 | + } | |
| 64 | + | |
| 65 | + return array_values(array_filter($result)); | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 45 | 69 | * Set an item into the config array on the fly. |
| 46 | 70 | * @param string $key |
| 47 | - * @param mkixed $value | |
| 71 | + * @param mixed $value | |
| 48 | 72 | */ |
| 49 | 73 | public function set($key, $value) |
| 50 | 74 | { |
| 51 | 75 | Arr::set($this->data, $key, $value); |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Resolove the config key, add app. prefix if needed. | |
| 80 | + * | |
| 81 | + * @param string $key | |
| 82 | + * @return string | |
| 83 | + */ | |
| 84 | + protected function resolveKey($key) | |
| 85 | + { | |
| 86 | + if (!$key) return $key; | |
| 87 | + | |
| 88 | + if (array_key_exists($key, $this->data)) { | |
| 89 | + return $key; | |
| 90 | + } | |
| 91 | + | |
| 92 | + return str_contains($key, '.') ? $key : "app.{$key}"; | |
| 52 | 93 | } |
| 53 | 94 | } |