From 4901c363c6b457e17e3818f3e49e6b1ad073b9a6 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 20 Jun 2018 17:33:27 -0700 Subject: [PATCH] (dev/core#174) NaiveMultipleTrait - Throw errors for bad inputs --- CRM/Utils/Cache/NaiveMultipleTrait.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CRM/Utils/Cache/NaiveMultipleTrait.php b/CRM/Utils/Cache/NaiveMultipleTrait.php index b1f37a4490..ac7e4dedb8 100644 --- a/CRM/Utils/Cache/NaiveMultipleTrait.php +++ b/CRM/Utils/Cache/NaiveMultipleTrait.php @@ -51,6 +51,8 @@ trait CRM_Utils_Cache_NaiveMultipleTrait { * or if any of the $keys are not a legal value. */ public function getMultiple($keys, $default = NULL) { + $this->assertIterable('getMultiple', $keys); + $result = []; foreach ($keys as $key) { $result[$key] = $this->get($key, $default); @@ -73,8 +75,13 @@ trait CRM_Utils_Cache_NaiveMultipleTrait { * or if any of the $values are not a legal value. */ public function setMultiple($values, $ttl = NULL) { + $this->assertIterable('setMultiple', $values); + $result = TRUE; foreach ($values as $key => $value) { + if (is_int($key)) { + $key = (string) $key; + } $result = $this->set($key, $value, $ttl) || $result; } return $result; @@ -92,6 +99,8 @@ trait CRM_Utils_Cache_NaiveMultipleTrait { * or if any of the $keys are not a legal value. */ public function deleteMultiple($keys) { + $this->assertIterable('deleteMultiple', $keys); + $result = TRUE; foreach ($keys as $key) { $result = $this->delete($key) || $result; @@ -99,4 +108,14 @@ trait CRM_Utils_Cache_NaiveMultipleTrait { return $result; } + /** + * @param $keys + * @throws \CRM_Utils_Cache_InvalidArgumentException + */ + private function assertIterable($func, $keys) { + if (!is_array($keys) || $keys instanceof Traversable) { + throw new CRM_Utils_Cache_InvalidArgumentException("$func expects iterable input"); + } + } + } -- 2.25.1