Skip to content

Commit

Permalink
#9: updated Textarea and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisBirkholz committed Aug 31, 2017
1 parent 59ab7c6 commit 5293848
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 0 deletions.
176 changes: 176 additions & 0 deletions src/Html/Textarea.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

/**
* This file is part of Replum: the web widget framework.
*
* Copyright (c) Dennis Birkholz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Replum\Html;

use \Replum\Util;

/**
* @author Dennis Birkholz <[email protected]>
* @link https://www.w3.org/TR/html5/forms.html#the-textarea-element
*/
final class Textarea extends HtmlElement implements FormInputInterface
{
use FormElementTrait;
use AutocompleteAttributeTrait;
use MinlengthMaxLengthAttributesTrait;
use PlaceholderAttributeTrait;
use ReadonlyAttributeTrait;
use RequiredAttributeTrait;

const TAG = 'textarea';

const WRAP_SOFT = 'soft';
const WRAP_HARD = 'hard';

/**
* Maximum number of characters per line
*
* @var int
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols
*/
private $cols;

/**
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols
*/
final public function getCols() : int
{
return $this->cols;
}

/**
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols
*/
final public function hasCols() : bool
{
return ($this->cols !== null);
}

/**
* @return static $this
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols
*/
final public function setCols(int $cols = null) : self
{
if ($this->cols !== $cols) {
$this->cols = $cols;
$this->setChanged(true);
}
return $this;
}

/**
* Number of lines to show
*
* @var int
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows
*/
private $rows;

/**
* Get the number of lines to show
*
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows
*/
final public function getRows() : int
{
return $this->rows;
}

/**
* Check whether the number of lines to show is set
*
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows
*/
final public function hasRows() : bool
{
return ($this->rows !== null);
}

/**
* Set the number of lines to show
*
* @return static $this
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows
*/
final public function setRows(int $rows = null) : self
{
if ($this->rows !== $rows) {
$this->rows = $rows;
$this->setChanged(true);
}
return $this;
}

/**
* How the value of the form control is to be wrapped for form submission
*
* @var WRAP_SOFT|WRAP_HARD
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap
*/
private $wrap;

/**
* Get how the value of the form control is to be wrapped for form submission
*
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap
*/
final public function getWrap() : string
{
return $this->wrap;
}

/**
* Check whether how the value of the form control is to be wrapped for form submission is set
*
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap
*/
final public function hasWrap() : bool
{
return ($this->wrap !== null);
}

/**
* Set how the value of the form control is to be wrapped for form submission
*
* @return static $this
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap
*/
final public function setWrap(string $wrap = null) : self
{
if ($wrap !== Textarea::WRAP_SOFT && $wrap !== Textarea::WRAP_HARD && $wrap !== null) {
throw new \InvalidArgumentException('Valid values for wrap are: ' . Textarea::class . '::WRAP_SOFT, ' . Textarea::class . '::WRAP_HARD and NULL!');
}

if ($this->wrap !== $wrap) {
$this->wrap = $wrap;
$this->setChanged(true);
}

return $this;
}

protected function renderAttributes() : string
{
return parent::renderAttributes()
. $this->renderFormElementAttributes()
. $this->renderAutocompleteAttribute()
. $this->renderMinlengthMaxlengthAttributes()
. $this->renderPlaceholderAttribute()
. $this->renderReadonlyAttribute()
. $this->renderRequiredAttribute()
. Util::renderHtmlAttribute('cols', $this->cols)
. Util::renderHtmlAttribute('rows', $this->rows)
. Util::renderHtmlAttribute('wrap', $this->wrap)
;
}
}
8 changes: 8 additions & 0 deletions src/HtmlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,14 @@ final public static function text(PageInterface $page, string $text = null) : Ht
return $obj;
}

/**
* Create an Textarea element
*/
final public static function textarea(PageInterface $page) : Html\Textarea
{
return new Html\Textarea($page);
}

/**
* Create an Input[type=text] element
*/
Expand Down
48 changes: 48 additions & 0 deletions tests/Html/TextareaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* This file is part of Replum: the web widget framework.
*
* Copyright (c) Dennis Birkholz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Replum\Html;

use \PHPUnit\Framework\TestCase;
use \Replum\Context;
use \Replum\HtmlFactory;
use Symfony\Component\Console\Tests\Input\InputTest;

class TextareaTest extends HtmlTestBase
{
/**
* @return Textarea
*/
protected function factory() : HtmlElement
{
return HtmlFactory::textarea($this->page);
}

protected $attributes = [
// Common attributes
"autofocus" => true,
"disabled" => true,
"form" => null,
"name" => ["foo", "bar"],
"value" => ["foo", "bar"],

"autocomplete" => [Input::AUTOCOMPLETE_ON, Input::AUTOCOMPLETE_OFF],
"cols" => [20, 10],
"dirname" => null,
"inputmode" => null,
"maxlength" => [20, 10],
"minlength" => [10, 20],
"placeholder" => ["placeholder I am", "Stupid text"],
"readonly" => true,
"required" => true,
"rows" => [5, 10],
"wrap" => [Textarea::WRAP_SOFT, Textarea::WRAP_HARD],
];
}

0 comments on commit 5293848

Please sign in to comment.