Esempio n. 1
0
 public function testRemoveValueByPathNotExisting()
 {
     $c = new StdClass();
     $c->d1 = "This is the value";
     $c->d2 = "This will be removed";
     $b = new StdClass();
     $b->c = $c;
     $a = new StdClass();
     $a->b = $b;
     $tree = new StdClass();
     $tree->a = $a;
     $this->assertFalse(ValueByPath::removeValueByPath($tree, "a.b.c.d3"));
     $this->assertTrue(isset($tree->a->b->c->d1));
     $this->assertTrue(isset($tree->a->b->c->d2));
 }
Esempio n. 2
0
 /**
  * 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));
 }