Skip to content

Commit 1f6a365

Browse files
committed
Fix trim values for meta data
1 parent d05971a commit 1f6a365

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

includes/helper.php

+51-28
Original file line numberDiff line numberDiff line change
@@ -226,34 +226,7 @@ public function set_metadata($data = [])
226226
// Map values to correct properties
227227
foreach ($data as $key => $value)
228228
{
229-
if (is_string($value))
230-
{
231-
$value = trim($value);
232-
}
233-
else if (is_array($value))
234-
{
235-
$value = array_map(function($val) {
236-
if (is_string($val))
237-
{
238-
return trim($val);
239-
}
240-
241-
if (is_array($val))
242-
{
243-
foreach ($val as $k => $v)
244-
{
245-
if (!is_string($v))
246-
{
247-
continue;
248-
}
249-
250-
$val[$k] = trim($v);
251-
}
252-
253-
return $val;
254-
}
255-
}, $value);
256-
}
229+
$value = $this->trim_items($value);
257230

258231
switch ($key)
259232
{
@@ -1411,6 +1384,56 @@ public function is_wide_image($width = 0, $height = 0)
14111384
return $is_wide;
14121385
}
14131386

1387+
/**
1388+
* Trim strings from given data, recursively.
1389+
*
1390+
* @param mixed $data
1391+
* @param integer $depth
1392+
*
1393+
* @return mixed
1394+
*/
1395+
public function trim_items($data = [], $depth = 0)
1396+
{
1397+
if (empty($data))
1398+
{
1399+
return [];
1400+
}
1401+
1402+
$max_depth = 5;
1403+
$depth = abs($depth) + 1;
1404+
1405+
// Do not go deeper, return data as is
1406+
if ($depth > $max_depth)
1407+
{
1408+
return $data;
1409+
}
1410+
1411+
if (!is_string($data) && !is_array($data))
1412+
{
1413+
return $data;
1414+
}
1415+
1416+
if (is_string($data))
1417+
{
1418+
return trim($data);
1419+
}
1420+
1421+
// Trim strings
1422+
return array_map(function($item) use ($depth) {
1423+
if (is_string($item))
1424+
{
1425+
return trim($item);
1426+
}
1427+
1428+
if (is_array($item))
1429+
{
1430+
return $this->trim_items($item, $depth);
1431+
}
1432+
1433+
return $item;
1434+
}, $data);
1435+
}
1436+
14141437
/**
14151438
* Remove empty items from an array, recursively.
14161439
*

phpunit.xml.dist

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
backupStaticAttributes="true"
44
bootstrap="../../../../tests/bootstrap.php"
55
colors="true"
6-
convertErrorsToExceptions="false"
7-
convertNoticesToExceptions="false"
8-
convertWarningsToExceptions="false"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnError="false"
1111
stopOnFailure="false"

0 commit comments

Comments
 (0)