Skip to content

Commit

Permalink
Fix type error into String helper when argument is not a string
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed Jun 12, 2024
1 parent dfbde55 commit 3d55f6a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/Helper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,37 @@ public static function parseArray(array $values): string {

$output = [];

$callback = function($v): ?string {

if (true === is_bool($v)) {
return static::parseBoolean($v);
}

if (true === is_numeric($v)) {
return "$v";
}

if (true === is_string($v)) {
return trim($v);
}

return $v;
};

foreach ($values as $key => $value) {

if (null === $value) {
continue;
}

if (true === is_array($value)) {
$buffer = trim(implode(" ", $value));
} elseif (true === is_bool($value)) {
$buffer = static::parseBoolean($value);
} elseif (true === is_numeric($value)) {
$buffer = "$value";
$buffer = trim(implode(" ", array_map($callback, $value)));
} else {
$buffer = trim($value);
$buffer = $callback($value);
}

if ("" !== $buffer) {
$output[] = $key . '="' . preg_replace("/\s+/", " ", $buffer) . '"';
$output[] = $callback($key) . '="' . preg_replace("/\s+/", " ", $buffer) . '"';
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/Helper/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@ public function testParseArray(): void {
"exception" => null,
"id" => "id",
"class" => [
"class1",
"class2",
"class3 class4",
null,
0.1,
1,
true,
"class1 class2",
],
"data-type-boolean" => true,
"data-type-float" => 0.1,
"data-type-integer" => 1,
];
$exp = 'id="id" class="class1 class2 class3 class4" data-type-boolean="true" data-type-float="0.1" data-type-integer="1"';
$exp = 'id="id" class="0.1 1 true class1 class2" data-type-boolean="true" data-type-float="0.1" data-type-integer="1"';

$this->assertEquals($exp, StringHelper::parseArray($arg));
}
Expand Down

0 comments on commit 3d55f6a

Please sign in to comment.