From d7206040cf8f5926f122d52ddcee76d2dd60717a Mon Sep 17 00:00:00 2001 From: Daniel Ronkainen Date: Sun, 26 Oct 2025 11:40:24 +0100 Subject: [PATCH 1/2] refactor: clean up and organize codebase --- AbstractArguments.php | 16 ++++++++-------- AbstractFields.php | 22 +++++++++++----------- AbstractFormFields.php | 8 ++++---- Arguments.php | 11 ++++++----- Examples/TestFormFields.php | 1 - Fields.php | 14 +++++++------- Validate.php | 16 ++++++++-------- composer.json | 2 +- 8 files changed, 45 insertions(+), 45 deletions(-) diff --git a/AbstractArguments.php b/AbstractArguments.php index bf93a6a..828cbcf 100755 --- a/AbstractArguments.php +++ b/AbstractArguments.php @@ -17,16 +17,16 @@ abstract class AbstractArguments protected $attr; protected $fieldType; - protected $attrArr = array(); + protected $attrArr = []; protected $name; protected $value; protected $default; protected $label; protected $description; - protected $validate = array(); - protected $items = array(); - protected $config = array(); - protected $fields = array(); + protected $validate = []; + protected $items = []; + protected $config = []; + protected $fields = []; protected $identifier; /** @@ -141,9 +141,9 @@ protected function setAttr(): string protected function groupFields(callable $callback, bool $manipulateName = true) { $out = ""; - + if (!is_array($this->value)) { - $this->value = array(0); + $this->value = [0]; } // This will add new value foreach ($this->value as $k => $a) { $outB = ""; @@ -191,7 +191,7 @@ protected function isChecked($val): bool protected function lastKey(): int { $findKey = 0; - if (!is_null($this->value) && is_array($this->value)) { + if ($this->value !== null && is_array($this->value)) { $findKey = $this->value; krsort($findKey); $findKey = key($findKey); diff --git a/AbstractFields.php b/AbstractFields.php index c51650e..890d0d6 100755 --- a/AbstractFields.php +++ b/AbstractFields.php @@ -27,9 +27,9 @@ abstract class AbstractFields protected $name = "form"; protected $fields; protected $type; - protected $args = array(); - protected $values = array(); - protected $inpArr = array(); + protected $args = []; + protected $values = []; + protected $inpArr = []; /** * Form creator @@ -49,10 +49,10 @@ public function __construct(FormFieldsInterface $fields) public function __call($method, $args): FormFieldsInterface { // Reset fields instance - if (!is_null($this->type)) { + if ($this->type !== null) { $this->fields = $this->fields->withField(); } - + if ($this instanceof FieldInterface) { $this->fields->setFieldInst($this); } @@ -109,11 +109,11 @@ public function setValues(array|object $values): void } } - /** - * Delete search and find a array item - * @param array $key Possible to traverse to form field with the comma select property - * @return void - */ + /** + * Delete search and find a array item + * @param array $key Possible to traverse to form field with the comma select property + * @return void + */ final protected function findDelete(array &$array, array $key): void { $firstKey = array_shift($key); @@ -133,7 +133,7 @@ final protected function findDelete(array &$array, array $key): void */ final protected function resolveGrpName(): array { - $get = array(); + $get = []; foreach ($this->inpArr as $a1) { foreach ($a1 as $k => $a2) { if (isset($a2['type'])) { diff --git a/AbstractFormFields.php b/AbstractFormFields.php index f4272bd..27e4ccc 100755 --- a/AbstractFormFields.php +++ b/AbstractFormFields.php @@ -26,12 +26,12 @@ public function container(callable $callback): string $out = ""; $out .= "
"; - if (!is_null($this->label)) { + if ($this->label !== null) { $boolLength = (isset($this->validate['length'][0]) && $this->validate['length'][0] > 0); $req = ($boolLength) ? "*" : ""; $out .= ""; } - if (!is_null($this->description)) { + if ($this->description !== null) { $out .= "
{$this->description}
"; } $out .= $callback(); @@ -183,10 +183,10 @@ public function group() $lastKey = $this->lastKey(); $out = "
attr}data-key=\"{$lastKey}\">"; - if (!is_null($this->label)) { + if ($this->label !== null) { $out .= ""; } - if (!is_null($this->description)) { + if ($this->description !== null) { $out .= "
{$this->description}
"; } diff --git a/Arguments.php b/Arguments.php index 68daa39..6699185 100755 --- a/Arguments.php +++ b/Arguments.php @@ -1,4 +1,5 @@ default = $default; } return $this; @@ -149,7 +150,7 @@ public function name(string $name): self $this->inst->setValidateData($this->identifier, [ "id" => ($this->rows['id'] ?? 0), - "type" => (!is_null($this->fieldType) ? $this->fieldType : "text"), + "type" => ($this->fieldType !== null ? $this->fieldType : "text"), "validate" => $this->validate, "default" => $this->default, "config" => $this->config @@ -168,7 +169,7 @@ public function name(string $name): self */ public function value(?string $val = null): self { - if (!is_null($val)) { + if ($val !== null) { $this->value = $val; } elseif (is_array($this->nameExp) && count($this->nameExp) > 0) { $this->valueShifting($this->nameExp, $val); @@ -185,7 +186,7 @@ public function value(?string $val = null): self protected function valueShifting(array $exp, ?string $fallback): void { $values = $this->inst->getValues(); - if (!is_null($values)) { + if ($values !== null) { // Can convert obj to arr if needed $values = (array)$values; $first = array_shift($exp); @@ -195,7 +196,7 @@ protected function valueShifting(array $exp, ?string $fallback): void $this->value = $this->json($this->value); foreach ($exp as $item) { $item = htmlentities(trim($item)); - if (!is_null($this->value)) { + if ($this->value !== null) { $this->value = (isset($this->value[$item]) && is_array($this->value[$item])) ? $this->value[$item] : $fallback; } } diff --git a/Examples/TestFormFields.php b/Examples/TestFormFields.php index 172ea02..88372f8 100755 --- a/Examples/TestFormFields.php +++ b/Examples/TestFormFields.php @@ -13,7 +13,6 @@ class TestFormFields extends AbstractFormFields { - /** * Input text (Take a look at AbstractFormFields) * @return string diff --git a/Fields.php b/Fields.php index 0ced46b..16d8797 100755 --- a/Fields.php +++ b/Fields.php @@ -14,7 +14,7 @@ class Fields extends AbstractFields implements FieldInterface { private $buildArr; - private $validateData = array(); + private $validateData = []; /** * Get form name @@ -41,7 +41,7 @@ public function getFormData(): array */ public function hasFormData(?string $name = null): bool { - if (is_null($name)) { + if ($name === null) { $name = $this->name; } return (isset($this->inpArr[$name])); @@ -81,7 +81,7 @@ public function getValues(): mixed */ public function add($fields, ?string $name = null): self { - if (is_null($name)) { + if ($name === null) { $name = $this->name; } $this->inpArr[$name] = $fields; @@ -171,7 +171,7 @@ public function getValidateDataRow(string $key): array */ public function build(): void { - $this->validateData = array(); + $this->validateData = []; foreach ($this->inpArr as $key => $array) { $this->buildArr[$key] = $this->html($array); } @@ -195,7 +195,7 @@ public function withBuild(): static */ public function get(): string { - if (!is_null($this->type) && method_exists($this->fields, $this->type)) { + if ($this->type !== null && method_exists($this->fields, $this->type)) { $get = call_user_func_array([$this->fields, $this->type], $this->args); return $get; } @@ -209,7 +209,7 @@ public function get(): string */ public function hasForm(?string $name = null): bool { - if (is_null($name)) { + if ($name === null) { $name = $this->name; } return (isset($this->buildArr[$name])); @@ -222,7 +222,7 @@ public function hasForm(?string $name = null): bool */ public function getForm(?string $name = null): string { - if (is_null($name)) { + if ($name === null) { $name = $this->name; } if (!$this->hasFormData($name)) { diff --git a/Validate.php b/Validate.php index 76e0fef..bbcf7f8 100755 --- a/Validate.php +++ b/Validate.php @@ -3,7 +3,7 @@ namespace MaplePHP\Form; use MaplePHP\Form\Interfaces\FieldInterface; -use MaplePHP\Validate\Inp; +use MaplePHP\Validate\Validator; use MaplePHP\DTO\Format\Local; class Validate @@ -14,8 +14,8 @@ class Validate private $validArr; private $fields; private $post; - private $request = array(); - private $files = array(); + private $request = []; + private $files = []; private $local; private $value; private $length = 0; @@ -80,7 +80,7 @@ public function execute(): ?array $this->fields->setValues($this->post); $this->fields->build(); - $postArr = array(); + $postArr = []; $arr = $this->fields->getValidateData(); foreach ($arr as $name => $arr) { $field = $this->fields->{$arr['type']}(); @@ -95,7 +95,7 @@ public function execute(): ?array if (isset($arr['validate'])) { $this->value = htmlspecialchars((string)$value); $this->length = strlen($this->value); - $this->validate = Inp::value($this->value); + $this->validate = Validator::value($this->value); if ($error = $this->isInvalid($arr['validate'])) { $this->validArr[$nameKey] = $error; @@ -151,7 +151,7 @@ protected function isInvalid($arr): bool|array private function validateWithMethod(string $method, ?array $args, bool $valFilledIn): bool { if (!is_array($args)) { - $args = array(); + $args = []; } $object = call_user_func_array([$this->validate, $method], $args); return (($valFilledIn && $this->length > 0 && !$object) || (!$valFilledIn && !$object)); @@ -175,9 +175,9 @@ private function buildMessage(string $method, ?array $args, string $message): ar * @param array $args sprint push possible values * @return string */ - protected function message(string $key, array $args = array()) + protected function message(string $key, array $args = []) { - if (!is_null($this->local)) { + if ($this->local !== null) { return $this->local->get($key, $key, $args); } return $key; diff --git a/composer.json b/composer.json index d91976b..1438573 100755 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=8.0", - "maplephp/dto": "^1.0", + "maplephp/dto": "^2.0", "maplephp/validate": "^1.0" }, "autoload": { From b789f7c5c88957ac16bf7fa0faa023a7393e3067 Mon Sep 17 00:00:00 2001 From: Daniel Ronkainen Date: Sun, 26 Oct 2025 12:21:06 +0100 Subject: [PATCH 2/2] chore: minor adjustments --- composer.json | 57 ++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/composer.json b/composer.json index 1438573..39fbc89 100755 --- a/composer.json +++ b/composer.json @@ -1,29 +1,34 @@ { - "name": "maplephp/form", - "type": "library", - "description": "Develop advanced, consistent, and secure forms that are also very easy to validate.", - "keywords": ["form builder", "form", "builder", "validate"], - "homepage": "https://wazabii.se", - "license": "Apache-2.0", - "authors": [ - { - "name": "Daniel Ronkainen", - "email": "daniel.ronkainen@wazabii.se" - }, - { - "name": "MaplePHP", - "homepage": "https://wazabii.se" - } - ], - "require": { - "php": ">=8.0", - "maplephp/dto": "^2.0", - "maplephp/validate": "^1.0" + "name": "maplephp/form", + "type": "library", + "description": "Develop advanced, consistent, and secure forms that are also very easy to validate.", + "keywords": [ + "form builder", + "form", + "builder", + "validate" + ], + "homepage": "https://wazabii.se", + "license": "Apache-2.0", + "authors": [ + { + "name": "Daniel Ronkainen", + "email": "daniel.ronkainen@wazabii.se" }, - "autoload": { - "psr-4": { - "MaplePHP\\Form\\": "" - } - }, - "minimum-stability": "dev" + { + "name": "MaplePHP", + "homepage": "https://wazabii.se" + } + ], + "require": { + "php": ">=8.2", + "maplephp/dto": "^3.1", + "maplephp/validate": "^2.0" + }, + "autoload": { + "psr-4": { + "MaplePHP\\Form\\": "" + } + }, + "minimum-stability": "dev" } \ No newline at end of file