/** * Runs a validator through a list of data keys. * * @param mixed $validator to check * * @throws \plan\Invalid * @return \Closure */ function dictkeys($validator) { $compiled = Schema::compile($validator); $type = assert\any(assert\type('array'), assert\instance('\\Traversable')); return function ($data, $root = null) use($type, $compiled) { $data = $type($data, $root); $keys = \array_keys($data); $keys = $compiled($keys, $root); $return = array(); foreach ($keys as $key) { if (!\array_key_exists($key, $data)) { $msg = \strtr('Value for key {key} not found in {data}', array('{key}' => \json_encode($key), '{data}' => \json_encode($data))); throw new Invalid($msg, null, null, $root); } $return[$key] = $data[$key]; } return $return; }; }
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'))); }