Exemple #1
0
 /**
  * @param mixed            $data
  * @param array|string|int $keys
  * @param mixed            $value
  *
  * @return mixed
  */
 public function setValue($data, $keys, $value)
 {
     if ($this->isKeysEmpty($keys)) {
         return $data;
     }
     if (!is_array($keys)) {
         $keys = [$keys];
     }
     $accessor = new Accessor($data);
     $depth = 0;
     $keyCount = count($keys);
     foreach ($keys as $key) {
         if ($depth + 1 === $keyCount) {
             if ($accessor->set($key, $value) === false) {
                 throw new InvalidArgumentException(sprintf('Did not set path %s in structure %s', json_encode($keys), json_encode($data)));
             }
         } elseif ($accessor->to($key) === false) {
             throw new InvalidArgumentException(sprintf('Did not find path %s in structure %s', json_encode($keys), json_encode($data)));
         }
         ++$depth;
     }
     return $accessor->getData();
 }
Exemple #2
0
 /**
  * @test
  * @covers Cocur\Vale\Accessor::set()
  */
 public function setReturnsFalseIfValueCouldNotBeSet()
 {
     $accessor = new Accessor('invalid');
     $this->assertFalse($accessor->set('level1', 'foo'));
 }