Skip to content

Commit

Permalink
Add image widget. (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 15, 2024
1 parent a170719 commit d3fb834
Show file tree
Hide file tree
Showing 7 changed files with 540 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Attribute/Input/HasFormmethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function formmethod(string $value): static
}

$new = clone $this;
$new->attributes['formn-method'] = $value;
$new->attributes['formmethod'] = $value;

return $new;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/Input/HasSrc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ trait HasSrc
* Set the source attribute valid for the image input button or img only, the src is string specifying the URL of
* the image file to display to represent the graphical submit button.
*
* @param string $value The source of the widget.
* @param string|null $value The source of the widget.
*
* @return static A new instance of the current class with the specified source value.
*
* @link https://www.w3.org/TR/html52/embedded-content-0.html#attr-img-srcset
*/
public function src(string $value): static
public function src(string $value = null): static
{
$new = clone $this;
$new->attributes['src'] = $value;
Expand Down
41 changes: 41 additions & 0 deletions src/Input/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Input;

use PHPForge\Html\Attribute;

/**
* The input element with a type attribute whose value is "image" represents either an image from which the UA enables a
* user to interactively select a pair of coordinates and submit the form, or alternatively a button from which the user
* can submit the form.
*
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.image.html#input.image
*/
final class Image extends Base\AbstractInput
{
use Attribute\Custom\HasWidgetValidation;
use Attribute\Input\HasAlt;
use Attribute\Input\HasFormaction;
use Attribute\Input\HasFormenctype;
use Attribute\Input\HasFormmethod;
use Attribute\Input\HasFormnovalidate;
use Attribute\Input\HasFormtarget;
use Attribute\Input\HasHeight;
use Attribute\Input\HasSrc;
use Attribute\Input\HasWidth;

protected function run(): string
{
$attributes = $this->attributes;
$value = $attributes['src'] ?? $this->getValue();
$this->validateString($value);

unset($attributes['value']);

$attributes['src'] = $value;

return $this->buildInputTag($attributes, 'image');
}
}
2 changes: 1 addition & 1 deletion tests/Attribute/Input/HasSrcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function testImmutability(): void
protected array $attributes = [];
};

$this->assertNotSame($instance, $instance->src(''));
$this->assertNotSame($instance, $instance->src(null));
}
}
23 changes: 23 additions & 0 deletions tests/Input/Image/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\Image;

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

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

Image::widget()->value(1)->render();
}
}

0 comments on commit d3fb834

Please sign in to comment.