Skip to content

Commit 6b60a7f

Browse files
Add method render() in CssClass::class. (#6)
1 parent c55c2d6 commit 6b60a7f

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Change Log
22

3-
## 0.1.2 Under development
3+
## 0.1.2 March 25, 2024
4+
5+
- Bug #6: Add method `render()` in `CssClass::class` (@terabytesoftw)
46

57
## 0.1.1 March 9, 2024
68

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,30 @@ $attributes = Attributes::render(
311311
);
312312
```
313313

314+
### Render HMTL class attribute
315+
316+
The `CssClass::class` helper can be used to render the `class` attribute.
317+
318+
The method accepts one parameter:
319+
320+
- `class:` (string): The class to render.
321+
- `baseClass:` (string): The base class to use.
322+
- `inList:` (array): The list of classes to validate.
323+
324+
```php
325+
<?php
326+
327+
declare(strict_types=1);
328+
329+
use UIAwesome\Html\Helper\CssClass;
330+
331+
$class = CssClass::render(
332+
'yellow',
333+
'p-4 mb-4 text-sm text-%1$s-800 rounded-lg bg-%1$s-50 dark:bg-gray-800 dark:text-%1$s-400',
334+
['blue', 'gray', 'green', 'red', 'yellow'],
335+
);
336+
```
337+
314338
### Render Template
315339

316340
The `Template::class` helper can be used to render a template.

src/CssClass.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use function is_array;
1010
use function is_int;
1111
use function preg_split;
12+
use function sprintf;
1213

1314
/**
1415
* This class provides static methods for managing CSS classes.
@@ -72,6 +73,32 @@ public static function add(array &$attributes, array|string $classes, bool $over
7273
$attributes['class'] = implode(' ', $classArray);
7374
}
7475

76+
/**
77+
* This method, dynamically generates CSS classes based on the provided class parameter and a template string.
78+
*
79+
* The template string should contain a placeholder for the class value.
80+
*
81+
* @param string $class The class value.
82+
* @param string $baseClass The template string. The placeholder for the class value should be represented by "%s".
83+
* @param string[] $inList The list of classes to validate.
84+
*
85+
* @return string The generated CSS class.
86+
*/
87+
public static function render(string $class, string $baseClass, array $inList): string
88+
{
89+
if (in_array($class, $inList, true) === false) {
90+
throw new \InvalidArgumentException(
91+
sprintf(
92+
'Invalid value: "%s". Available values: "%s".',
93+
$class,
94+
implode('", "', $inList)
95+
)
96+
);
97+
}
98+
99+
return sprintf($baseClass, $class);
100+
}
101+
75102
/**
76103
* Merges already existing CSS classes with new one.
77104
*

tests/CssClassTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,30 @@ public function testMergeMethodAssignToKey()
105105
$this->assertArrayHasKey('keyed-class', $merged);
106106
$this->assertEquals('new-class', $merged['keyed-class']);
107107
}
108+
109+
public function testRender(): void
110+
{
111+
$this->assertSame(
112+
'p-4 mb-4 text-sm text-yellow-800 rounded-lg bg-yellow-50 dark:bg-gray-800 dark:text-yellow-400',
113+
CssClass::render(
114+
'yellow',
115+
'p-4 mb-4 text-sm text-%1$s-800 rounded-lg bg-%1$s-50 dark:bg-gray-800 dark:text-%1$s-400',
116+
['blue', 'gray', 'green', 'red', 'yellow'],
117+
)
118+
);
119+
}
120+
121+
public function testRenderWithInvalidValue(): void
122+
{
123+
$this->expectException(\InvalidArgumentException::class);
124+
$this->expectExceptionMessage(
125+
'Invalid value: "indigo". Available values: "blue", "gray", "green", "red", "yellow".'
126+
);
127+
128+
CssClass::render(
129+
'indigo',
130+
'p-4 mb-4 text-sm text-%1$s-800 rounded-lg bg-%1$s-50 dark:bg-gray-800 dark:text-%1$s-400',
131+
['blue', 'gray', 'green', 'red', 'yellow'],
132+
);
133+
}
108134
}

0 commit comments

Comments
 (0)