浏览代码

Try to fix trimDeep.

mscherer 8 年之前
父节点
当前提交
b5e4a20da4
共有 3 个文件被更改,包括 34 次插入9 次删除
  1. 2 1
      composer.json
  2. 10 6
      src/Utility/Utility.php
  3. 22 2
      tests/TestCase/Utility/UtilityTest.php

+ 2 - 1
composer.json

@@ -42,8 +42,9 @@
 		"issues": "https://github.com/dereuromark/cakephp-tools/issues"
 	},
 	"scripts": {
-		"setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar",
 		"test": "php phpunit.phar",
+		"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar",
+		"test-coverage": "php phpunit.phar --log-junit webroot/coverage/unitreport.xml --coverage-html webroot/coverage --coverage-clover webroot/coverage/coverage.xml",
 		"cs-check": "vendor/bin/phpcs -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files/ --extensions=php ./",
 		"cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files --extensions=php ./"
 	}

+ 10 - 6
src/Utility/Utility.php

@@ -129,7 +129,7 @@ class Utility {
 	 */
 	public static function strSplit($str, $length = 1) {
 		if ($length < 1) {
-			return false;
+			return [];
 		}
 		$result = [];
 		$c = mb_strlen($str);
@@ -406,18 +406,22 @@ class Utility {
 	/**
 	 * Trim recursively
 	 *
-	 * @param mixed $value
+	 * @param string|array|null $value
+	 * @param bool $transformNullToString
 	 * @return array|string
 	 */
-	public static function trimDeep($value) {
-		$value = is_array($value) ? array_map('self::trimDeep', $value) : trim($value);
-		return $value;
+	public static function trimDeep($value, $transformNullToString = false) {
+		if (is_array($value)) {
+			return array_map('self::trimDeep', $value);
+		}
+
+		return ($value === null && !$transformNullToString) ? $value : trim($value);
 	}
 
 	/**
 	 * Applies h() recursively
 	 *
-	 * @param mixed $value
+	 * @param string|array $value
 	 * @return array|string
 	 */
 	public static function specialcharsDeep($value) {

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

@@ -291,12 +291,12 @@ class UtilityTest extends TestCase {
 		$is = [
 			'f some',
 			'e 49r ' => 'rf r ',
-			'er' => [['ee' => ['rr ' => ' tt ']]]
+			'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]]
 		];
 		$expected = [
 			'f some',
 			'e 49r ' => 'rf r',
-			'er' => [['ee' => ['rr ' => 'tt']]]
+			'er' => [['ee' => ['rr ' => 'tt', 'empty' => null]]]
 		];
 
 		$res = Utility::trimDeep($is);
@@ -304,6 +304,26 @@ class UtilityTest extends TestCase {
 	}
 
 	/**
+	 * @covers ::trimDeep
+	 * @return void
+	 */
+	public function testDeepTransformNullToString() {
+		$is = [
+			'f some',
+			'e 49r ' => 'rf r ',
+			'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]]
+		];
+		$expected = [
+			'f some',
+			'e 49r ' => 'rf r',
+			'er' => [['ee' => ['rr ' => 'tt', 'empty' => '']]]
+		];
+
+		$res = Utility::trimDeep($is, true);
+		$this->assertSame($expected, $res);
+	}
+
+	/**
 	 * //TODO
 	 *
 	 * @return void