When one path doesn't exist, try reading another path.
*/
private $readAliases = array();
protected $isPreviewing = false;
protected $previewData = [];
protected function __construct() {
$this->undefinedMarker = new \StdClass();
}
protected function parsePath($path) {
return AmeMultiDictionary::parsePath($path, StorageInterface::PATH_SEPARATOR);
}
/**
* @param array $prefix
* @param string|array $path
* @return array
*/
protected function addPrefixToPath($prefix, $path) {
return AmeMultiDictionary::addPrefixToPath($prefix, $path, StorageInterface::PATH_SEPARATOR);
}
public function save() {
//Does nothing. Subclasses can override this to save changes.
}
public function getStorageKey() {
return 'not_applicable';
}
public function getPath($path, $defaultValue = null) {
$path = $this->parsePath($path);
//Empty path = return all data.
if ( empty($path) ) {
return $this->getValue($defaultValue);
}
//Try the specified path.
$result = $this->rawGetPath($path, $this->undefinedMarker);
if ( ($result === $this->undefinedMarker) && !empty($this->readAliases) ) {
//Try aliases.
$stringPath = is_array($path)
? implode(StorageInterface::PATH_SEPARATOR, $path)
: $path;
if ( isset($this->readAliases[$stringPath]) ) {
$result = $this->rawGetPath(
$this->readAliases[$stringPath],
$defaultValue
);
}
}
if ( $this->isPreviewing ) {
$previewValue = ameMultiDictionary::get($this->previewData, $path, $this->undefinedMarker);
if ( $previewValue !== $this->undefinedMarker ) {
if ( is_array($previewValue) && is_array($result) ) {
//Merge preview data with existing settings.
$result = $this->mergeArraysRecursively($result, $previewValue);
} else {
//Just override the setting.
return $previewValue;
}
}
}
if ( $result !== $this->undefinedMarker ) {
return $result;
}
//Fall back to default value.
return $defaultValue;
}
/**
* Get the value at a path without using aliases.
*
* @param string[] $path Should always be a pre-parsed array, not a string.
* @param mixed|null $defaultValue
* @return mixed
*/
abstract protected function rawGetPath($path, $defaultValue = null);
public function getValue($defaultValue = null) {
$result = $this->rawGetValue($defaultValue);
if ( $this->isPreviewing ) {
if ( is_array($result) && is_array($this->previewData) ) {
$result = array_merge($result, $this->previewData);
} else {
$result = $this->previewData;
}
}
return $result;
}
abstract protected function rawGetValue($defaultValue = null);
/**
* @param array