This commit is contained in:
kali 2024-03-15 14:17:25 +03:00
commit 000e0acfb8
13 changed files with 492 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

33
index.php Normal file
View File

@ -0,0 +1,33 @@
<?php
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;
$form = new Form();
$form->beginForm("tsad");
$form->textInput("phone", ['placeholder' => 'Введите телефон', 'class' => 'form-group']);
$form->textInput("email", ['placeholder' => 'Введите email']);
$form->textInput("name", ['type' => 'number']);
$form->checkBox("Button", "1", ["class" => 'sdsa', "id" => "b1"]);
$form->checkBox("Button", "2", ["class" => 'sdsa', "id" => "b1"]);
$form->checkBox("Button", "3", ["class" => 'sdsa', "id" => "b1"]);
$form->label("RadioButton", ["for" => "r2"]);
$form->radio("name1", ["id" => "r2"]);
$form->label("RadioButton", ["for" => "r3"]);
$form->radio("name1", ["id" => "r3"]);
$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();

110
src/Form.php Normal file
View File

@ -0,0 +1,110 @@
<?php
namespace src;
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;
class Form {
/**
* @param string $action
* @return void
*/
public function beginForm(string $action): void
{
echo "<form action='$action'>";
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public function textInput(string $name, array $paramsArray = []): void
{
TextInput::build($name, $paramsArray);
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public function checkBox(string $name, string $value, array $paramsArray = []): void
{
Checkbox::build($name, $value, $paramsArray);
}
/**
* @param string $title
* @param array $paramsArray
* @return void
*/
public function label(string $title, array $paramsArray = []): void
{
Label::build($title, $paramsArray);
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public function radio(string $name, array $paramsArray = []): void
{
Radio::build($name, $paramsArray);
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public function textarea(string $name, string $value = "", array $paramsArray = []): void
{
TextArea::build($name, $value, $paramsArray);
}
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
* @return void
*/
public function select(string $name, array $options = [], $value = null, array $paramsArray = []): void
{
Select::build($name, $options, $value, $paramsArray);
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public function button(string $name, string $value, array $paramsArray = []): void
{
Button::build($name, $value, $paramsArray);
}
/**
* @return void
*/
public function endForm(): void
{
echo "</form>";
}
}

20
src/debug/Debug.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace src\debug;
class Debug
{
public static function prn($content)
{
echo '<pre style="background: lightgray; border: 1px solid black; padding: 2px">';
print_r($content);
echo '</pre>';
}
public static function dd($content)
{
echo '<pre style="background: lightgray; border: 1px solid black; padding: 2px">';
print_r($content);
echo '</pre>';
die();
}
}

33
src/inputs/Button.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class Button
{
use CreateParams;
private string $name;
private string $value;
private array $paramsArray;
public function __construct(string $name, string $value, array $paramsArray = [])
{
$this->name = $name;
$this->paramsArray = $paramsArray;
$this->value = $value;
}
public function create(): void
{
$paramsString = $this->createParams($this->paramsArray);
echo "<button name='$this->name' $paramsString>$this->value</button>";
}
public static function build(string $name, string $value, array $paramsArray = []): void
{
$input = new self($name, $value, $paramsArray);
$input->create();
}
}

34
src/inputs/Checkbox.php Normal file
View File

@ -0,0 +1,34 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class Checkbox
{
use CreateParams;
private string $name;
private string $value;
private array $paramsArray;
public function __construct(string $name, string $value, array $paramsArray = [])
{
$this->name = $name;
$this->value = $value;
$this->paramsArray = $paramsArray;
}
public function create(): void
{
$paramsString = $this->createParams($this->paramsArray);
echo "<input name='$this->name' type='checkbox' value='$this->value' $paramsString >";
}
public static function build(string $name, string $value, array $paramsArray): void
{
$checkbox = new self($name, $value, $paramsArray);
$checkbox->create();
}
}

35
src/inputs/Label.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class Label
{
use CreateParams;
private string $title;
private array $paramsArray;
public function __construct(string $title, array $paramsArray = [])
{
$this->title = $title;
$this->paramsArray = $paramsArray;
}
public function create(): void
{
$paramsString = $this->createParams($this->paramsArray);
echo "<label $paramsString >$this->title</label>";
}
public static function build(string $title, array $paramsArray = [])
{
$label = new self($title, $paramsArray);
$label->create();
}
}

36
src/inputs/Radio.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class Radio
{
use CreateParams;
private $name;
private $paramsArray;
public function __construct(string $name, array $paramsArray = [])
{
$this->name = $name;
$this->paramsArray = $paramsArray;
}
/**
* @return void
*/
public function create(): void
{
$paramsString = $this->createParams($this->paramsArray);
echo "<input name='$this->name' type='radio' $paramsString>";
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $paramsArray = []): void
{
$label = new self($name, $paramsArray);
$label->create();
}
}

55
src/inputs/Select.php Normal file
View File

@ -0,0 +1,55 @@
<?php
namespace src\inputs;
use src\traits\CreateOption;
use src\traits\CreateParams;
class Select
{
use CreateParams;
use CreateOption;
private $name;
private $options;
private $value;
private $paramsArray;
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
*/
public function __construct(string $name, array $options = [], $value = null, array $paramsArray = [])
{
$this->name = $name;
$this->options = $options;
$this->value = $value;
$this->paramsArray = $paramsArray;
}
/**
* @return void
*/
public function create(): void
{
$paramsString = $this->createParams($this->paramsArray);
$optionsString = $this->createOption($this->options, $this->value);
echo "<select name='$this->name' $paramsString>$optionsString</select>";
}
/**
* @param string $name
* @param array $options
* @param $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $options = [], $value = null, array $paramsArray = []): void
{
$textarea = new self($name, $options, $value, $paramsArray);
$textarea->create();
}
}

47
src/inputs/TextArea.php Normal file
View File

@ -0,0 +1,47 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class TextArea
{
use CreateParams;
private $name;
private $value;
private $paramsArray;
/**
* @param string $name
* @param string $value
* @param array $paramsArray
*/
public function __construct(string $name, string $value, array $paramsArray = [])
{
$this->name = $name;
$this->value = $value;
$this->paramsArray = $paramsArray;
}
/**
* @return void
*/
public function create(): void
{
$paramsString = $this->createParams($this->paramsArray);
echo "<textarea name='$this->name' $paramsString>$this->value</textarea>";
}
/**
* @param string $name
* @param string $value
* @param array $paramsArray
* @return void
*/
public static function build(string $name, string $value, array $paramsArray = []): void
{
$textarea = new self($name, $value, $paramsArray);
$textarea->create();
}
}

45
src/inputs/TextInput.php Normal file
View File

@ -0,0 +1,45 @@
<?php
namespace src\inputs;
use src\traits\CreateParams;
class TextInput
{
use CreateParams;
private string $name;
private array $paramsArray;
/**
* @param string $name
* @param array $paramsArray
*/
public function __construct(string $name, array $paramsArray = [])
{
$this->name = $name;
$this->paramsArray = $paramsArray;
}
/**
* @return void
*/
public function create(): void
{
$paramsString = $this->createParams($this->paramsArray);
echo "<input name='$this->name' $paramsString >";
}
/**
* @param string $name
* @param array $paramsArray
* @return void
*/
public static function build(string $name, array $paramsArray = []): void
{
$input = new self($name, $paramsArray);
$input->create();
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace src\traits;
trait CreateOption
{
/**
* @param array $options
* @param $value
* @return string
*/
public function createOption(array $options, $value = null): string
{
$optionsString = "";
foreach ($options as $val => $title){
$selected = $value === $val ? "selected" : "";
$optionsString .= "<option $selected value='$val'>$title</option>";
}
return $optionsString;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace src\traits;
trait CreateParams
{
/**
* @param array $data
* @return string
*/
public function createParams(array $data = []): string
{
$paramsString = "";
foreach($data as $key => $param){
$paramsString .= $key . "='" . $param . "'";
}
return $paramsString;
}
}