code
<?php
declare(strict_types=1);
interface RequestInterface
{
public function get(string $name): array;
}
class Request implements RequestInterface
{
public function get(string $name): array
{
$request = $_REQUEST[$name] ?? '';
if (!is_array($request)) {
$request = [$request];
}
return $request;
}
}
interface ElementInterface
{
public function addClass(array $class): self;
public function getName(): string;
public function getValue(): array;
public function getHtml(): string;
}
abstract class ElementAbstract implements ElementInterface
{
private string $name;
private array $class = [];
private Request $request;
public function __construct(Request $request, string $name)
{
$this->request = $request;
$this->name = $name;
}
public function addClass(array $class): self
{
$this->class = array_merge($this->class, $class);
return $this;
}
public function getName(): string
{
return $this->name;
}
public function getValue(): array
{
return $this->request->get($this->name);
}
protected function getClass(): string
{
return !empty($this->class) ? ' class ="' . implode(' ', $this->class) . '"' : '';
}
abstract public function getHtml(): string;
}
interface ElementWithItemInterface
{
public function addItem(array $item): self;
public function getItem(): array;
}
abstract class ElementWithItem extends ElementAbstract implements ElementWithItemInterface
{
private array $items = [];
public function __construct(Request $request, string $name)
{
parent::__construct($request, $name);
}
public function addItem(array $item): self
{
$this->items = array_merge($this->items, $item);
return $this;
}
public function getItem(): array
{
return $this->items;
}
public function getValue(): array
{
return array_intersect($this->getItem(), parent::getValue());
}
}
final class Radio extends ElementWithItem
{
public function getHtml(): string
{
$html = '';
foreach ($this->getItem() as $id => $value) {
$checked = in_array($value, $this->getValue()) ? ' checked' : '';
$html .= '<div class="form-check form-switch"><input' . $this->getClass() . ' name="' . $this->getName()
. '" type="radio" value="' . $value . '" id="' . $id . '"' . $checked . '><label for="'
. $id . '">' . $value . '</label></div>';
}
return $html;
}
}
final class Checkbox extends ElementWithItem
{
public function getHtml(): string
{
$html = '';
foreach ($this->getItem() as $id => $value) {
$checked = in_array($value, $this->getValue()) ? ' checked' : '';
$html .= '<div class="form-check form-switch"><input' . $this->getClass() . ' name="' . $this->getName()
. '[]" type="checkbox" value="' . $value . '" id="' . $id . '"' . $checked . '><label for="'
. $id . '">' . $value . '</label></div>';
}
return $html;
}
}
// Клиентский код
$request = new Request();
$pencil = new Radio($request,'pencil');
$pencil->addItem([
'color1' => 'Красный карандаш',
'color2' => 'Белый карандаш',
'color3' => 'Синий карандаш',
])->addClass([
'form-check-input'
]);
$paper = new Checkbox($request,'paper');
$paper->addItem([
'format2' => 'A2',
'format3' => 'A3',
'format4' => 'A4',
])->addClass([
'form-check-input'
]);
// Вывод шаблона
?>
<html>
<head>
<title></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<form>
<h2>radio</h2>
<?php echo $pencil->getHtml(); ?>
<hr />
<h2>checkbox</h2>
<?php echo $paper->getHtml(); ?>
<hr />
<input type="submit" value="Отправить">
</form>
</div>
</body>
</html>
вот первый момент:
public function addClass(array $class): self
{
$this->class = array_merge($this->class, $class);
return $this;
}
вот второй:
public function addItem(array $item): self
{
$this->items = array_merge($this->items, $item);
return $this;
}
как понимать когда пишут :self и return $this (почему не $this->items, это будет то же самое или нет)