Пример #1
0
/**
 * 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;
    };
}
Пример #2
0
 public function testDictkeysProvider()
 {
     return array(array(assert\type('array'), array()), array(assert\length(1, 1), array('key' => 'value')));
 }