Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## 0.1.2 Under development
## 0.1.2 March 25, 2024

- Bug #6: Add method `render()` in `CssClass::class` (@terabytesoftw)

## 0.1.1 March 9, 2024

Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,30 @@ $attributes = Attributes::render(
);
```

### Render HMTL class attribute

The `CssClass::class` helper can be used to render the `class` attribute.

The method accepts one parameter:

- `class:` (string): The class to render.
- `baseClass:` (string): The base class to use.
- `inList:` (array): The list of classes to validate.

```php
<?php

declare(strict_types=1);

use UIAwesome\Html\Helper\CssClass;

$class = CssClass::render(
'yellow',
'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',
['blue', 'gray', 'green', 'red', 'yellow'],
);
```

### Render Template

The `Template::class` helper can be used to render a template.
Expand Down
27 changes: 27 additions & 0 deletions src/CssClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function is_array;
use function is_int;
use function preg_split;
use function sprintf;

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

/**
* This method, dynamically generates CSS classes based on the provided class parameter and a template string.
*
* The template string should contain a placeholder for the class value.
*
* @param string $class The class value.
* @param string $baseClass The template string. The placeholder for the class value should be represented by "%s".
* @param string[] $inList The list of classes to validate.
*
* @return string The generated CSS class.
*/
public static function render(string $class, string $baseClass, array $inList): string
{
if (in_array($class, $inList, true) === false) {
throw new \InvalidArgumentException(
sprintf(
'Invalid value: "%s". Available values: "%s".',
$class,
implode('", "', $inList)
)
);
}

return sprintf($baseClass, $class);
}

/**
* Merges already existing CSS classes with new one.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/CssClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,30 @@ public function testMergeMethodAssignToKey()
$this->assertArrayHasKey('keyed-class', $merged);
$this->assertEquals('new-class', $merged['keyed-class']);
}

public function testRender(): void
{
$this->assertSame(
'p-4 mb-4 text-sm text-yellow-800 rounded-lg bg-yellow-50 dark:bg-gray-800 dark:text-yellow-400',
CssClass::render(
'yellow',
'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',
['blue', 'gray', 'green', 'red', 'yellow'],
)
);
}

public function testRenderWithInvalidValue(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(
'Invalid value: "indigo". Available values: "blue", "gray", "green", "red", "yellow".'
);

CssClass::render(
'indigo',
'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',
['blue', 'gray', 'green', 'red', 'yellow'],
);
}
}