/** * Save the configuration to the specified configuration file * * @param null|string $configFile An optional path to a JSON file to which the data should be written instead of the $configFile defined on instance creation * @param int $jsonOptions Optional options passed to json_encode (e.g. JSON_PRETTY_PRINT) * @param bool $saveUnset Whether to also write unset and default values */ public function save($configFile = null, $jsonOptions = 0, $saveUnset = false) { if ($configFile == null) { $configFile = $this->configFile; } $data = new \StdClass(); foreach ($this->configData as $name => $itemData) { if (isset($itemData->value)) { ValueByPath::setValueByPath($data, $name, $itemData->value, true); } elseif ($saveUnset and isset($itemData->defaultValue)) { ValueByPath::setValueByPath($data, $name, $itemData->defaultValue, true); } elseif ($saveUnset) { ValueByPath::setValueByPath($data, $name, null, true); } if (!$saveUnset and isset($itemData->value) and isset($itemData->defaultValue) and $itemData->value == $itemData->defaultValue) { ValueByPath::removeValueByPath($data, $name); } } file_put_contents($configFile, json_encode($data, $jsonOptions)); }
public function testSetValueByPathNotExisting() { $tree = new StdClass(); $this->assertFalse(ValueByPath::setValueByPath($tree, "a.b.c.d", "New value", false)); $this->assertFalse(isset($tree->a->b->c->d)); }