Beispiel #1
0
 /**
  * @param mixed            $data
  * @param array|string|int $keys
  *
  * @return mixed
  */
 public function removeValue($data, $keys)
 {
     if ($this->isKeysEmpty($keys)) {
         return;
     }
     if (!is_array($keys)) {
         $keys = [$keys];
     }
     $accessor = new Accessor($data);
     $keyCount = count($keys);
     $depth = 0;
     foreach ($keys as $key) {
         if ($depth + 1 === $keyCount) {
             if ($accessor->remove($key) === false) {
                 throw new InvalidArgumentException(sprintf('Did not remove 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();
 }
Beispiel #2
0
 /**
  * @test
  * @covers Cocur\Vale\Accessor::remove()
  */
 public function removeRemovesValueFromObjectUsingRemove()
 {
     eval('class AccessorTestMockRemovesValueFromObjectUsingRemove { private $v; public function remove($v) { unset($this->v); } public function is() { return isset($this->v); } }');
     $obj = new \AccessorTestMockRemovesValueFromObjectUsingRemove();
     $accessor = new Accessor($obj);
     $this->assertTrue($accessor->remove('level1'));
     $this->assertFalse($accessor->getData()->is());
 }