Skip to content

Commit 92854d4

Browse files
committed
Updated constructor to accept type
1 parent a75add2 commit 92854d4

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/Validator/Multiple.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@ class Multiple extends Validator
1818
*/
1919
protected $rules = [];
2020

21+
protected $type = self::TYPE_MIXED;
22+
2123
/**
2224
* Constructor
2325
*
2426
* Multiple constructor can get any number of arguments containing Validator instances using PHP func_get_args function.
2527
*
2628
* Example:
2729
*
28-
* $multiple = new Multiple($validator1, $validator2, $validator3);
30+
* $multiple = new Multiple([$validator1, $validator2]);
31+
* $multiple = new Multiple([$validator1, $validator2, $validator3], SELF::TYPE_STRING);
2932
*/
30-
public function __construct()
33+
public function __construct(array $rules, ?string $type = null)
3134
{
32-
// array of all method arguments
33-
$rules = \func_get_args();
34-
3535
foreach ($rules as $rule) {
3636
$this->addRule($rule);
3737
}
38-
}
3938

39+
if ($type !== null) {
40+
$this->type = $type;
41+
}
42+
}
4043
/**
4144
* Add rule
4245
*
@@ -63,7 +66,7 @@ public function getDescription(): string
6366
{
6467
$description = '';
6568
foreach ($this->rules as $key => $rule) {
66-
$description .= ++$key . '. ' . $rule->getDescription() . " \n";
69+
$description .= ++$key . '. ' . $rule->getDescription() . " \\n";
6770
}
6871

6972
return $description;
@@ -97,7 +100,7 @@ public function isValid(mixed $value): bool
97100
*/
98101
public function getType(): string
99102
{
100-
return self::TYPE_MIXED;
103+
return $this->type;
101104
}
102105

103106
/**

tests/Validator/MultipleTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ class MultipleTest extends TestCase
1010

1111
public function setUp(): void
1212
{
13-
$this->validator = new Multiple(new Text(20), new URL());
13+
$this->validator = new Multiple([new Text(20), new URL()]);
1414
}
1515

1616
public function testIsValid()
1717
{
18+
$this->assertEquals('1. Value must be a valid string and at least 1 chars and no longer than 20 chars \n2. Value must be a valid URL \n', $this->validator->getDescription());
19+
1820
// Valid URL but invalid text length
1921
$this->assertFalse($this->validator->isValid('http://example.com/very-long-url'));
2022

0 commit comments

Comments
 (0)