replaceRecursive() public static method

This differs from {@see \array_replace_recursive} in a couple ways: - Lists (indexed arrays) from second array completely replace list in first array. - Null values from second array do not replace lists or associative arrays in first (they do still replace scalar values).
public static replaceRecursive ( array $array1, array $array2 ) : array
$array1 array
$array2 array
return array The combined array
Example #1
0
File: Import.php Project: bolt/bolt
 /**
  * Insert an individual Contenttype record into the database
  *
  * @param string $filename
  * @param string $contenttypeslug
  * @param array  $values
  *
  * @return boolean
  */
 private function insertRecord($filename, $contenttypeslug, array $values)
 {
     // Determine a/the slug
     $slug = isset($values['slug']) ? $values['slug'] : substr($this->app['slugify']->slugify($values['title']), 0, 127);
     if (!$this->isRecordUnique($contenttypeslug, $slug)) {
         $this->setWarning(true)->setWarningMessage("File '{$filename}' has an exiting ContentType '{$contenttypeslug}' with the slug '{$slug}'! Skipping record.");
         return false;
     }
     // Get a status
     if (isset($values['status'])) {
         $status = $values['status'];
     } else {
         $status = $this->contenttypes[$contenttypeslug]['default_status'];
     }
     // Transform the 'publish' action to a 'published' status
     $status = $status === 'publish' ? 'published' : $status;
     // Insist on a title field
     if (!isset($values['title'])) {
         $this->setWarning(true)->setWarningMessage("File '{$filename}' has a '{$contenttypeslug}' with a missing title field! Skipping record.");
         return false;
     }
     // Set up default meta
     $meta = ['slug' => $slug, 'datecreated' => date('Y-m-d H:i:s'), 'datepublish' => $status == 'published' ? date('Y-m-d H:i:s') : null, 'ownerid' => 1];
     $values = Arr::replaceRecursive($values, $meta);
     $record = $this->app['storage']->getEmptyContent($contenttypeslug);
     $record->setValues($values);
     if ($this->app['storage']->saveContent($record) === false) {
         $this->setWarning(true)->setWarningMessage("Failed to imported record with title: {$values['title']} from '{$filename}'! Skipping record.");
         return false;
     } else {
         $this->setNotice(true)->setNoticeMessage("Imported record with title: {$values['title']}.");
         return true;
     }
 }
Example #2
0
 /**
  * @dataProvider replaceRecursiveProvider
  *
  * @param array $array1
  * @param array $array2
  * @param array $result
  */
 public function testReplaceRecursive($array1, $array2, $result)
 {
     $this->assertEquals($result, Arr::replaceRecursive($array1, $array2));
 }
Example #3
0
 /**
  * Merge in a yaml file to the config.
  *
  * @param YamlFile $file
  */
 private function addConfig(YamlFile $file)
 {
     $app = $this->getContainer();
     try {
         $newConfig = $file->parse();
     } catch (RuntimeException $e) {
         $app['logger.flash']->danger($e->getMessage());
         $app['logger.system']->error($e->getMessage(), ['event' => 'exception', 'exception' => $e]);
         throw $e;
     }
     if (is_array($newConfig)) {
         $this->config = Arr::replaceRecursive($this->config, $newConfig);
     }
 }
