class ConfigDiffTransformer in Update helper 8
Same name and namespace in other branches
- 2.x src/ConfigDiffTransformer.php \Drupal\update_helper\ConfigDiffTransformer
Config transformer for configuration diffing.
@package Drupal\update_helper
Hierarchy
- class \Drupal\update_helper\ConfigDiffTransformer
Expanded class hierarchy of ConfigDiffTransformer
1 file declares its use of ConfigDiffTransformer
- ConfigDiffTransformerTest.php in tests/
src/ Unit/ ConfigDiffTransformerTest.php
1 string reference to 'ConfigDiffTransformer'
1 service uses ConfigDiffTransformer
File
- src/
ConfigDiffTransformer.php, line 10
Namespace
Drupal\update_helperView source
class ConfigDiffTransformer {
/**
* Prefix to use to indicate config hierarchy.
*
* @var string
*
* @see ReversibleConfigDiffer::format().
*/
protected $hierarchyPrefix = '::';
/**
* Prefix to use to indicate config values.
*
* @var string
*
* @see ReversibleConfigDiffer::format().
*/
protected $valuePrefix = ' : ';
/**
* {@inheritdoc}
*/
public function transform($config, $prefix = '') {
$lines = [];
$associative_config = array_keys($config) !== range(0, count($config) - 1);
foreach ($config as $key => $value) {
if (!$associative_config) {
$key = '-';
}
$section_prefix = $prefix ? $prefix . $this->hierarchyPrefix . $key : $key;
if (is_array($value) && !empty($value)) {
$lines[] = $section_prefix;
$new_lines = $this
->transform($value, $section_prefix);
foreach ($new_lines as $line) {
$lines[] = $line;
}
}
else {
$lines[] = $section_prefix . $this->valuePrefix . $this
->stringifyValue($value);
}
}
return $lines;
}
/**
* Reverse transformation of diff.
*
* @param array $config_string_lines
* String configuration lines.
*
* @return array
* Nested configuration array.
*/
public function reverseTransform(array $config_string_lines) {
$result = [];
foreach ($config_string_lines as $row) {
$key_value = explode(' : ', $row);
$key_path = explode('::', $key_value[0]);
$last_key = array_pop($key_path);
$current_element =& $result;
foreach ($key_path as $key) {
if ($key === '-') {
$key = count($current_element) - 1;
}
elseif (!isset($current_element[$key])) {
$current_element[$key] = [];
}
$current_element =& $current_element[$key];
}
$value = [];
if (count($key_value) === 2) {
$value = $this
->unstringifyValue($key_value[1]);
}
if ($last_key === '-') {
$current_element[] = $value;
}
else {
$current_element[$last_key] = $value;
}
}
return $result;
}
/**
* Get string representation of value in format that it can be un-serialized.
*
* @param mixed $value
* Value that should be serialized.
*
* @return string
* Return string representation of value.
*/
protected function stringifyValue($value) {
return serialize($value);
}
/**
* Get correct value from string representation of it.
*
* @param string $value
* String value.
*
* @return mixed
* Returns value.
*/
protected function unstringifyValue($value) {
return unserialize($value);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigDiffTransformer:: |
protected | property | Prefix to use to indicate config hierarchy. | |
ConfigDiffTransformer:: |
protected | property | Prefix to use to indicate config values. | |
ConfigDiffTransformer:: |
public | function | Reverse transformation of diff. | |
ConfigDiffTransformer:: |
protected | function | Get string representation of value in format that it can be un-serialized. | |
ConfigDiffTransformer:: |
public | function | ||
ConfigDiffTransformer:: |
protected | function | Get correct value from string representation of it. |