Exemple #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;
    };
}
Exemple #2
0
 /**
  * @expectedException        \plan\InvalidList
  * @expectedExceptionMessage Multiple invalid: ["Expected \\stdClass (is ArrayObject)"]
  */
 public function testInstanceInvalid()
 {
     $validator = new plan(assert\instance('\\stdClass'));
     $validator(new \ArrayObject());
 }