Example #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;
    };
}
Example #2
0
 public function testNotInvalidProvider()
 {
     return array(array(123, 123), array(assert\str(), 'string'), array(assert\length(2, 4), array('a', 'b', 'c')), array(assert\any(assert\str(), assert\bool()), true), array(assert\all(assert\str(), assert\length(2, 4)), 'abc'), array(array(1, '1'), array(1, '1', 1, '1')));
 }