From 42c82513daae67323ee769feaa64525b60449165 Mon Sep 17 00:00:00 2001 From: Webklex Date: Mon, 30 Jul 2018 22:22:35 +0200 Subject: [PATCH] Fixing undefined index #131 Fixing undefined index error if associative config array isn't properly filled #131 --- CHANGELOG.md | 7 +++ README.md | 2 +- src/IMAP/Providers/LaravelServiceProvider.php | 54 ++++++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1729df6..cc7888e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,13 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ### Affected Classes - NaN +## [1.2.5] - 2018-07-30 +### Fixed +- Fixing undefined index error if associative config array isn't properly filled #131 + +### Affected Classes +- [LaravelServiceProvider::class](src/IMAP/Providers/LaravelServiceProvider.php) + ## [1.2.4] - 2018-07-26 ### Fixed - fetch_flags default set to true on all methods diff --git a/README.md b/README.md index a59abae..b5872c4 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Laravel IMAP is an easy way to integrate the native php imap library into your * 1) Install the php-imap library if it isn't already installed: ``` shell -sudo apt-get install php*-imap && sudo apache2ctl graceful +sudo apt-get install php*-imap php*-mbstring php*-mcrypt && sudo apache2ctl graceful ``` You might also want to check `phpinfo()` if the extension is enabled. diff --git a/src/IMAP/Providers/LaravelServiceProvider.php b/src/IMAP/Providers/LaravelServiceProvider.php index fbf1641..2306709 100644 --- a/src/IMAP/Providers/LaravelServiceProvider.php +++ b/src/IMAP/Providers/LaravelServiceProvider.php @@ -66,7 +66,7 @@ private function setVendorConfig(){ $vendor_config = require $path; $config = $this->app['config']->get($config_key, []); - $this->app['config']->set($config_key, array_merge($vendor_config, $config)); + $this->app['config']->set($config_key, $this->array_merge_recursive_distinct($vendor_config, $config)); $config = $this->app['config']->get($config_key); @@ -90,4 +90,56 @@ private function setVendorConfig(){ $this->app['config']->set($config_key, $config); } + + /** + * Marge arrays recursively and distinct + * + * Merges any number of arrays / parameters recursively, replacing + * entries with string keys with values from latter arrays. + * If the entry or the next value to be assigned is an array, then it + * automatically treats both arguments as an array. + * Numeric entries are appended, not replaced, but only if they are + * unique + * + * @param array $array1 Initial array to merge. + * @param array ... Variable list of arrays to recursively merge. + * + * @return array|mixed + * + * @link http://www.php.net/manual/en/function.array-merge-recursive.php#96201 + * @author Mark Roduner + */ + private function array_merge_recursive_distinct() { + + $arrays = func_get_args(); + $base = array_shift($arrays); + + if(!is_array($base)) $base = empty($base) ? array() : array($base); + + foreach($arrays as $append) { + + if(!is_array($append)) $append = array($append); + + foreach($append as $key => $value) { + + if(!array_key_exists($key, $base) and !is_numeric($key)) { + $base[$key] = $append[$key]; + continue; + } + + if(is_array($value) or is_array($base[$key])) { + $base[$key] = $this->array_merge_recursive_distinct($base[$key], $append[$key]); + } else if(is_numeric($key)) { + if(!in_array($value, $base)) $base[] = $value; + } else { + $base[$key] = $value; + } + + } + + } + + return $base; + } + } \ No newline at end of file