aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/src/Google/Protobuf/Internal/GPBUtil.php
diff options
context:
space:
mode:
Diffstat (limited to 'php/src/Google/Protobuf/Internal/GPBUtil.php')
-rw-r--r--php/src/Google/Protobuf/Internal/GPBUtil.php68
1 files changed, 55 insertions, 13 deletions
diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php
index ba1d2eb3..0e66ae6f 100644
--- a/php/src/Google/Protobuf/Internal/GPBUtil.php
+++ b/php/src/Google/Protobuf/Internal/GPBUtil.php
@@ -34,6 +34,7 @@ namespace Google\Protobuf\Internal;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
+use Google\Protobuf\Internal\MapField;
class GPBUtil
{
@@ -175,19 +176,60 @@ class GPBUtil
public static function checkRepeatedField(&$var, $type, $klass = null)
{
- if (!$var instanceof RepeatedField) {
- trigger_error("Expect repeated field.", E_USER_ERROR);
- }
- if ($var->getType() != $type) {
- trigger_error(
- "Expect repeated field of different type.",
- E_USER_ERROR);
- }
- if ($var->getType() === GPBType::MESSAGE &&
- $var->getClass() !== $klass) {
- trigger_error(
- "Expect repeated field of different message.",
- E_USER_ERROR);
+ if (!$var instanceof RepeatedField && !is_array($var)) {
+ trigger_error("Expect array.", E_USER_ERROR);
+ }
+ if (is_array($var)) {
+ $tmp = new RepeatedField($type, $klass);
+ foreach ($var as $value) {
+ $tmp[] = $value;
+ }
+ return $tmp;
+ } else {
+ if ($var->getType() != $type) {
+ trigger_error(
+ "Expect repeated field of different type.",
+ E_USER_ERROR);
+ }
+ if ($var->getType() === GPBType::MESSAGE &&
+ $var->getClass() !== $klass) {
+ trigger_error(
+ "Expect repeated field of different message.",
+ E_USER_ERROR);
+ }
+ return $var;
+ }
+ }
+
+ public static function checkMapField(&$var, $key_type, $value_type, $klass = null)
+ {
+ if (!$var instanceof MapField && !is_array($var)) {
+ trigger_error("Expect dict.", E_USER_ERROR);
+ }
+ 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) {
+ trigger_error(
+ "Expect map field of key type.",
+ E_USER_ERROR);
+ }
+ if ($var->getValueType() != $value_type) {
+ trigger_error(
+ "Expect map field of value type.",
+ E_USER_ERROR);
+ }
+ if ($var->getValueType() === GPBType::MESSAGE &&
+ $var->getValueClass() !== $klass) {
+ trigger_error(
+ "Expect map field of different value message.",
+ E_USER_ERROR);
+ }
+ return $var;
}
}