name = $name; $this->drupalConfig = $drupalConfig; $this->read(); } public function read() { $data = $this->drupalConfig->read($this->name); $this->setData($data !== FALSE ? $data : array()); return $this; } /** * Replaces the data of this configuration object. * * @param array $data * The new configuration data. */ public function setData(array $data) { $this->data = $data; return $this; } /** * Sets value in this config object. * * @param $key * @todo * @param $value * @todo */ public function set($key, $value) { // Type-cast value into a string. $value = $this->castValue($value); // The dot/period is a reserved character; it may appear between keys, but // not within keys. $parts = explode('.', $key); if (count($parts) == 1) { $this->data[$key] = $value; } else { drupal_array_set_nested_value($this->data, $parts, $value); } return $this; } /** * Casts a saved value to a string. * * The configuration system only saves strings or arrays. Any scalar * non-string value is cast to a string. The one exception is boolean FALSE * which would normally become '' when cast to a string, but is manually * cast to '0' here for convenience and consistency. * * Any non-scalar value that is not an array (aka objects) gets cast * to an array. * * @param $value * A value being saved into the configuration system. * @param $value * The value cast to a string or array. */ public function castValue($value) { if (is_scalar($value)) { // Handle special case of FALSE, which should be '0' instead of ''. if ($value === FALSE) { $value = '0'; } else { $value = (string) $value; } } else { // Any non-scalar value must be an array. if (!is_array($value)) { $value = (array) $value; } // Recurse into any nested keys. foreach ($value as $key => $nested_value) { $value[$key] = $this->castValue($nested_value); } } return $value; } /** * Unsets value in this config object. * * @param $key * Name of the key whose value should be unset. */ public function clear($key) { $parts = explode('.', $key); if (count($parts) == 1) { unset($this->data[$key]); } else { drupal_array_unset_nested_value($this->data, $parts); } } /** * Saves the configuration object. */ public function save() { $this->drupalConfig->save($this->name, $this->data); } /** * Deletes the configuration object. */ public function delete() { $this->data = array(); $this->drupalConfig->delete($this->name); } }