/** * 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; }; }
/** * @expectedException \plan\InvalidList * @expectedExceptionMessage Multiple invalid: ["Invalid value at key age (value is \"18 years old\")","Extra key sex not allowed","Required key name not provided"] */ public function testMultipleInvalid() { $validator = assert\dict(array('name' => assert\str(), 'age' => assert\int()), true); $validator(array('age' => '18 years old', 'sex' => 'female')); }