Skip to content

Commit da09908

Browse files
committed
refactor(helper): Enhance DotCmsHelper methods for better data handling
- Added validation to ensure containers are arrays before accessing. - Improved HTML attribute generation by safely converting values to strings. - Refined title extraction logic to handle various content structures and ensure default values are set correctly.
1 parent d551177 commit da09908

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

src/Utils/DotCmsHelper.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public static function getContainerData(array $containers, array $container): ?a
2626
return null;
2727
}
2828

29+
if (!is_array($containers[$identifier])) {
30+
return null;
31+
}
32+
2933
return $containers[$identifier];
3034
}
3135

@@ -49,7 +53,17 @@ public static function htmlAttributes(array $attributes): string
4953
$html .= ' ' . $key;
5054
}
5155
} else {
52-
$html .= ' ' . $key . '="' . htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8') . '"';
56+
// Convert value to string safely
57+
if (is_scalar($value)) {
58+
$stringValue = (string)$value;
59+
} elseif (is_null($value)) {
60+
$stringValue = '';
61+
} else {
62+
$encoded = json_encode($value);
63+
$stringValue = $encoded !== false ? $encoded : '[complex value]';
64+
}
65+
66+
$html .= ' ' . $key . '="' . htmlspecialchars($stringValue, ENT_QUOTES, 'UTF-8') . '"';
5367
}
5468
}
5569

@@ -68,10 +82,22 @@ public static function simpleContentHtml(array $content): string
6882
return '';
6983
}
7084

71-
$title = $content['title'] ?? $content['name'] ?? 'No Title';
85+
$title = '';
86+
if (isset($content['title']) && is_string($content['title'])) {
87+
$title = $content['title'];
88+
} elseif (isset($content['name']) && is_string($content['name'])) {
89+
$title = $content['name'];
90+
} else {
91+
$title = 'No Title';
92+
}
93+
94+
$contentType = 'unknown';
95+
if (isset($content['contentType']) && is_string($content['contentType'])) {
96+
$contentType = $content['contentType'];
97+
}
7298

7399
return '<div class="dotcms-content" data-content-type="' .
74-
htmlspecialchars($content['contentType'] ?? 'unknown', ENT_QUOTES, 'UTF-8') .
100+
htmlspecialchars($contentType, ENT_QUOTES, 'UTF-8') .
75101
'"><h3>' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</h3></div>';
76102
}
77103
}

0 commit comments

Comments
 (0)