Skip to content

Commit 49eefcd

Browse files
committed
update
1 parent 8b0c521 commit 49eefcd

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

src/Elements.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public static function checkbox(string $name = '', string $value = 'on'): Checkb
151151
}
152152

153153
/**
154-
* @param array<string, string> $options
154+
* @param array<string|int, string> $options
155155
*/
156156
public static function select(string $name = '', array $options = []): Select
157157
{

src/Select.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Select implements Control
1010
use HtmlElement;
1111

1212
protected string $name = '';
13-
/** @var array<string,string> $options */
13+
/** @var array<string|int,string> $options */
1414
protected array $options = [];
1515
/** @var string[] $values */
1616
protected array $values = [];
@@ -39,7 +39,7 @@ public function getName(): string
3939
}
4040

4141
/**
42-
* @param array<string,string> $options
42+
* @param array<string|int,string> $options
4343
*/
4444
public function options(array $options): self
4545
{
@@ -98,7 +98,7 @@ public function renderDom(\DOMDocument $doc): \DOMElement
9898
}
9999
foreach ($this->options as $key => $value) {
100100
$option = $doc->createElement('option');
101-
$option->setAttribute('value', $key);
101+
$option->setAttribute('value', strval($key));
102102
if (in_array($key, $this->values)) {
103103
$option->setAttribute('selected', 'selected');
104104
}

src/Validator/CustomFunc.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MintyPHP\Form\Validator;
4+
5+
class CustomFunc implements Validator
6+
{
7+
protected mixed $function;
8+
protected string $message = 'Invalid value';
9+
10+
public function __construct(mixed $function)
11+
{
12+
if (!is_callable($function)) {
13+
throw new \InvalidArgumentException("Invalid function");
14+
}
15+
$this->function = $function;
16+
}
17+
18+
public function evaluate(string $value): bool
19+
{
20+
return call_user_func($this->function, $value) ? true : false;
21+
}
22+
23+
public function message(string $message): self
24+
{
25+
$this->message = $message;
26+
return $this;
27+
}
28+
29+
public function validate(string $value): string
30+
{
31+
return !$this->evaluate($value) ? $this->message : '';
32+
}
33+
}

src/Validator/Expression.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
class Expression implements Validator
66
{
77
protected string $comparator;
8-
protected string $value;
8+
protected string|int|float $value;
99
protected string $message = 'Number must be {comparator} {value}';
1010

11-
public function __construct(string $comparator, string $value)
11+
public function __construct(string $comparator, string|int|float $value)
1212
{
1313
if (!in_array($comparator, ['>', '>=', '<', '<='])) {
1414
throw new \InvalidArgumentException('Comparator not supported');
@@ -42,7 +42,7 @@ public function evaluate(string $value): bool
4242

4343
public function message(string $message): self
4444
{
45-
$this->message = str_replace(['{value}', '{comparator}'], [$this->value, $this->comparator], $message);
45+
$this->message = str_replace(['{value}', '{comparator}'], [strval($this->value), $this->comparator], $message);
4646
return $this;
4747
}
4848

src/Validator/Validators.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function integer(string $message = ''): Validator
4040
return $validator;
4141
}
4242

43-
public static function expression(string $comperator, string $value, string $message = ''): Validator
43+
public static function expression(string $comperator, string|int|float $value, string $message = ''): Validator
4444
{
4545
$validator = new Expression($comperator, $value);
4646
if ($message) {
@@ -75,4 +75,13 @@ public static function regex(string $pattern, string $message = ''): Validator
7575
}
7676
return $validator;
7777
}
78+
79+
public static function function(callable $function, string $message = ''): Validator
80+
{
81+
$validator = new CustomFunc($function);
82+
if ($message) {
83+
$validator->message($message);
84+
}
85+
return $validator;
86+
}
7887
}

0 commit comments

Comments
 (0)