Skip to content

Commit 6d3bbce

Browse files
authored
Merge pull request #178 from dereuromark/bugfix
Fix trimDeep()
2 parents 68e4b0a + d237998 commit 6d3bbce

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
"issues": "https://github.com/dereuromark/cakephp-tools/issues"
4343
},
4444
"scripts": {
45-
"setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar",
4645
"test": "php phpunit.phar",
46+
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar",
47+
"test-coverage": "php phpunit.phar --log-junit webroot/coverage/unitreport.xml --coverage-html webroot/coverage --coverage-clover webroot/coverage/coverage.xml",
4748
"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 ./",
4849
"cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/cakephp-tools/vendor/,/tmp/,/logs/,/tests/test_files --extensions=php ./"
4950
}

src/Utility/Utility.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public static function pregMatch($pattern, $subject, $flags = null, $offset = nu
129129
*/
130130
public static function strSplit($str, $length = 1) {
131131
if ($length < 1) {
132-
return false;
132+
return [];
133133
}
134134
$result = [];
135135
$c = mb_strlen($str);
@@ -406,18 +406,25 @@ public static function typeCast($value, $type) {
406406
/**
407407
* Trim recursively
408408
*
409-
* @param mixed $value
409+
* @param string|array|null $value
410+
* @param bool $transformNullToString
410411
* @return array|string
411412
*/
412-
public static function trimDeep($value) {
413-
$value = is_array($value) ? array_map('self::trimDeep', $value) : trim($value);
414-
return $value;
413+
public static function trimDeep($value, $transformNullToString = false) {
414+
if (is_array($value)) {
415+
foreach ($value as $k => $v) {
416+
$value[$k] = static::trimDeep($v, $transformNullToString);
417+
}
418+
return $value;
419+
}
420+
421+
return ($value === null && !$transformNullToString) ? $value : trim($value);
415422
}
416423

417424
/**
418425
* Applies h() recursively
419426
*
420-
* @param mixed $value
427+
* @param string|array $value
421428
* @return array|string
422429
*/
423430
public static function specialcharsDeep($value) {

tests/TestCase/Utility/UtilityTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,38 @@ public function testDeep() {
291291
$is = [
292292
'f some',
293293
'e 49r ' => 'rf r ',
294-
'er' => [['ee' => ['rr ' => ' tt ']]]
294+
'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]]
295295
];
296296
$expected = [
297297
'f some',
298298
'e 49r ' => 'rf r',
299-
'er' => [['ee' => ['rr ' => 'tt']]]
299+
'er' => [['ee' => ['rr ' => 'tt', 'empty' => null]]]
300300
];
301301

302302
$res = Utility::trimDeep($is);
303303
$this->assertSame($expected, $res);
304304
}
305305

306+
/**
307+
* @covers ::trimDeep
308+
* @return void
309+
*/
310+
public function testDeepTransformNullToString() {
311+
$is = [
312+
'f some',
313+
'e 49r ' => 'rf r ',
314+
'er' => [['ee' => ['rr ' => ' tt ', 'empty' => null]]]
315+
];
316+
$expected = [
317+
'f some',
318+
'e 49r ' => 'rf r',
319+
'er' => [['ee' => ['rr ' => 'tt', 'empty' => '']]]
320+
];
321+
322+
$res = Utility::trimDeep($is, true);
323+
$this->assertSame($expected, $res);
324+
}
325+
306326
/**
307327
* //TODO
308328
*

0 commit comments

Comments
 (0)