Skip to content

Commit

Permalink
Add Color input element and ExceptionTest class. (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 21, 2023
1 parent b3f1923 commit 77cfd82
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/Input/Base/AbstractColor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Input\Base;

use InvalidArgumentException;

use function is_string;

abstract class AbstractColor extends AbstractInput
{
protected string $type = 'color';

/**
* @return string the generated input tag.
*/
protected function run(): string
{
$attributes = $this->attributes;
$value = $attributes['value'] ?? null;

/**
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.color.html#input.color.attrs.value
*/
if ($value !== null && is_string($value) === false) {
throw new InvalidArgumentException(
sprintf('%s::class widget must be a string or null value.', static::class)
);
}

return parent::run();
}
}
3 changes: 1 addition & 2 deletions src/Input/Base/AbstractHidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace PHPForge\Html\Input\Base;

use InvalidArgumentException;
use PHPForge\Html\Helper\Encode;

use function array_key_exists;
use function is_string;
Expand Down Expand Up @@ -37,7 +36,7 @@ protected function run(): string
foreach (static::NOT_ALLOWED_ATTRIBUTES as $attribute) {
if (array_key_exists($attribute, $attributes)) {
throw new InvalidArgumentException(
sprintf('%s::class widget must not be "%s" attribute.', static::class, Encode::content($attribute))
sprintf('%s::class widget must not be "%s" attribute.', static::class, $attribute)
);
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Input/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Input;

/**
* The input element with a type attribute whose value is "color" represents a color-well control, for setting the
* element’s value to a string representing a simple color.
*
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.color.html#input.color
*/
final class Color extends Base\AbstractColor
{
}
23 changes: 23 additions & 0 deletions tests/Input/Color/ExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Input\Color;

use InvalidArgumentException;
use PHPForge\Html\Input\Color;
use PHPUnit\Framework\TestCase;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ExceptionTest extends TestCase
{
public function testValue(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Color::class widget must be a string or null value.');

Color::widget()->value([])->render();
}
}

0 comments on commit 77cfd82

Please sign in to comment.