This commit is contained in:
kali 2024-03-15 16:56:03 +03:00
parent 6e62ce7363
commit 5bda1e0525
16 changed files with 244 additions and 75 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
.idea
.idea
/vendor/

15
composer.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "itguild/forms",
"type": "library",
"autoload": {
"psr-4": {
"itguild\\forms\\": "src/"
}
},
"authors": [
{
"name": "nikita"
}
],
"require": {}
}

View File

@ -1,19 +1,13 @@
<?php
use itguild\forms\Form;
use \itguild\forms\inputs\TextInput;
use \itguild\forms\inputs\Radio;
ini_set("display_errors", true);
error_reporting(-1);
require_once "src/traits/CreateParams.php";
require_once "src/traits/CreateOption.php";
require_once "src/inputs/Radio.php";
require_once "src/inputs/Select.php";
require_once "src/inputs/Button.php";
require_once "src/inputs/TextInput.php";
require_once "src/inputs/TextArea.php";
require_once "src/inputs/Checkbox.php";
require_once "src/inputs/Label.php";
require_once "src/debug/Debug.php";
require_once "src/Form.php";
use src\Form;
require_once "vendor/autoload.php";
$form = new Form();
$form->beginForm("tsad");
@ -32,3 +26,16 @@ $form->textarea("textarea", "dsadasads", ["id" => "t1"]);
$form->select("select", ["class1" => "option", "class2" => "b2", "class3" => "b4"], 'class2', ["id" => "s1"]);
$form->button('button', "Кнопка", ["id" => "button"]);
$form->endForm();
$activeForm = new \itguild\forms\ActiveForm();
$activeForm->field(class: TextInput::class, name: 'nnn', params: ["style" => "color:RED;display:flex"])
->setLabel("dfdfdffd")
->render();
$activeForm->field(class: TextInput::class, name: 'bbb', params: [])
->setLabel("bbbbbb")
->render();
$activeForm->field(class: Radio::class, name: 'nameee',params: ["style" => "color:RED;display:flex"])
->setLabel("bbbbb")
->render();

32
src/ActiveForm.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace itguild\forms;
use itguild\forms\debug\Debug;
use itguild\forms\inputs\BaseInput;
class ActiveForm
{
private BaseInput $fieldObject;
public function field($class, string $name, array $params = [])
{
$this->fieldObject = new $class($name, $params);
$this->fieldObject->create();
return $this;
}
public function setLabel(string $title)
{
$this->fieldObject->setLabel($title);
return $this;
}
public function render()
{
$this->fieldObject->render();
}
}

View File

@ -1,14 +1,14 @@
<?php
namespace src;
namespace itguild\forms;
use src\inputs\Checkbox;
use src\inputs\Select;
use src\inputs\TextInput;
use src\inputs\Label;
use src\inputs\Radio;
use src\inputs\TextArea;
use src\inputs\Button;
use itguild\forms\inputs\Checkbox;
use itguild\forms\inputs\Select;
use itguild\forms\inputs\TextInput;
use itguild\forms\inputs\Label;
use itguild\forms\inputs\Radio;
use itguild\forms\inputs\TextArea;
use itguild\forms\inputs\Button;
class Form {

View File

@ -1,6 +1,6 @@
<?php
namespace src\debug;
namespace itguild\forms\debug;
class Debug
{

25
src/inputs/BaseInput.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace itguild\forms\inputs;
abstract class BaseInput
{
protected $html;
/**
* @return void
*/
public function render(): void
{
echo $this->html;
}
public abstract function create();
/**
* @return $this
*/
}

View File

@ -1,16 +1,19 @@
<?php
namespace src\inputs;
namespace itguild\forms\inputs;
use src\traits\CreateParams;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
class Button
class Button extends BaseInput
{
use CreateParams;
private string $name;
private string $value;
private array $paramsArray;
/**
* @param string $name
* @param string $value
@ -24,12 +27,14 @@ class Button
}
/**
* @return void
* @return $this
*/
public function create(): void
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
echo "<button name='$this->name' $paramsString>$this->value</button>";
$this->html = "<button name='$this->name' $paramsString>$this->value</button>";
return $this;
}
/**
@ -41,7 +46,16 @@ class Button
public static function build(string $name, string $value, array $paramsArray = []): void
{
$input = new self($name, $value, $paramsArray);
$input->create();
$input->create()->render();
}
/**
* @param string $title
* @return $this
*/
public function setLabel(string $title): self
{
$this->html = "<label>$title $this->html</label>";
return $this;
}
}

View File

@ -1,10 +1,11 @@
<?php
namespace src\inputs;
namespace itguild\forms\inputs;
use src\traits\CreateParams;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
class Checkbox
class Checkbox extends BaseInput
{
use CreateParams;
@ -12,6 +13,7 @@ class Checkbox
private string $value;
private array $paramsArray;
/**
* @param string $name
* @param string $value
@ -25,12 +27,13 @@ class Checkbox
}
/**
* @return void
* @return $this
*/
public function create(): void
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
echo "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
$this->html = "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
return $this;
}
/**
@ -42,8 +45,17 @@ class Checkbox
public static function build(string $name, string $value, array $paramsArray): void
{
$checkbox = new self($name, $value, $paramsArray);
$checkbox->create();
$checkbox->create()->render();
}
/**
* @param string $title
* @return $this
*/
public function setLabel(string $title): self
{
$this->html = "<label>$title $this->html</label>";
return $this;
}
}

