Skip to content

Commit

Permalink
add has dom trait to remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
delboy1978uk committed Dec 26, 2016
1 parent ef63c84 commit ac30861
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -7,7 +7,7 @@ A super easy Bootstrap ready HTML form generator for PHP
```
composer require delboy1978uk/form
```
## Usage
## Usage
Firstly, "use" all the classes you'll need for your form. Then create your form and fields.
```php
<?php
Expand Down Expand Up @@ -182,7 +182,7 @@ $check->setOptions([
'Q' => 'Quick',
]);
```
###Checkbox
###Submit
*Del\Form|Field\Submit* doesn't really need much:
```php
<?php
Expand Down
21 changes: 10 additions & 11 deletions src/Renderer/AbstractFormRenderer.php
Expand Up @@ -13,14 +13,13 @@
use Del\Form\FormInterface;
use Del\Form\Renderer\Error\DefaultErrorRender;
use Del\Form\Renderer\Error\ErrorRendererInterface;
use Del\Form\Traits\HasDomTrait;
use DOMDocument;
use DomElement;
use DOMText;

abstract class AbstractFormRenderer implements FormRendererInterface
{
/** @var DOMDocument $dom */
protected $dom;
use HasDomTrait;

/** @var DomElement $form */
protected $form;
Expand Down Expand Up @@ -57,8 +56,8 @@ public function __construct()
*/
private function resetDom()
{
$this->dom = new DOMDocument();
$this->form = $this->dom->createElement('form');
$this->setDom(new DOMDocument());
$this->form = $this->getDom()->createElement('form');
}

/**
Expand All @@ -74,8 +73,8 @@ public function render(FormInterface $form, $displayErrors = true)
$fields = $form->getFields();
$this->processFields($fields);

$this->dom->appendChild($this->form);
$html = $this->dom->saveHTML();
$this->getDom()->appendChild($this->form);
$html = $this->getDom()->saveHTML();
$this->resetDom();
return $html;
}
Expand Down Expand Up @@ -120,7 +119,7 @@ private function processFields(FieldCollection $fields)
{
$fields->rewind();
while ($fields->valid()) {
$this->block = $this->dom->createElement('div');
$this->block = $this->createElement('div');
$this->field = $fields->current();
$this->label = $this->renderFieldLabel();
$this->element = $this->field->getRenderer()->render($this->dom, $this->field);
Expand Down Expand Up @@ -152,7 +151,7 @@ public function renderError()
*/
protected function createLabelElement()
{
$label = $this->dom->createElement('label');
$label = $this->createElement('label');
$label->setAttribute('for', $this->field->getId());
if ($this->field->isRequired()) {
$label = $this->addRequiredAsterisk($label);
Expand All @@ -163,9 +162,9 @@ protected function createLabelElement()

public function addRequiredAsterisk(DomElement $label)
{
$span = $this->dom->createElement('span');
$span = $this->createElement('span');
$span->setAttribute('class', 'text-danger');
$text = new DOMText('* ');
$text = $this->createText('* ');
$span->appendChild($text);
$label->appendChild($span);
return $label;
Expand Down
6 changes: 3 additions & 3 deletions src/Renderer/Error/AbstractErrorRender.php
Expand Up @@ -9,19 +9,19 @@

use DOMDocument;
use Del\Form\Field\FieldInterface;
use Del\Form\Traits\HasDomTrait;

abstract class AbstractErrorRender implements ErrorRendererInterface
{
/** @var DOMDocument */
protected $dom;
use HasDomTrait;

/**
* AbstractErrorRender constructor.
* @param DOMDocument $dom
*/
public function __construct(DOMDocument $dom)
{
$this->dom = $dom;
$this->setDom($dom);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Renderer/Error/DefaultErrorRender.php
Expand Up @@ -20,7 +20,7 @@ class DefaultErrorRender extends AbstractErrorRender implements ErrorRendererInt
*/
public function render(FieldInterface $field)
{
$helpBlock = $this->dom->createElement('span');
$helpBlock = $this->getDom()->createElement('span');
$helpBlock->setAttribute('class', 'help-block');

if ($this->shouldRender($field)) {
Expand All @@ -39,7 +39,7 @@ public function render(FieldInterface $field)
private function addCustomErrorMessage(DOMElement $helpBlock, FieldInterface $field)
{
$message = $field->getCustomErrorMessage();
$text = new DOMText($message);
$text = $this->createText($message);
$helpBlock->appendChild($text);
return $helpBlock;
}
Expand All @@ -66,8 +66,8 @@ private function addErrorMessages(DOMElement $helpBlock, FieldInterface $field)
*/
private function appendMessage(DOMElement $helpBlock, $message)
{
$text = new DOMText($message);
$br = $this->dom->createElement('br');
$text = $this->createText($message);
$br = $this->createLineBreak();
$helpBlock->appendChild($text);
$helpBlock->appendChild($br);
return $helpBlock;
Expand Down
10 changes: 5 additions & 5 deletions src/Renderer/Error/HorizontalFormErrorRender.php
Expand Up @@ -20,7 +20,7 @@ class HorizontalFormErrorRender extends AbstractErrorRender implements ErrorRend
*/
public function render(FieldInterface $field)
{
$helpBlock = $this->dom->createElement('span');
$helpBlock = $this->createElement('span');
$helpBlock->setAttribute('class', 'help-block');

if ($this->shouldRender($field)) {
Expand All @@ -29,7 +29,7 @@ public function render(FieldInterface $field)
: $this->addErrorMessages($helpBlock, $field);
}

$div = $this->dom->createElement('div');
$div = $this->createElement('div');
$div->setAttribute('class', 'col-sm-offset-2 col-sm-10');
$div->appendChild($helpBlock);
return $div;
Expand All @@ -43,7 +43,7 @@ public function render(FieldInterface $field)
private function addCustomErrorMessage(DOMElement $helpBlock, FieldInterface $field)
{
$message = $field->getCustomErrorMessage();
$text = new DOMText($message);
$text = $this->createText($message);
$helpBlock->appendChild($text);
return $helpBlock;
}
Expand All @@ -70,8 +70,8 @@ private function addErrorMessages(DOMElement $helpBlock, FieldInterface $field)
*/
private function appendMessage(DOMElement $helpBlock, $message)
{
$text = new DOMText($message);
$br = $this->dom->createElement('br');
$text = $this->createText($message);
$br = $this->createLineBreak();
$helpBlock->appendChild($text);
$helpBlock->appendChild($br);
return $helpBlock;
Expand Down
12 changes: 6 additions & 6 deletions src/Renderer/Field/AbstractFieldRender.php
Expand Up @@ -9,32 +9,32 @@

use Del\Form\Field\ArrayValueInterface;
use Del\Form\Field\FieldInterface;
use Del\Form\Traits\HasDomTrait;
use DOMDocument;
use DOMElement;

abstract class AbstractFieldRender implements FieldRendererInterface
{
/** @var DOMDocument $dom */
protected $dom;
use HasDomTrait;

/**
* @param DOMDocument $dom
* @return DOMElement
*/
public function render(DOMDocument $dom, FieldInterface $field)
{
$this->dom = $dom;
$element = $this->createElement($field);
$this->setDom($dom);
$element = $this->createElementFromField($field);
return $this->renderBlock($field, $element);
}

/**
* @param FieldInterface $field
* @return DOMElement
*/
public function createElement(FieldInterface $field)
public function createElementFromField(FieldInterface $field)
{
$element = $this->dom->createElement($field->getTag());
$element = $this->createElement($field->getTag());

foreach ($field->getAttributes() as $key => $value) {
$element = $this->setAttribute($field, $element, $key, $value);
Expand Down
10 changes: 5 additions & 5 deletions src/Renderer/Field/CheckboxRender.php
Expand Up @@ -31,7 +31,7 @@ public function renderBlock(FieldInterface $field, DOMElement $element)
// We don't really want a containing div, so we'll ignore $element
// and instead create a DOMDocumentFragment
unset($element);
$this->fragment = $this->dom->createDocumentFragment();
$this->fragment = $this->getDom()->createDocumentFragment();

// Make sure the FieldInterface is actually a Radio
if (!$field instanceof CheckBox) {
Expand Down Expand Up @@ -77,7 +77,7 @@ private function processOption(FieldInterface $field, $value, $labelText, $inlin
*/
private function renderCheckbox(FieldInterface $field, $value, $labelText)
{
$div = $this->dom->createElement('div');
$div = $this->getDom()->createElement('div');
$div->setAttribute('class', 'checkbox');
$radio = $this->renderCheckboxInline($field, $value, $labelText);
$radio->removeAttribute('class');
Expand All @@ -93,15 +93,15 @@ private function renderCheckbox(FieldInterface $field, $value, $labelText)
*/
private function renderCheckboxInline(FieldInterface $field, $value, $labelText)
{
$label = $this->dom->createElement('label');
$label = $this->getDom()->createElement('label');
$label->setAttribute('for', $field->getId());
$label->setAttribute('class', 'checkbox-inline');

$radio = $this->dom->createElement('input');
$radio = $this->getDom()->createElement('input');
$radio->setAttribute('type', 'checkbox');
$radio->setAttribute('name', $field->getName().'[]');
$radio->setAttribute('value', $value);
$text = new DOMText($labelText);
$text = $this->createText($labelText);

if(in_array($value, $field->getValue())) {
$radio->setAttribute('checked', 'checked');
Expand Down
10 changes: 5 additions & 5 deletions src/Renderer/Field/RadioRender.php
Expand Up @@ -31,7 +31,7 @@ public function renderBlock(FieldInterface $field, DOMElement $element)
// We don't really want a containing div, so we'll ignore $element
// and instead create a DOMDocumentFragment
unset($element);
$this->fragment = $this->dom->createDocumentFragment();
$this->fragment = $this->getDom()->createDocumentFragment();

// Make sure the FieldInterface is actually a Radio
if (!$field instanceof Radio) {
Expand Down Expand Up @@ -77,7 +77,7 @@ private function processOption(FieldInterface $field, $value, $labelText, $inlin
*/
private function renderRadio(FieldInterface $field, $value, $labelText)
{
$div = $this->dom->createElement('div');
$div = $this->createElement('div');
$div->setAttribute('class', 'radio');
$radio = $this->renderRadioInline($field, $value, $labelText);
$radio->removeAttribute('class');
Expand All @@ -93,15 +93,15 @@ private function renderRadio(FieldInterface $field, $value, $labelText)
*/
private function renderRadioInline(FieldInterface $field, $value, $labelText)
{
$label = $this->dom->createElement('label');
$label = $this->createElement('label');
$label->setAttribute('for', $field->getId());
$label->setAttribute('class', 'radio-inline');

$radio = $this->dom->createElement('input');
$radio = $this->createElement('input');
$radio->setAttribute('type', 'radio');
$radio->setAttribute('name', $field->getName());
$radio->setAttribute('value', $value);
$text = new DOMText($labelText);
$text = $this->createText($labelText);

if($field->getValue() == $radio->getAttribute('value')) {
$radio->setAttribute('checked', 'checked');
Expand Down
4 changes: 2 additions & 2 deletions src/Renderer/Field/SelectRender.php
Expand Up @@ -40,9 +40,9 @@ public function renderBlock(FieldInterface $field, DOMElement $element)
*/
private function processOption(FieldInterface $field, $value, $label)
{
$option = $this->dom->createElement('option');
$option = $this->createElement('option');
$option->setAttribute('value', $value);
$label = new DOMText($label);
$label = $this->createText($label);
$option->appendChild($label);
if($field->getValue() == $option->getAttribute('value')) {
$option->setAttribute('selected', 'selected');
Expand Down
8 changes: 4 additions & 4 deletions src/Renderer/HorizontalFormRenderer.php
Expand Up @@ -20,7 +20,7 @@ public function __construct()

// Add horizontal form class
$this->form->setAttribute('class', 'form-horizontal');
$this->errorRenderer = new HorizontalFormErrorRender($this->dom);
$this->errorRenderer = new HorizontalFormErrorRender($this->getDom());
}

/**
Expand All @@ -30,7 +30,7 @@ public function renderFieldLabel()
{
$label = $this->createLabelElement();
$label->setAttribute('class', 'col-sm-2 control-label');
$text = new DOMText($this->field->getLabel());
$text = $this->createText($this->field->getLabel());
$label->appendChild($text);
return $label;
}
Expand All @@ -43,7 +43,7 @@ public function renderFieldBlock()
$class = $this->block->getAttribute('class').'form-group';
$this->block->setAttribute('class', $class);

$div = $this->dom->createElement('div');
$div = $this->createElement('div');
$div->setAttribute('class', 'col-sm-10');

$this->processField($div);
Expand Down Expand Up @@ -92,7 +92,7 @@ private function processField(DOMElement $div)
*/
private function surroundInDiv(DOMNode $element, $class)
{
$div = $this->dom->createElement('div');
$div = $this->createElement('div');
$div->setAttribute('class', $class);
$div->appendChild($element);
return $div;
Expand Down

0 comments on commit ac30861

Please sign in to comment.