Example #4
0
File: Config.php Project: bolt/bolt
 /**
  * Read and parse the config.yml and config_local.yml configuration files.
  *
  * @return array
  */
 protected function parseGeneral()
 {
     // Read the config and merge it. (note: We use temp variables to prevent
     // "Only variables should be passed by reference")
     $tempconfig = $this->parseConfigYaml('config.yml');
     $tempconfiglocal = $this->parseConfigYaml('config_local.yml');
     $general = Arr::replaceRecursive($tempconfig, $tempconfiglocal);
     // Make sure old settings for 'accept_file_types' are not still picked up. Before 1.5.4 we used to store them
     // as a regex-like string, and we switched to an array. If we find the old style, fall back to the defaults.
     if (isset($general['accept_file_types']) && !is_array($general['accept_file_types'])) {
         unset($general['accept_file_types']);
     }
     // Merge the array with the defaults. Setting the required values that aren't already set.
     $general = Arr::replaceRecursive($this->defaultConfig, $general);
     // Make sure the cookie_domain for the sessions is set properly.
     if (empty($general['cookies_domain'])) {
         $request = Request::createFromGlobals();
         if ($request->server->get('HTTP_HOST', false)) {
             $hostSegments = explode(':', $request->server->get('HTTP_HOST'));
             $hostname = reset($hostSegments);
         } elseif ($request->server->get('SERVER_NAME', false)) {
             $hostname = $request->server->get('SERVER_NAME');
         } else {
             $hostname = '';
         }
         // Don't set the domain for a cookie on a "TLD" - like 'localhost', or if the server_name is an IP-address
         if (strpos($hostname, '.') > 0 && preg_match('/[a-z0-9]/i', $hostname)) {
             if (preg_match('/^www[0-9]*./', $hostname)) {
                 $general['cookies_domain'] = '.' . preg_replace('/^www[0-9]*./', '', $hostname);
             } else {
                 $general['cookies_domain'] = '.' . $hostname;
             }
             // Make sure we don't have consecutive '.'-s in the cookies_domain.
             $general['cookies_domain'] = str_replace('..', '.', $general['cookies_domain']);
         } else {
             $general['cookies_domain'] = '';
         }
     }
     // Make sure Bolt's mount point is OK:
     $general['branding']['path'] = '/' . Str::makeSafe($general['branding']['path']);
     // Set the link in branding, if provided_by is set.
     $general['branding']['provided_link'] = Html::providerLink($general['branding']['provided_by']);
     $general['database'] = $this->parseDatabase($general['database']);
     return $general;
 }
Example #5
0
 /**
  * Enforce the default JSON settings.
  *
  * @param array $json
  *
  * @return array
  */
 private function setJsonDefaults(array $json)
 {
     $rootPath = $this->app['resources']->getPath('root');
     $extensionsPath = $this->app['resources']->getPath('extensions');
     $srcPath = $this->app['resources']->getPath('src');
     $webPath = $this->app['resources']->getPath('web');
     $pathToRoot = Path::makeRelative($rootPath, $extensionsPath);
     $pathToWeb = Path::makeRelative($webPath, $extensionsPath);
     $eventPath = Path::makeRelative($srcPath . '/Composer/EventListener', $extensionsPath);
     /** @deprecated Handle BC on 'stability' key until 4.0 */
     $minimumStability = $this->app['config']->get('general/extensions/stability') ?: $this->app['config']->get('general/extensions/composer/minimum-stability', 'stable');
     // Enforce standard settings
     $defaults = ['name' => 'bolt/extensions', 'description' => 'Bolt extension installation interface', 'license' => 'MIT', 'repositories' => ['packagist' => false, 'bolt' => ['type' => 'composer', 'url' => $this->app['extend.site'] . 'satis/']], 'minimum-stability' => $minimumStability, 'prefer-stable' => true, 'config' => ['discard-changes' => true, 'preferred-install' => 'dist'], 'provide' => ['bolt/bolt' => Bolt\Version::forComposer()], 'extra' => ['bolt-web-path' => $pathToWeb, 'bolt-root-path' => $pathToRoot], 'autoload' => ['psr-4' => ['Bolt\\Composer\\EventListener\\' => $eventPath]], 'scripts' => ['post-autoload-dump' => 'Bolt\\Composer\\EventListener\\PackageEventListener::dump', 'post-package-install' => 'Bolt\\Composer\\EventListener\\PackageEventListener::handle', 'post-package-update' => 'Bolt\\Composer\\EventListener\\PackageEventListener::handle']];
     $json = Arr::replaceRecursive($json, $defaults);
     ksort($json);
     return $json;
 }