View File

@ -1,9 +1,10 @@
<?php
namespace src\inputs;
namespace itguild\forms\inputs;
use src\traits\CreateParams;
class Label
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
class Label extends BaseInput
{
use CreateParams;
@ -11,6 +12,7 @@ class Label
private array $paramsArray;
/**
* @param string $title
* @param array $paramsArray
@ -22,12 +24,14 @@ class Label
}
/**
* @return void
* @return $this
*/
public function create(): void
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
echo "<label $paramsString >$this->title</label>";
$this->html = "<label $paramsString >$this->title</label>";
return $this;
}
@ -39,7 +43,7 @@ class Label
public static function build(string $title, array $paramsArray = [])
{
$label = new self($title, $paramsArray);
$label->create();
$label->create()->render();
}

View File

@ -1,13 +1,15 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class Radio
namespace itguild\forms\inputs;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
class Radio extends BaseInput
{
use CreateParams;
private $name;
private $paramsArray;
public function __construct(string $name, array $paramsArray = [])
{
$this->name = $name;
@ -15,12 +17,14 @@ class Radio
}
/**
* @return void
* @return $this
*/
public function create(): void
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
echo "<input name='$this->name' type='radio' $paramsString>";
$this->html = "<input name='$this->name' type='radio' $paramsString>";
return $this;
}
/**
@ -31,6 +35,16 @@ class Radio
public static function build(string $name, array $paramsArray = []): void
{
$label = new self($name, $paramsArray);
$label->create();
$label->create()->render();
}
/**
* @param string $title
* @return $this
*/
public function setLabel(string $title): self
{
$this->html = "<label>$title $this->html</label>";
return $this;
}
}

View File

@ -1,11 +1,12 @@
<?php
namespace src\inputs;
namespace itguild\forms\inputs;
use src\traits\CreateOption;
use src\traits\CreateParams;
use itguild\forms\traits\CreateOption;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
class Select
class Select extends BaseInput
{
use CreateParams;
use CreateOption;
@ -14,6 +15,7 @@ class Select
private $value;
private $paramsArray;
/**
* @param string $name
* @param array $options
@ -29,13 +31,15 @@ class Select
}
/**
* @return void
* @return $this
*/
public function create(): void
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
$optionsString = $this->createOption($this->options, $this->value);
echo "<select name='$this->name' $paramsString>$optionsString</select>";
$this->html = "<select name='$this->name' $paramsString>$optionsString</select>";
return $this;
}
@ -49,7 +53,16 @@ class Select
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
{
$textarea = new self($name, $options, $value, $paramsArray);
$textarea->create();
$textarea->create()->render();
}
/**
* @param string $title
* @return $this
*/
public function setLabel(string $title): self
{
$this->html = "<label>$title $this->html</label>";
return $this;
}
}

View File

@ -1,8 +1,11 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class TextArea
namespace itguild\forms\inputs;
use itguild\forms\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
class TextArea extends BaseInput
{
use CreateParams;
@ -15,7 +18,7 @@ class TextArea
* @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;
@ -23,14 +26,17 @@ class TextArea
}
/**
* @return void
* @return $this
*/
public function create(): void
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
echo "<textarea name='$this->name' $paramsString>$this->value</textarea>";
$this->html = "<textarea name='$this->name' $paramsString>$this->value</textarea>";
return $this;
}
/**
* @param string $name
* @param string $value
@ -40,7 +46,17 @@ class TextArea
public static function build(string $name, string $value, array $paramsArray = []): void
{
$textarea = new self($name, $value, $paramsArray);
$textarea->create();
$textarea->create()->render();
}
/**
* @param string $title
* @return $this
*/
public function setLabel(string $title): self
{
$this->html = "<label>$title $this->html</label>";
return $this;
}

View File

@ -1,10 +1,11 @@
<?php
namespace src\inputs;
namespace itguild\forms\inputs;
use src\traits\CreateParams;
use itguild\forms\inputs\BaseInput;
use itguild\forms\traits\CreateParams;
class TextInput
class TextInput extends BaseInput
{
use CreateParams;
@ -12,6 +13,7 @@ class TextInput
private array $paramsArray;
/**
* @param string $name
* @param array $paramsArray
@ -23,12 +25,14 @@ class TextInput
}
/**
* @return void
* @return self
*/
public function create(): void
public function create(): self
{
$paramsString = $this->createParams($this->paramsArray);
echo "<input name='$this->name' $paramsString >";
$this->html = "<input name='$this->name' $paramsString >";
return $this;
}
/**
@ -39,7 +43,19 @@ class TextInput
public static function build(string $name, array $paramsArray = []): void
{
$input = new self($name, $paramsArray);
$input->create();
$input->create()->render();
}
/**
* @param string $title
* @return $this
*/
public function setLabel(string $title): self
{
$this->html = "<label>$title $this->html</label>";
return $this;
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace src\traits;
namespace itguild\forms\traits;
trait CreateOption
{

View File

@ -1,6 +1,6 @@
<?php
namespace src\traits;
namespace itguild\forms\traits;
trait CreateParams
{