BackgroundImageSettings.php in Background Image 8
File
src/BackgroundImageSettings.php
View source
<?php
namespace Drupal\background_image;
use Drupal\Component\Utility\DiffArray;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ImmutableConfigException;
use Drupal\Core\Config\StorableConfigBase;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
class BackgroundImageSettings extends StorableConfigBase {
protected $overriddenData;
public function __construct() {
$this->name = 'background_image.settings.fake';
$this->typedConfigManager = \Drupal::service('config.typed');
}
public function delete() {
throw new ImmutableConfigException("Can not delete immutable configuration {$this->getName()}. This config is automatically stored in the \\Drupal\\background_image\\Entity\\BackgroundImage entity.");
}
public function save($has_trusted_data = FALSE) {
throw new ImmutableConfigException("Can not save immutable configuration {$this->getName()}. This config is automatically handled by the \\Drupal\\background_image\\Entity\\BackgroundImage entity.");
}
public function drupalSettings($name = '') {
$data = self::snakeCaseToCamelCase(empty($name) ? $this
->get() : [
$name => $this
->get($name),
]);
if (empty($name)) {
return $data;
}
return reset($data);
}
public function getOriginal($key = '') {
if (empty($key)) {
return $this->originalData;
}
else {
$parts = explode('.', $key);
if (count($parts) == 1) {
return isset($this->originalData[$key]) ? $this->originalData[$key] : NULL;
}
else {
$value = NestedArray::getValue($this->originalData, $parts, $key_exists);
return $key_exists ? $value : NULL;
}
}
}
public function getOverridden($key = '') {
if (empty($key)) {
return $this->overriddenData;
}
$parts = explode('.', $key);
if (count($parts) == 1) {
return isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : NULL;
}
$value = NestedArray::getValue($this->overriddenData, $parts, $key_exists);
return $key_exists ? $value : NULL;
}
public function initWithData(array $data) {
$this->isNew = FALSE;
$this->data = $data;
$this
->merge($data);
$this->originalData = $this->data;
$this->overriddenData = [];
return $this;
}
public function isOverridden($key = NULL) {
return !empty($this
->getOverridden($key));
}
public function merge(array $data_to_merge) {
foreach ($data_to_merge as $key => $value) {
$this
->set($key, $value);
}
return $this;
}
public function set($key, $value) {
$this->schemaWrapper = NULL;
$value = $this
->castSafeStrings($value);
if (is_array($value)) {
$this
->validateKeys($value);
}
$parts = explode('.', $key);
$nested = count($parts) > 1;
if ($nested) {
NestedArray::setValue($this->data, $parts, $value);
$value = $this
->castValue($key, $value);
NestedArray::setValue($this->data, $parts, $value);
}
else {
$this->data[$key] = $value;
$value = $this
->castValue($key, $value);
$this->data[$key] = $value;
}
$overridden_value = NULL;
if ($this->originalData) {
$overridden_value = $value;
$original_value = $this
->getOriginal($key);
if (is_array($value) && is_array($original_value)) {
if ($diff_value = DiffArray::diffAssocRecursive($value, $original_value)) {
$overridden_value = $value;
}
}
if (isset($overridden_value) && $overridden_value !== $original_value) {
if ($nested) {
NestedArray::setValue($this->overriddenData, $parts, $overridden_value);
}
else {
$this->overriddenData[$key] = $overridden_value;
}
}
elseif ($nested) {
NestedArray::unsetValue($this->overriddenData, $parts);
}
else {
unset($this->overriddenData[$key]);
}
}
return $this;
}
protected static function snakeCaseToCamelCase(array $array = []) {
$converter = new CamelCaseToSnakeCaseNameConverter();
$data = [];
foreach ($array as $key => $value) {
$data[$converter
->denormalize($key)] = is_array($value) ? self::snakeCaseToCamelCase($value) : $value;
}
ksort($data);
return $data;
}
}