aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/src/Google/Protobuf/Internal/GPBUtil.php
diff options
context:
space:
mode:
authorGravatar Paul Yang <TeBoring@users.noreply.github.com>2017-03-10 13:42:59 -0800
committerGravatar GitHub <noreply@github.com>2017-03-10 13:42:59 -0800
commit616e68ecc1c997c8b4c22a708cc9f6605a329bef (patch)
tree423b87d463238652d667bedd4791314d869045e0 /php/src/Google/Protobuf/Internal/GPBUtil.php
parenta1bb147e96b6f74db6cdf3c3fcb00492472dbbfa (diff)
Repeated/Map field setter should accept a regular PHP array (#2817)
Accept regular PHP array for repeated/map setter. Existing map/repeated field will be swapped by a clean map/repeated field. Then, elements in the array will be added to the map/repeated field. All elements will be type checked before adding. See #2686 for detail.
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;
}
}