ソースを参照

Correcting trimDeep

Correcting trimDeep to only work on array, null, or string. And to return the value if it does not match any of those conditions.
Chris Hallgren 6 年 前
コミット
d5fb653c59
2 ファイル変更16 行追加6 行削除
  1. 6 4
      src/Utility/Utility.php
  2. 10 2
      tests/TestCase/Utility/UtilityTest.php

+ 6 - 4
src/Utility/Utility.php

@@ -441,7 +441,7 @@ class Utility {
 	 *
 	 *
 	 * @param string|array|null $value
 	 * @param string|array|null $value
 	 * @param bool $transformNullToString
 	 * @param bool $transformNullToString
-	 * @return array|string
+	 * @return mixed
 	 */
 	 */
 	public static function trimDeep($value, $transformNullToString = false) {
 	public static function trimDeep($value, $transformNullToString = false) {
 		if (is_array($value)) {
 		if (is_array($value)) {
@@ -449,11 +449,13 @@ class Utility {
 				$value[$k] = static::trimDeep($v, $transformNullToString);
 				$value[$k] = static::trimDeep($v, $transformNullToString);
 			}
 			}
 			return $value;
 			return $value;
-		} elseif (is_int($value)) {
-			return $value;
 		}
 		}
 
 
-		return ($value === null && !$transformNullToString) ? $value : trim($value);
+		if (is_string($value) || is_null($value)) {
+			return ($value === null && !$transformNullToString) ? $value : trim($value);
+		}
+
+		return $value;
 	}
 	}
 
 
 	/**
 	/**

+ 10 - 2
tests/TestCase/Utility/UtilityTest.php

@@ -339,14 +339,22 @@ class UtilityTest extends TestCase {
 			'e 49r ' => 'rf r ',
 			'e 49r ' => 'rf r ',
 			'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]],
 			'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]],
 			'bsh' => 1,
 			'bsh' => 1,
-			'bkd' => '1'
+			'bkd' => '1',
+			'bol' => true,
+			'bl' => 'true',
+			'flt' => 3.14,
+			'fl' => '3.14'
 		];
 		];
 		$expected = [
 		$expected = [
 			'f some',
 			'f some',
 			'e 49r ' => 'rf r',
 			'e 49r ' => 'rf r',
 			'er' => [['ee' => ['rr ' => 'tt', 'empty' => null]]],
 			'er' => [['ee' => ['rr ' => 'tt', 'empty' => null]]],
 			'bsh' => 1,
 			'bsh' => 1,
-			'bkd' => '1'
+			'bkd' => '1',
+			'bol' => true,
+			'bl' => 'true',
+			'flt' => 3.14,
+			'fl' => '3.14'
 		];
 		];
 
 
 		$res = Utility::trimDeep($is);
 		$res = Utility::trimDeep($is);