update 3
This commit is contained in:
@ -2,10 +2,14 @@
|
||||
|
||||
namespace itguild\forms;
|
||||
|
||||
use itguild\forms\builders\ButtonBuilder;
|
||||
use itguild\forms\builders\CheckBoxBuilder;
|
||||
use itguild\forms\builders\SelectBuilder;
|
||||
use itguild\forms\builders\TextAreaBuilder;
|
||||
use itguild\forms\debug\Debug;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
use itguild\forms\inputs\Button;
|
||||
use itguild\forms\inputs\Checkbox;
|
||||
use itguild\forms\inputs\Select;
|
||||
use itguild\forms\inputs\TextArea;
|
||||
use itguild\forms\templates\Template;
|
||||
@ -41,6 +45,12 @@ class ActiveForm
|
||||
elseif ($class === TextArea::class){
|
||||
$this->fieldObject = TextAreaBuilder::build($name, $params);
|
||||
}
|
||||
elseif ($class === Checkbox::class) {
|
||||
$this->fieldObject = CheckBoxBuilder::build($name, $params);
|
||||
}
|
||||
elseif ($class === Button::class) {
|
||||
$this->fieldObject = ButtonBuilder::build($name, $params);
|
||||
}
|
||||
else {
|
||||
$this->fieldObject = new $class($name, $params);
|
||||
}
|
||||
|
16
src/builders/ButtonBuilder.php
Normal file
16
src/builders/ButtonBuilder.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\builders;
|
||||
use itguild\forms\inputs\Button;
|
||||
class ButtonBuilder
|
||||
{
|
||||
|
||||
public static function build(string $name, array $params = [])
|
||||
{
|
||||
$value = $params['value'] ?? null;
|
||||
unset($params['value']);
|
||||
|
||||
return new Button(name: $name, value: $value, paramsArray: $params);
|
||||
}
|
||||
|
||||
}
|
16
src/builders/CheckBoxBuilder.php
Normal file
16
src/builders/CheckBoxBuilder.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\builders;
|
||||
use itguild\forms\inputs\Checkbox;
|
||||
|
||||
|
||||
class CheckBoxBuilder
|
||||
{
|
||||
public static function build(string $name, array $params = [])
|
||||
{
|
||||
$value = $params['value'] ?? null;
|
||||
unset($params['value']);
|
||||
|
||||
return new Checkbox(name: $name, value: $value, paramsArray: $params);
|
||||
}
|
||||
}
|
@ -2,10 +2,14 @@
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\templates\Template;
|
||||
|
||||
abstract class BaseInput
|
||||
{
|
||||
|
||||
protected $html;
|
||||
protected Template $inputTemplate;
|
||||
protected bool $hasLabel = false;
|
||||
protected string $html;
|
||||
protected string $labelTitle = "";
|
||||
|
||||
/**
|
||||
* @return void
|
||||
@ -18,8 +22,27 @@ abstract class BaseInput
|
||||
public abstract function create();
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->inputTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
|
||||
@ -12,8 +13,6 @@ class Button extends BaseInput
|
||||
private string $name;
|
||||
private string $value;
|
||||
private array $paramsArray;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
@ -25,6 +24,7 @@ class Button extends BaseInput
|
||||
$this->name = $name;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->value = $value;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,10 +33,13 @@ class Button extends BaseInput
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$this->html = "<button name='$this->name' $paramsString>$this->value</button>";
|
||||
$label = "";
|
||||
$button = "<button name='$this->name' $paramsString>$this->value</button>";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{input}', $button, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -49,19 +52,8 @@ class Button extends BaseInput
|
||||
*/
|
||||
public static function build(string $name, string $value, array $paramsArray = []): void
|
||||
{
|
||||
$input = new self($name, $value, $paramsArray);
|
||||
$input->create()->render();
|
||||
}
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
$button = new self($name, $value, $paramsArray);
|
||||
$button->create()->render();
|
||||
}
|
||||
|
||||
}
|
37
src/inputs/CgSelectInput.php
Normal file
37
src/inputs/CgSelectInput.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
class CgSelectInput extends BaseInput
|
||||
{
|
||||
|
||||
private string $selector;
|
||||
private array $params;
|
||||
private $params;
|
||||
public function __construct(string $selector, array $params = [])
|
||||
{
|
||||
$this->selector = $selector;
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$optionsString = $this->createOption($this->options, $this->value);
|
||||
$label = "";
|
||||
$select = "<select name='$this->name' $paramsString>$optionsString</select>";
|
||||
if($this->hasLabel == true) {
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
|
||||
{
|
||||
$select = new self($name, $options, $value, $paramsArray);
|
||||
$select->create()->render();
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
|
||||
@ -12,8 +13,7 @@ class Checkbox extends BaseInput
|
||||
private string $name;
|
||||
private string $value;
|
||||
private array $paramsArray;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -21,11 +21,12 @@ class Checkbox extends BaseInput
|
||||
* @param string $value
|
||||
* @param array $paramsArray
|
||||
*/
|
||||
public function __construct(string $name, string $value, array $paramsArray = [])
|
||||
public function __construct(string $name, string $value = '', array $paramsArray = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,10 +35,13 @@ class Checkbox extends BaseInput
|
||||
public function create(): self
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$this->html = "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
|
||||
$checkBox = "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
|
||||
$label = "";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{input}', $checkBox, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -50,20 +54,8 @@ class Checkbox extends BaseInput
|
||||
*/
|
||||
public static function build(string $name, string $value, array $paramsArray): void
|
||||
{
|
||||
$checkbox = new self($name, $value, $paramsArray);
|
||||
$checkbox->create()->render();
|
||||
$checkBox = new self($name, $value, $paramsArray);
|
||||
$checkBox->create()->render();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\inputs;
|
||||
use itguild\forms\templates\Simple\SimpleTemplate;
|
||||
use itguild\forms\traits\CreateOption;
|
||||
use itguild\forms\traits\CreateParams;
|
||||
use itguild\forms\inputs\BaseInput;
|
||||
@ -12,13 +13,13 @@ class Radio extends BaseInput
|
||||
private $paramsArray;
|
||||
private $options = [];
|
||||
private $value;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
|
||||
|
||||
public function __construct(string $name, array $paramsArray = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,11 +29,13 @@ class Radio extends BaseInput
|
||||
{
|
||||
$paramsString = $this->createParams($this->paramsArray);
|
||||
$optionsString = $this->createOption($this->options, $this->value);
|
||||
$this->html = "<input name='$this->name' type='radio' $paramsString>$optionsString";
|
||||
$radioButton = "<input name='$this->name' type='radio' $paramsString>$optionsString";
|
||||
$label = "";
|
||||
if($this->hasLabel == true) {
|
||||
$this->html = "<label>$this->labelTitle</label> $this->html";
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
|
||||
$this->html = str_replace('{input}', $radioButton, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -43,21 +46,10 @@ class Radio extends BaseInput
|
||||
*/
|
||||
public static function build(string $name, array $paramsArray = []): void
|
||||
{
|
||||
$label = new self($name, $paramsArray);
|
||||
$label->create()->render();
|
||||
$radioButton = new self($name, $paramsArray);
|
||||
$radioButton->create()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $options
|
||||
|
@ -17,11 +17,9 @@ class Select extends BaseInput
|
||||
private $name;
|
||||
private $options;
|
||||
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
private $value;
|
||||
private $paramsArray;
|
||||
private Template $selectTemplate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -36,7 +34,7 @@ class Select extends BaseInput
|
||||
$this->options = $options;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->selectTemplate = new SimpleTemplate();
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,7 +49,7 @@ class Select extends BaseInput
|
||||
if($this->hasLabel == true) {
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
$this->html = str_replace('{input}', $select, $this->selectTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{input}', $select, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
@ -71,18 +69,6 @@ class Select extends BaseInput
|
||||
$select->create()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $options
|
||||
* @return $this
|
||||
@ -94,14 +80,5 @@ class Select extends BaseInput
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->selectTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -14,9 +14,8 @@ class TextArea extends BaseInput
|
||||
private $name;
|
||||
private $value;
|
||||
private $paramsArray;
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
private Template $textareaTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
@ -27,7 +26,7 @@ class TextArea extends BaseInput
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
$this->paramsArray = $paramsArray;
|
||||
$this->textareaTemplate = new SimpleTemplate();
|
||||
$this->inputTemplate = new SimpleTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,7 +41,7 @@ class TextArea extends BaseInput
|
||||
$label = "<label>$this->labelTitle</label>";
|
||||
}
|
||||
|
||||
$this->html = str_replace('{textarea}', $textarea, $this->textareaTemplate->getTextAreaTemplate());
|
||||
$this->html = str_replace('{input}', $textarea, $this->inputTemplate->getInputTemplate());
|
||||
$this->html = str_replace('{label}', $label, $this->html);
|
||||
|
||||
return $this;
|
||||
@ -61,28 +60,4 @@ class TextArea extends BaseInput
|
||||
$textarea->create()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->textareaTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -14,12 +14,9 @@ class TextInput extends BaseInput
|
||||
|
||||
use CreateParams;
|
||||
private string $name;
|
||||
|
||||
private bool $hasLabel = false;
|
||||
private string $labelTitle = '';
|
||||
private array $paramsArray;
|
||||
|
||||
private Template $inputTemplate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -63,28 +60,4 @@ class TextInput extends BaseInput
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $title): self
|
||||
{
|
||||
$this->hasLabel = true;
|
||||
$this->labelTitle = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $template
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplate($template): self
|
||||
{
|
||||
$this->inputTemplate = new $template();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -14,25 +14,4 @@ class SimpleTemplate extends Template
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{input}</div>";
|
||||
}
|
||||
|
||||
public static function getTextAreaTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{textarea}</div>";
|
||||
}
|
||||
public static function getSelectTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{select}</div>";
|
||||
}
|
||||
public static function getRadioTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{radio}</div>";
|
||||
}
|
||||
public static function getCheckBoxTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{checkbox}</div>";
|
||||
}
|
||||
public static function getButtonTemplate(): string
|
||||
{
|
||||
return "<div class='form-group'>{label}<br>{button}</div>";
|
||||
}
|
||||
}
|
@ -7,11 +7,5 @@ abstract class Template
|
||||
|
||||
abstract static function getInputTemplate();
|
||||
|
||||
abstract static function getTextAreaTemplate();
|
||||
|
||||
abstract static function getSelectTemplate();
|
||||
|
||||
abstract static function getRadioTemplate();
|
||||
abstract static function getCheckBoxTemplate();
|
||||
abstract static function getButtonTemplate();
|
||||
}
|
Reference in New Issue
Block a user