Skip to content

Commit 9b64d8d

Browse files
committed
feat(helper): Introduce DotCmsHelper for streamlined content operations
- Added DotCmsHelper class with methods for fetching container data, generating HTML attributes, and rendering simple HTML. - Refactored DotCmsHelpers and DotCMSExtension to utilize DotCmsHelper methods for improved code efficiency and maintainability. - Created unit tests for DotCmsHelper to ensure functionality and reliability.
1 parent ea3afe0 commit 9b64d8d

4 files changed

Lines changed: 202 additions & 41 deletions

File tree

examples/dotcms-laravel/app/Helpers/DotCmsHelpers.php

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Helpers;
44

5+
use Dotcms\PhpSdk\Utils\DotCmsHelper;
6+
57
class DotCmsHelpers
68
{
79
/**
@@ -13,17 +15,7 @@ class DotCmsHelpers
1315
*/
1416
public function getContainersData($containers, $container)
1517
{
16-
if (empty($containers) || empty($container)) {
17-
return null;
18-
}
19-
20-
$identifier = $container['identifier'] ?? null;
21-
22-
if (!$identifier || !isset($containers[$identifier])) {
23-
return null;
24-
}
25-
26-
return $containers[$identifier];
18+
return DotCmsHelper::getContainerData($containers, $container);
2719
}
2820

2921
/**
@@ -34,23 +26,7 @@ public function getContainersData($containers, $container)
3426
*/
3527
public function htmlAttr($attributes)
3628
{
37-
if (empty($attributes)) {
38-
return '';
39-
}
40-
41-
$html = '';
42-
43-
foreach ($attributes as $key => $value) {
44-
if (is_bool($value)) {
45-
if ($value) {
46-
$html .= ' ' . $key;
47-
}
48-
} else {
49-
$html .= ' ' . $key . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
50-
}
51-
}
52-
53-
return $html;
29+
return DotCmsHelper::htmlAttributes($attributes);
5430
}
5531

5632
/**
@@ -74,8 +50,7 @@ public function generateHtmlBasedOnProperty($content)
7450
}
7551
}
7652

77-
// Default rendering with title
78-
$title = $content['title'] ?? $content['name'] ?? 'No Title';
79-
return '<div class="content-wrapper"><h3>' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</h3></div>';
53+
// Fall back to the SDK simple HTML renderer
54+
return DotCmsHelper::simpleContentHtml($content);
8055
}
8156
}

examples/dotcms-symfony/src/Twig/DotCMSExtension.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Twig;
44

5+
use Dotcms\PhpSdk\Utils\DotCmsHelper;
56
use Twig\Environment;
67
use Twig\Extension\AbstractExtension;
78
use Twig\TwigFunction;
@@ -27,13 +28,7 @@ public function getFunctions(): array
2728

2829
public function htmlAttr(array $attrs): string
2930
{
30-
return implode(' ', array_map(
31-
fn($key, $value) => is_bool($value)
32-
? sprintf('%s="%s"', $key, $value ? 'true' : 'false')
33-
: sprintf('%s="%s"', $key, htmlspecialchars((string)$value, ENT_QUOTES)),
34-
array_keys($attrs),
35-
$attrs
36-
));
31+
return DotCmsHelper::htmlAttributes($attrs);
3732
}
3833

3934
public function getGridClass(int $position, string $type = 'start'): string
@@ -60,22 +55,30 @@ public function generateHtmlBasedOnProperty(array $content): string
6055
};
6156

6257
if (empty($template)) {
63-
return '';
58+
// Fall back to the SDK simple content HTML renderer if no template is found
59+
return DotCmsHelper::simpleContentHtml($content);
6460
}
6561

6662
try {
6763
return $twig->render($template, ['content' => $content]);
6864
} catch (\Exception $e) {
69-
return '';
65+
// Fall back to the SDK simple content HTML renderer if rendering fails
66+
return DotCmsHelper::simpleContentHtml($content);
7067
}
7168
}
7269

7370
public function getContainersData(array $containers, array $containerRef): array
7471
{
72+
// First try to get the container data using the SDK helper
73+
$containerData = DotCmsHelper::getContainerData($containers, $containerRef);
74+
75+
if (!$containerData) {
76+
throw new RuntimeException("Container not found: " . ($containerRef['identifier'] ?? 'unknown'));
77+
}
78+
7579
$identifier = $containerRef['identifier'] ?? throw new RuntimeException("Missing container identifier");
7680
$uuid = $containerRef['uuid'] ?? throw new RuntimeException("Missing container UUID");
7781

78-
$containerData = $containers[$identifier] ?? throw new RuntimeException("Container not found: $identifier");
7982
$structures = $containerData['containerStructures'] ?? [];
8083
$container = $containerData['container'] ?? [];
8184

src/Utils/DotCmsHelper.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Dotcms\PhpSdk\Utils;
4+
5+
/**
6+
* Helper class for dotCMS content operations
7+
*/
8+
class DotCmsHelper
9+
{
10+
/**
11+
* Get container data from the containers array
12+
*
13+
* @param array<string, mixed> $containers Array of containers indexed by identifier
14+
* @param array<string, mixed> $container Container reference with identifier
15+
* @return array<string, mixed>|null The container data or null if not found
16+
*/
17+
public static function getContainerData(array $containers, array $container): ?array
18+
{
19+
if (empty($containers) || empty($container)) {
20+
return null;
21+
}
22+
23+
$identifier = $container['identifier'] ?? null;
24+
25+
if (! $identifier || ! isset($containers[$identifier])) {
26+
return null;
27+
}
28+
29+
return $containers[$identifier];
30+
}
31+
32+
/**
33+
* Generate HTML attributes from an associative array
34+
*
35+
* @param array<string, mixed> $attributes Array of attribute names and values
36+
* @return string HTML attributes string
37+
*/
38+
public static function htmlAttributes(array $attributes): string
39+
{
40+
if (empty($attributes)) {
41+
return '';
42+
}
43+
44+
$html = '';
45+
46+
foreach ($attributes as $key => $value) {
47+
if (is_bool($value)) {
48+
if ($value) {
49+
$html .= ' ' . $key;
50+
}
51+
} else {
52+
$html .= ' ' . $key . '="' . htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8') . '"';
53+
}
54+
}
55+
56+
return $html;
57+
}
58+
59+
/**
60+
* Default rendering for content when a framework-specific implementation is not available
61+
*
62+
* @param array<string, mixed> $content Content data
63+
* @return string Simple HTML representation of the content
64+
*/
65+
public static function simpleContentHtml(array $content): string
66+
{
67+
if (empty($content)) {
68+
return '';
69+
}
70+
71+
$title = $content['title'] ?? $content['name'] ?? 'No Title';
72+
73+
return '<div class="dotcms-content" data-content-type="' .
74+
htmlspecialchars($content['contentType'] ?? 'unknown', ENT_QUOTES, 'UTF-8') .
75+
'"><h3>' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</h3></div>';
76+
}
77+
}

tests/Utils/DotCmsHelperTest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Dotcms\PhpSdk\Tests\Utils;
4+
5+
use Dotcms\PhpSdk\Utils\DotCmsHelper;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DotCmsHelperTest extends TestCase
9+
{
10+
public function testGetContainerData(): void
11+
{
12+
$containers = [
13+
'abc123' => ['title' => 'Test Container'],
14+
'def456' => ['title' => 'Another Container'],
15+
];
16+
17+
$container = ['identifier' => 'abc123'];
18+
$result = DotCmsHelper::getContainerData($containers, $container);
19+
$this->assertEquals(['title' => 'Test Container'], $result);
20+
21+
// Test with non-existent identifier
22+
$container = ['identifier' => 'nonexistent'];
23+
$result = DotCmsHelper::getContainerData($containers, $container);
24+
$this->assertNull($result);
25+
26+
// Test with empty inputs
27+
$this->assertNull(DotCmsHelper::getContainerData([], []));
28+
$this->assertNull(DotCmsHelper::getContainerData($containers, []));
29+
$this->assertNull(DotCmsHelper::getContainerData([], $container));
30+
}
31+
32+
public function testHtmlAttributes(): void
33+
{
34+
// Test with regular attributes
35+
$attributes = [
36+
'id' => 'test-id',
37+
'class' => 'test-class',
38+
'data-test' => 'value',
39+
];
40+
$expected = ' id="test-id" class="test-class" data-test="value"';
41+
$this->assertEquals($expected, DotCmsHelper::htmlAttributes($attributes));
42+
43+
// Test with boolean attributes
44+
$attributes = [
45+
'disabled' => true,
46+
'readonly' => false,
47+
];
48+
$expected = ' disabled';
49+
$this->assertEquals($expected, DotCmsHelper::htmlAttributes($attributes));
50+
51+
// Test with mixed attributes
52+
$attributes = [
53+
'id' => 'test-id',
54+
'disabled' => true,
55+
'readonly' => false,
56+
];
57+
$expected = ' id="test-id" disabled';
58+
$this->assertEquals($expected, DotCmsHelper::htmlAttributes($attributes));
59+
60+
// Test with special characters
61+
$attributes = [
62+
'data-content' => 'A "quoted" & <special> string',
63+
];
64+
$expected = ' data-content="A &quot;quoted&quot; &amp; &lt;special&gt; string"';
65+
$this->assertEquals($expected, DotCmsHelper::htmlAttributes($attributes));
66+
67+
// Test with empty input
68+
$this->assertEquals('', DotCmsHelper::htmlAttributes([]));
69+
}
70+
71+
public function testSimpleContentHtml(): void
72+
{
73+
// Test with title
74+
$content = [
75+
'contentType' => 'Article',
76+
'title' => 'Test Title',
77+
];
78+
$expected = '<div class="dotcms-content" data-content-type="Article"><h3>Test Title</h3></div>';
79+
$this->assertEquals($expected, DotCmsHelper::simpleContentHtml($content));
80+
81+
// Test with name but no title
82+
$content = [
83+
'contentType' => 'Page',
84+
'name' => 'Test Name',
85+
];
86+
$expected = '<div class="dotcms-content" data-content-type="Page"><h3>Test Name</h3></div>';
87+
$this->assertEquals($expected, DotCmsHelper::simpleContentHtml($content));
88+
89+
// Test with no title or name
90+
$content = [
91+
'contentType' => 'Widget',
92+
];
93+
$expected = '<div class="dotcms-content" data-content-type="Widget"><h3>No Title</h3></div>';
94+
$this->assertEquals($expected, DotCmsHelper::simpleContentHtml($content));
95+
96+
// Test with no content type
97+
$content = [
98+
'title' => 'Test Title',
99+
];
100+
$expected = '<div class="dotcms-content" data-content-type="unknown"><h3>Test Title</h3></div>';
101+
$this->assertEquals($expected, DotCmsHelper::simpleContentHtml($content));
102+
103+
// Test with empty content
104+
$this->assertEquals('', DotCmsHelper::simpleContentHtml([]));
105+
}
106+
}

0 commit comments

Comments
 (0)