Skip to content

Commit 8c1a704

Browse files
committed
refactor: Move isEmpty() to BaseField class
1 parent 547c3c5 commit 8c1a704

8 files changed

+32
-28
lines changed

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/AllOrNoneConstraint.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public function validate($validator, $attribute): bool
5353
$countEmpty = 0;
5454
$countNotEmpty = 0;
5555
foreach ($Fields as $fieldname) {
56-
if ($this->isEmpty($parentNode->$fieldname)) {
57-
$countEmpty++;
58-
} else {
56+
if ($parentNode->$fieldname->isNotEmpty()) {
5957
$countNotEmpty++;
58+
} else {
59+
$countEmpty++;
6060
}
6161
}
6262
if ($countEmpty != 0 && $countNotEmpty != 0) {

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/BaseConstraint.php

-15
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,6 @@
3333

3434
abstract class BaseConstraint extends BaseValidator
3535
{
36-
/**
37-
* check if field is empty (either boolean field as false or an empty field)
38-
* @param $node
39-
* @return bool
40-
*/
41-
public function isEmpty($node)
42-
{
43-
$node_class = get_class($node);
44-
if ($node_class == "OPNsense\Base\FieldTypes\BooleanField") {
45-
return empty((string)$node);
46-
} elseif (empty((string)$node->getCurrentValue()) || (string)$node->getCurrentValue() == "0") {
47-
return true;
48-
}
49-
return false;
50-
}
5136

5237
/**
5338
* @param $validator

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/ComparedToFieldConstraint.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public function validate($validator, $attribute): bool
5454
$node = $this->getOption('node');
5555
$field_name = $this->getOption('field');
5656
$operator = $this->getOption('operator');
57-
if ($node && !($this->isEmpty($node) || empty($operator) || empty($field_name))) {
57+
if ($node?->isNotEmpty() && !(empty($operator) || empty($field_name))) {
5858
$parent_node = $node->getParentNode();
5959
$other_node_content = $parent_node->$field_name;
6060

6161
// if the other field is not set, or invalid type -> ignore this constraint
6262
if (
63-
$this->isEmpty($other_node_content) ||
64-
!$node->isNumeric() && !$other_node_content->isNumeric()
63+
!$other_node_content?->isNotEmpty() ||
64+
!$node->isNumeric() && !$other_node_content->isNumeric()
6565
) {
6666
return true;
6767
}

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/DependConstraint.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ public function validate($validator, $attribute): bool
5050
$node = $this->getOption('node');
5151
if ($node) {
5252
$parentNode = $node->getParentNode();
53-
if ($this->isEmpty($node)) {
53+
if ($node->isEmpty()) {
5454
foreach (array_unique($this->getOptionValueList('addFields')) as $fieldname) {
55-
if (!$this->isEmpty($parentNode->$fieldname)) {
55+
// When is null or not empty
56+
if ($parentNode->$fieldname?->isNotEmpty()) {
5657
$this->appendMessage($validator, $attribute);
5758
break;
5859
}

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/SetIfConstraint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function validate($validator, $attribute): bool
5656
$check = $this->getOption('check');
5757
if ($node) {
5858
$parentNode = $node->getParentNode();
59-
if ($this->isEmpty($node) && (string)$parentNode->$field_name == $check) {
59+
if ($node->isEmpty() && (string)$parentNode->$field_name == $check) {
6060
$this->appendMessage($validator, $attribute);
6161
}
6262
}

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/SingleSelectConstraint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function validate($validator, $attribute): bool
5353
$Fields = array_unique(array_merge(array($nodeName), $this->getOptionValueList('addFields')));
5454
$countNotEmpty = 0;
5555
foreach ($Fields as $fieldname) {
56-
if (!$this->isEmpty($parentNode->$fieldname)) {
56+
if (!$parentNode->$fieldname?->isNotEmpty()) {
5757
$countNotEmpty++;
5858
}
5959
}

src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function validate($validator, $attribute): bool
5151
$node = $this->getOption('node');
5252
$fieldSeparator = chr(10) . chr(0);
5353
if ($node) {
54-
if (!$node->isRequired() && $this->isEmpty($node)) {
54+
if (!$node->isRequired() && $node->isEmpty()) {
5555
return true;
5656
}
5757
$mdl = $node->getParentModel();

src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public function __toString()
368368
* @return null|string field current value
369369
*/
370370
public function getCurrentValue(): ?string {
371-
return (string)$this->internalValue;
371+
return trim((string)$this->internalValue);
372372
}
373373

374374
/**
@@ -496,6 +496,24 @@ public function getChild($name)
496496
}
497497
}
498498

499+
/**
500+
* check if current value is empty (either boolean field as false or an empty field)
501+
* @return bool
502+
*/
503+
public function isEmpty()
504+
{
505+
return empty($this->getCurrentValue()); // "0" is already considered false by `empty()`
506+
}
507+
508+
/**
509+
* check if current value is NOT empty (either boolean field as false or an empty field)
510+
* @return bool
511+
*/
512+
public function isNotEmpty()
513+
{
514+
return !$this->isEmpty();
515+
}
516+
499517
public function isRequired()
500518
{
501519
return $this->internalIsRequired;
@@ -507,7 +525,7 @@ public function isRequired()
507525
*/
508526
public function isEmptyAndRequired()
509527
{
510-
if ($this->internalIsRequired && ($this->internalValue == "" || $this->internalValue == null)) {
528+
if ($this->internalIsRequired && $this->isEmpty()) {
511529
return true;
512530
} else {
513531
return false;

0 commit comments

Comments
 (0)