Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">7.0",
"php": ">=8.0",
"ext-dom": "*"
}
}
21 changes: 0 additions & 21 deletions composer.lock

This file was deleted.

63 changes: 48 additions & 15 deletions src/Field.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Formify;

use DOMDocument;
use DOMElement;
use DOMException;
use Exception;

class Field
{
Expand All @@ -12,46 +16,72 @@ class Field
private string $value;
private string $placeholder;

public function __construct(array $attr = [])
/**
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
$this->name = $attr['name'] ?? '';
$this->placeholder = $attr['placeholder'] ?? '';
$this->type = $attr['type'] ?? 'text';
$this->style = $attr['class'] ?? '';
$this->value = $attr['value'] ?? '';
$this->name = $attributes['name'] ?? '';
$this->placeholder = $attributes['placeholder'] ?? '';
$this->type = $attributes['type'] ?? 'text';
$this->style = $attributes['class'] ?? '';
$this->value = $attributes['value'] ?? '';
}

public function name(string $name): self
/**
* @param string $name
* @return Field
*/
public function name(string $name): Field
{
$this->name = $name;
return $this;
}

public function placeholder(string $placeholder): self
/**
* @param string $placeholder
* @return Field
*/
public function placeholder(string $placeholder): Field
{
$this->placeholder = $placeholder;
return $this;
}

public function type(string $type): self
/**
* @param string $type
* @return Field
*/
public function type(string $type): Field
{
$this->type = $type;
return $this;
}

public function style(string $style): self
/**
* @param string $style
* @return Field
*/
public function style(string $style): Field
{
$this->style = $style;
return $this;
}

public function value(string $value): self
/**
* @param string $value
* @return Field
*/
public function value(string $value): Field
{
$this->value = $value;
return $this;
}

public function render(): mixed
/**
* @return DOMElement|null
*/
public function render(): ?DOMElement
{
try {
$doc = new DOMDocument();
Expand All @@ -74,8 +104,11 @@ public function render(): mixed
$doc->appendChild($input_elm);
return $doc->documentElement;
}
catch (\DOMException|\Exception $e) {
echo "An error occurred while rendering the field: " . $e->getMessage();
catch (DOMException|Exception $e) {
echo sprintf(
'An error occurred while rendering the field: %s',
$e->getMessage()
);
return null;
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/Form.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Formify;

Expand All @@ -9,8 +10,14 @@ class Form
private string $action;
private string $method;
private string $enctype;
/**
* @var Field[]
*/
private array $fields;

/**
* @param array $config
*/
public function __construct(array $config = [])
{
$this->action = $config['action'] ?? '';
Expand All @@ -19,13 +26,19 @@ public function __construct(array $config = [])
$this->fields = [];
}

/**
* @return Field
*/
public function field(): Field
{
$field = new Field;
$this->fields[] = $field;
return $field;
}

/**
* @return void
*/
public function render(): void
{
try {
Expand All @@ -44,7 +57,7 @@ public function render(): void

foreach ($this->fields as $field) {
$input = $field->render();
if ($field === null) {
if ($input === null) {
continue;
}

Expand All @@ -56,7 +69,7 @@ public function render(): void
echo $doc->saveHTML();
}
catch (\DOMException|\Exception $e) {
echo "An error occurred while rendering the form: " . $e->getMessage();
echo sprintf('An error occurred while rendering the form: %s', $e->getMessage());
}
}
}