function Message::convertJsonValueToProtoValue
1 call to Message::convertJsonValueToProtoValue()
- Message::mergeFromArrayJsonImpl in vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ Message.php
File
-
vendor/
google/ protobuf/ src/ Google/ Protobuf/ Internal/ Message.php, line 795
Class
- Message
- Parent class of all proto messages. Users should not instantiate this class or extend this class or its child classes by their own. See the comment of specific functions for more details.
Namespace
Google\Protobuf\InternalCode
private function convertJsonValueToProtoValue($value, $field, $ignore_unknown, $is_map_key = false) {
switch ($field->getType()) {
case GPBType::MESSAGE:
$klass = $field->getMessageType()
->getClass();
$submsg = new $klass();
if (is_a($submsg, "Google\\Protobuf\\Duration")) {
if (is_null($value)) {
return $this->defaultValue($field);
}
else {
if (!is_string($value)) {
throw new GPBDecodeException("Expect string.");
}
}
return GPBUtil::parseDuration($value);
}
else {
if ($field->isTimestamp()) {
if (is_null($value)) {
return $this->defaultValue($field);
}
else {
if (!is_string($value)) {
throw new GPBDecodeException("Expect string.");
}
}
try {
$timestamp = GPBUtil::parseTimestamp($value);
} catch (\Exception $e) {
throw new GPBDecodeException("Invalid RFC 3339 timestamp: " . $e->getMessage());
}
$submsg->setSeconds($timestamp->getSeconds());
$submsg->setNanos($timestamp->getNanos());
}
else {
if (is_a($submsg, "Google\\Protobuf\\FieldMask")) {
if (is_null($value)) {
return $this->defaultValue($field);
}
try {
return GPBUtil::parseFieldMask($value);
} catch (\Exception $e) {
throw new GPBDecodeException("Invalid FieldMask: " . $e->getMessage());
}
}
else {
if (is_null($value) && !is_a($submsg, "Google\\Protobuf\\Value")) {
return $this->defaultValue($field);
}
if (GPBUtil::hasSpecialJsonMapping($submsg)) {
}
elseif (!is_object($value) && !is_array($value)) {
throw new GPBDecodeException("Expect message.");
}
$submsg->mergeFromJsonArray($value, $ignore_unknown);
}
}
}
return $submsg;
case GPBType::ENUM:
if (is_null($value)) {
return $this->defaultValue($field);
}
if (is_integer($value)) {
return $value;
}
$enum_value = $field->getEnumType()
->getValueByName($value);
if (!is_null($enum_value)) {
return $enum_value->getNumber();
}
else {
if ($ignore_unknown) {
return $this->defaultValue($field);
}
else {
throw new GPBDecodeException("Enum field only accepts integer or enum value name");
}
}
case GPBType::STRING:
if (is_null($value)) {
return $this->defaultValue($field);
}
if (is_numeric($value)) {
return strval($value);
}
if (!is_string($value)) {
throw new GPBDecodeException("String field only accepts string value");
}
return $value;
case GPBType::BYTES:
if (is_null($value)) {
return $this->defaultValue($field);
}
if (!is_string($value)) {
throw new GPBDecodeException("Byte field only accepts string value");
}
$proto_value = base64_decode($value, true);
if ($proto_value === false) {
throw new GPBDecodeException("Invalid base64 characters");
}
return $proto_value;
case GPBType::BOOL:
if (is_null($value)) {
return $this->defaultValue($field);
}
if ($is_map_key) {
if ($value === "true") {
return true;
}
if ($value === "false") {
return false;
}
throw new GPBDecodeException("Bool field only accepts bool value");
}
if (!is_bool($value)) {
throw new GPBDecodeException("Bool field only accepts bool value");
}
return $value;
case GPBType::FLOAT:
case GPBType::DOUBLE:
if (is_null($value)) {
return $this->defaultValue($field);
}
if ($value === "Infinity") {
return INF;
}
if ($value === "-Infinity") {
return -INF;
}
if ($value === "NaN") {
return NAN;
}
return $value;
case GPBType::INT32:
case GPBType::SINT32:
case GPBType::SFIXED32:
if (is_null($value)) {
return $this->defaultValue($field);
}
if (!is_numeric($value)) {
throw new GPBDecodeException("Invalid data type for int32 field");
}
if (is_string($value) && trim($value) !== $value) {
throw new GPBDecodeException("Invalid data type for int32 field");
}
if (bccomp($value, "2147483647") > 0) {
throw new GPBDecodeException("Int32 too large");
}
if (bccomp($value, "-2147483648") < 0) {
throw new GPBDecodeException("Int32 too small");
}
return $value;
case GPBType::UINT32:
case GPBType::FIXED32:
if (is_null($value)) {
return $this->defaultValue($field);
}
if (!is_numeric($value)) {
throw new GPBDecodeException("Invalid data type for uint32 field");
}
if (is_string($value) && trim($value) !== $value) {
throw new GPBDecodeException("Invalid data type for int32 field");
}
if (bccomp($value, 4294967295) > 0) {
throw new GPBDecodeException("Uint32 too large");
}
return $value;
case GPBType::INT64:
case GPBType::SINT64:
case GPBType::SFIXED64:
if (is_null($value)) {
return $this->defaultValue($field);
}
if (!is_numeric($value)) {
throw new GPBDecodeException("Invalid data type for int64 field");
}
if (is_string($value) && trim($value) !== $value) {
throw new GPBDecodeException("Invalid data type for int64 field");
}
if (bccomp($value, "9223372036854775807") > 0) {
throw new GPBDecodeException("Int64 too large");
}
if (bccomp($value, "-9223372036854775808") < 0) {
throw new GPBDecodeException("Int64 too small");
}
return $value;
case GPBType::UINT64:
case GPBType::FIXED64:
if (is_null($value)) {
return $this->defaultValue($field);
}
if (!is_numeric($value)) {
throw new GPBDecodeException("Invalid data type for int64 field");
}
if (is_string($value) && trim($value) !== $value) {
throw new GPBDecodeException("Invalid data type for int64 field");
}
if (bccomp($value, "18446744073709551615") > 0) {
throw new GPBDecodeException("Uint64 too large");
}
if (bccomp($value, "9223372036854775807") > 0) {
$value = bcsub($value, "18446744073709551616");
}
return $value;
default:
return $value;
}
}