Skip to content

Commit

Permalink
improved decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Apr 23, 2019
1 parent cb0e5dc commit 39c48df
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 277 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
],
"require": {
"php": ">=7.1",
"packaged/glimpse": "^2.5.3",
"packaged/ui": "~0.2",
"packaged/glimpse": "^2.6",
"packaged/ui": "^1.0",
"packaged/validate": "^2.1"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/DataHandlers/TextDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function formatValue($value)
protected function _defaultDecorator(): DataHandlerDecorator
{
$decorator = new InputDecorator();
$decorator->setType(Input::TYPE_TEXT);
$decorator->getInput()->setAttribute('type', Input::TYPE_TEXT);
return $decorator;
}
}
115 changes: 69 additions & 46 deletions src/Decorators/AbstractDataHandlerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,98 @@
use Packaged\Glimpse\Tags\Lists\UnorderedList;
use Packaged\Helpers\Objects;
use Packaged\Helpers\Strings;
use Packaged\Ui\Html\HtmlElement;

abstract class AbstractDataHandlerDecorator extends AbstractDecorator implements DataHandlerDecorator
{
protected $_tag = 'div';
/**
* @var DataHandler
*/
protected $_handler;
/**
* @var callable
*/
protected $_formatCallback;

protected $_input;
protected $_label;

public function __construct()
{
$this->_input = $this->_initInputElement();
$this->_label = $this->_initLabelElement();
}

public function setHandler(DataHandler $handler)
{
$this->_handler = $handler;
return $this;
}

abstract protected function _getInputElement(): HtmlTag;
abstract protected function _initInputElement(): HtmlTag;

protected function _initLabelElement(): ?HtmlTag
{
return Label::create();
}

/**
* @return HtmlTag
*/
public function getInput(): HtmlElement
{
return $this->_input;
}

protected function _getLabelElement(): ?HtmlTag
/**
* @return HtmlTag|null
*/
public function getLabel(): ?HtmlTag
{
$labelText = $this->_handler->getLabel()
?? Strings::titleize(Strings::splitOnCamelCase($this->_handler->getName()));
if($labelText)
return $this->_label;
}

protected function _configureInputElement(HtmlElement $input)
{
$id = $input->getId();
$name = $this->_handler->getName();
if(empty($id) && $this->_label && $name)
{
$label = Label::create();
$label->setContent($labelText);
return $label;
// create an id
$id = strtolower(str_replace(' ', '-', Strings::splitOnCamelCase($name)) . '-' . Strings::randomString(3));
$input->setId($id);
}
return null;
$input->setAttribute('name', $name, true);
}

protected function _getElement()
protected function _configureLabelElement(HtmlTag $label)
{
$input = $this->_getInputElement();
$input->addAttributes($this->_attributes, true);
$label = $this->_getLabelElement();
if($label)
if(empty($label->getContent(true)))
{
$id = $input->getId();
if(empty($id))
$labelText = $this->_handler->getLabel()
?? Strings::titleize(Strings::splitOnCamelCase($this->_handler->getName()));
if($labelText)
{
// create an id
$name = $this->_handler->getName();
$idSeed = $name
? str_replace(' ', '-', Strings::splitOnCamelCase($name)) . '-' . Strings::randomString(3)
: Strings::pattern('XXXXX-0000-X0X0');

$id = strtolower($idSeed);
$input->setId($id);
$label->setContent($labelText);
}
$label->setAttribute('for', $id);
}
$label->setAttribute('for', $this->_input->getId(), true);
}

protected function _prepareForProduce(): HtmlElement
{
$this->addClass('p-form-field');
return parent::_prepareForProduce();
}

protected function _getContentForRender()
{
$input = $this->_input;
$this->_configureInputElement($input);
if($this->_label)
{
$this->_configureLabelElement($this->_label);
}

$errorTag = $this->_getErrorElement();
return $this->_formatElements($input, $label, $errorTag);
return $this->_formatElements($input, $this->_label, $errorTag);
}

protected function _getErrorElement()
Expand All @@ -83,28 +118,16 @@ protected function _getErrorElement()

protected function _formatElements(HtmlTag $input, ?HtmlTag $label, ?HtmlTag $errors)
{
$callback = $this->_formatCallback;
if(is_callable($callback))
{
return $callback($input, $label, $errors);
}

$return = Div::create()->addClass('p-form-field');
$return = [];
if($label)
{
$return->appendContent(Div::create($label)->addClass('p-form--label'));
$return [] = Div::create($label)->addClass('p-form--label');
}
if($errors)
{
$return->appendContent(Div::create($errors)->addClass('p-form--errors'));
$return[] = Div::create($errors)->addClass('p-form--errors');
}
$return->appendContent(Div::create($input)->addClass('p-form--input'));
$return[] = Div::create($input)->addClass('p-form--input');
return $return;
}

public function setFormatCallback(callable $callback)
{
$this->_formatCallback = $callback;
return $this;
}
}
81 changes: 2 additions & 79 deletions src/Decorators/AbstractDecorator.php
Original file line number Diff line number Diff line change
@@ -1,87 +1,10 @@
<?php
namespace Packaged\Form\Decorators;

