You are here

class BackgroundImageSettings in Background Image 8

Same name and namespace in other branches
  1. 2.x src/BackgroundImageSettings.php \Drupal\background_image\BackgroundImageSettings
  2. 2.0.x src/BackgroundImageSettings.php \Drupal\background_image\BackgroundImageSettings

Hierarchy

Expanded class hierarchy of BackgroundImageSettings

1 file declares its use of BackgroundImageSettings
BackgroundImage.php in src/Entity/BackgroundImage.php

File

src/BackgroundImageSettings.php, line 11

Namespace

Drupal\background_image
View source
class BackgroundImageSettings extends StorableConfigBase {

  /**
   * Overridden data.
   *
   * @var array
   */
  protected $overriddenData;

  /**
   * {@inheritdoc}
   */
  public function __construct() {
    $this->name = 'background_image.settings.fake';
    $this->typedConfigManager = \Drupal::service('config.typed');
  }

  /**
   * {@inheritdoc}
   */
  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.");
  }

  /**
   * {@inheritdoc}
   */
  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.");
  }

  /**
   * Retrieves the data currently set as a drupalSettings array.
   *
   * @param string $name
   *   A specific setting to retrieve. If not set, the entire settings array
   *   will be returned.
   *
   * @return array
   *   The data.
   */
  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 = '') {

    // Whole overridden array.
    if (empty($key)) {
      return $this->overriddenData;
    }

    // Top level key.
    $parts = explode('.', $key);
    if (count($parts) == 1) {
      return isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : NULL;
    }

    // Nested key.
    $value = NestedArray::getValue($this->overriddenData, $parts, $key_exists);
    return $key_exists ? $value : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function initWithData(array $data) {
    $this->isNew = FALSE;

    // Set initial data to default settings for casting to work (needs keys).
    $this->data = $data;

    // Now cast initial data to ensure proper values.
    $this
      ->merge($data);

    // Indicate this is the original data.
    $this->originalData = $this->data;

    // Set initial overridden data to an empty array.
    $this->overriddenData = [];
    return $this;
  }

  /**
   * @param string $key
   *
   * @return bool
   *   TRUE or FALSE
   */
  public function isOverridden($key = NULL) {
    return !empty($this
      ->getOverridden($key));
  }

  /**
   * {@inheritdoc}
   */
  public function merge(array $data_to_merge) {
    foreach ($data_to_merge as $key => $value) {
      $this
        ->set($key, $value);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function set($key, $value) {

    // The schema wrapper depends on the most recent data structure to cast
    // values. It must be reset every time a value has changed to properly cast.
    $this->schemaWrapper = NULL;

    // Ensure all MarkupInterface objects are cast to strings.
    $value = $this
      ->castSafeStrings($value);

    // The dot/period is a reserved character; it may appear between keys, but
    // not within keys.
    if (is_array($value)) {
      $this
        ->validateKeys($value);
    }
    $parts = explode('.', $key);
    $nested = count($parts) > 1;
    if ($nested) {

      // For casting to work properly, the raw value must first be set.
      NestedArray::setValue($this->data, $parts, $value);

      // Now cast the value and set it again.
      $value = $this
        ->castValue($key, $value);
      NestedArray::setValue($this->data, $parts, $value);
    }
    else {

      // For casting to work properly, the raw value must first be set.
      $this->data[$key] = $value;

      // Now cast the value and set it again.
      $value = $this
        ->castValue($key, $value);
      $this->data[$key] = $value;
    }

    // Determine if value overrides original data.
    $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;
  }

  /**
   * Converts snake_case_keys into camelCaseKeys.
   *
   * @param array $array
   *   An array to iterate over.
   *
   * @return array
   *   The converted array.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackgroundImageSettings::$overriddenData protected property Overridden data.
BackgroundImageSettings::delete public function Deletes the configuration object. Overrides StorableConfigBase::delete
BackgroundImageSettings::drupalSettings public function Retrieves the data currently set as a drupalSettings array.
BackgroundImageSettings::getOriginal public function
BackgroundImageSettings::getOverridden public function
BackgroundImageSettings::initWithData public function Initializes a configuration object with pre-loaded data. Overrides StorableConfigBase::initWithData
BackgroundImageSettings::isOverridden public function
BackgroundImageSettings::merge public function Merges data into a configuration object. Overrides ConfigBase::merge
BackgroundImageSettings::save public function Saves the configuration object. Overrides StorableConfigBase::save
BackgroundImageSettings::set public function Sets a value in this configuration object. Overrides ConfigBase::set
BackgroundImageSettings::snakeCaseToCamelCase protected static function Converts snake_case_keys into camelCaseKeys.
BackgroundImageSettings::__construct public function
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
ConfigBase::$data protected property The data of the configuration object.
ConfigBase::$name protected property The name of the configuration object.
ConfigBase::castSafeStrings protected function Casts any objects that implement MarkupInterface to string.
ConfigBase::clear public function Unsets a value in this configuration object. 1
ConfigBase::get public function Gets data from this configuration object. 1
ConfigBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyTrait::getCacheContexts
ConfigBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyTrait::getCacheMaxAge
ConfigBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyTrait::getCacheTags 1
ConfigBase::getName public function Returns the name of this configuration object.
ConfigBase::MAX_NAME_LENGTH constant The maximum length of a configuration object name.
ConfigBase::setData public function Replaces the data of this configuration object. 1
ConfigBase::setName public function Sets the name of this configuration object.
ConfigBase::validateKeys protected function Validates all keys in a passed in config array structure.
ConfigBase::validateName public static function Validates the configuration object name.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
StorableConfigBase::$isNew protected property Whether the configuration object is new or has been saved to the storage.
StorableConfigBase::$originalData protected property The data of the configuration object.
StorableConfigBase::$schemaWrapper protected property The config schema wrapper object for this configuration object.
StorableConfigBase::$storage protected property The storage used to load and save this configuration object.
StorableConfigBase::$typedConfigManager protected property The typed config manager.
StorableConfigBase::castValue protected function Casts the value to correct data type using the configuration schema.
StorableConfigBase::getSchemaWrapper protected function Gets the schema wrapper for the whole configuration object.
StorableConfigBase::getStorage public function Retrieves the storage used to load and save this configuration object.
StorableConfigBase::isNew public function Returns whether this configuration object is new.
StorableConfigBase::validateValue protected function Validate the values are allowed data types.