/** * @param mixed $data * @param array|string|int $keys * @param mixed|null $default * * @return mixed */ public function getValue($data, $keys, $default = null) { if ($this->isKeysEmpty($keys)) { return $data; } if (!is_array($keys)) { $keys = [$keys]; } $accessor = new Accessor($data); foreach ($keys as $key) { if ($accessor->to($key) === false) { return $default; } } return $accessor->getCurrent(); }
/** * @test * @covers Cocur\Vale\Accessor::to() * @covers Cocur\Vale\Accessor::isObjectWithMethod() */ public function toGoesToKeyInObjectUsingIs() { eval('class AccessorTestMockToGoesToKeyInObjectUsingIs { public function is($k) { return "bar"; } }'); $obj = new \AccessorTestMockToGoesToKeyInObjectUsingIs(); $accessor = new Accessor($obj); $this->assertTrue($accessor->to('level1')); $this->assertSame('bar', $accessor->getCurrent()); }