function GPBUtil::checkMapField
1 call to GPBUtil::checkMapField()
- Struct::setFields in vendor/
google/ protobuf/ src/ Google/ Protobuf/ Struct.php - Unordered map of dynamically typed values.
File
-
vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ GPBUtil.php, line 205
Class
Namespace
Google\Protobuf\InternalCode
public static function checkMapField(&$var, $key_type, $value_type, $klass = null) {
if (!$var instanceof MapField && !is_array($var)) {
throw new \Exception("Expect dict.");
}
if (is_array($var)) {
$tmp = new MapField($key_type, $value_type, $klass);
foreach ($var as $key => $value) {
$tmp[$key] = $value;
}
return $tmp;
}
else {
if ($var->getKeyType() != $key_type) {
throw new \Exception("Expect map field of key type.");
}
if ($var->getValueType() != $value_type) {
throw new \Exception("Expect map field of value type.");
}
if ($var->getValueType() === GPBType::MESSAGE && $var->getValueClass() !== $klass && $var->getLegacyValueClass() !== $klass) {
throw new \Exception("Expect map field of " . $klass . ".");
}
return $var;
}
}