use Exception;
use Packaged\Form\Decorators\Interfaces\Decorator;
use Packaged\Glimpse\Core\HtmlTag;
use Packaged\Helpers\Arrays;
use Packaged\SafeHtml\SafeHtml;
use Packaged\Ui\Html\HtmlElement;

abstract class AbstractDecorator implements Decorator
abstract class AbstractDecorator extends HtmlElement implements Decorator
{
protected $_attributes = [];

/**
* @return string
*/
public function getId(): ?string
{
return $this->getAttribute('id');
}

/**
* @param string $id
*
* @return $this
*/
public function setId($id)
{
$this->addAttribute('id', $id);
return $this;
}

public function addAttribute($name, $value)
{
$this->_attributes[$name] = $value;
return $this;
}

public function getAttribute($name): ?string
{
return Arrays::value($this->_attributes, $name);
}

public function removeAttribute($name)
{
unset($this->_attributes[$name]);
return $this;
}

public function hasAttribute($name): bool
{
return array_key_exists($name, $this->_attributes);
}

/**
* @param HtmlTag $ele
*
* @return HtmlTag $ele
*/
protected function _hydrateElement(HtmlTag $ele)
{
return $ele->setAttributes($this->_attributes);
}

/**
* @return HtmlTag
*/
abstract protected function _getElement();

/**
* @return string
* @throws Exception
*/
public function render(): string
{
return (string)$this->produceSafeHTML();
}

/**
* @return SafeHtml
* @throws Exception
*/
public function produceSafeHTML(): SafeHtml
{
return $this->_getElement()->produceSafeHTML();
}
}
30 changes: 20 additions & 10 deletions src/Decorators/DefaultFormDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

use Packaged\Form\Form\Form;
use Packaged\Form\Form\Interfaces\FormDecorator;
use Packaged\Glimpse\Core\CustomHtmlTag;
use Packaged\Ui\Html\HtmlElement;

class DefaultFormDecorator extends AbstractDecorator implements FormDecorator
{
protected $_tag = 'form';
/**
* @var Form
*/
protected $_form;

public function __construct()
{
$this->addClass('p-form');
}

public function setForm(Form $form): FormDecorator
{
$this->_form = $form;
Expand All @@ -23,27 +29,31 @@ public function getForm(): Form
return $this->_form;
}

protected function _getElement()
protected function _prepareForProduce(): HtmlElement
{
$form = $this->getForm();

$formElement = $this->_hydrateElement(CustomHtmlTag::build('form'))
->setAttribute('method', $form->getMethod())
->addClass('p-form');

$action = $form->getAction();
if($action)
{
$formElement->setAttribute('action', $action);
$this->setAttribute('action', $action);
}
$this->setAttribute('method', $form->getMethod());
return parent::_prepareForProduce();
}

protected function _getContentForRender()
{
$form = $this->getForm();

$content = [];
foreach($form->getDataHandlers() as $handler)
{
$formElement->appendContent($handler->getDecorator()->produceSafeHTML());
$content[] = $handler->getDecorator()->produceSafeHTML();
}

$formElement->appendContent($form->getSubmitDecorator()->produceSafeHTML());
$content[] = $form->getSubmitDecorator()->produceSafeHTML();

return $formElement;
return $content;
}
}
20 changes: 16 additions & 4 deletions src/Decorators/HiddenInputDecorator.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
<?php
namespace Packaged\Form\Decorators;

use Packaged\Glimpse\Core\HtmlTag;
use Packaged\Glimpse\Tags\Form\Input;
use Packaged\SafeHtml\SafeHtml;
use Packaged\Ui\Html\HtmlElement;

class HiddenInputDecorator extends InputDecorator
{
protected $_type = Input::TYPE_HIDDEN;
protected $_tag = '';

public function produceSafeHTML(): SafeHtml
protected function _formatElements(HtmlTag $input, ?HtmlTag $label, ?HtmlTag $errors)
{
return SafeHtml::escape([$this->_getInputElement(), $this->_getErrorElement()], '');
return [$input, $errors];
}

protected function _initLabelElement(): ?HtmlTag
{
return null;
}

protected function _configureInputElement(HtmlElement $input)
{
$input->setAttribute('type', Input::TYPE_HIDDEN);
parent::_configureInputElement($input);
}
}
Loading

0 comments on commit 39c48df

Please sign in to comment.