/** * Validate the structure of an object. * * @param array $structure to be validation in given $data * @param string $class the class name of the object * @param string $byref if false, a new object will be created * * @return \Closure */ function object(array $structure, $class = null, $byref = true) { $type = assert\all(assert\type('object'), assert\iif(null !== $class, assert\instance($class)), filter\vars(false, true), assert\dict($structure, false, true)); return function ($data, $path = null) use($type, $byref) { $vars = $type($data, $path); if ($byref) { $object = $data; } else { $object = clone $data; } foreach ($vars as $key => $value) { $object->{$key} = $value; } return $object; }; }
/** * @covers ::plan\filter\vars * @dataProvider testVarsProvider */ public function testVarsObject($recursive, $inscope, $expected, $object, $fix = null) { $validator = new plan(filter\vars($recursive, $inscope)); $validated = $validator($object); if (\is_callable($fix)) { $fix($validated); } $this->assertEquals($expected, $validated); }