diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php index 5e629807..ea0811bb 100644 --- a/src/Validator/Multiple.php +++ b/src/Validator/Multiple.php @@ -18,6 +18,8 @@ class Multiple extends Validator */ protected $rules = []; + protected $type = self::TYPE_MIXED; + /** * Constructor * @@ -25,18 +27,19 @@ class Multiple extends Validator * * Example: * - * $multiple = new Multiple($validator1, $validator2, $validator3); + * $multiple = new Multiple([$validator1, $validator2]); + * $multiple = new Multiple([$validator1, $validator2, $validator3], SELF::TYPE_STRING); */ - public function __construct() + public function __construct(array $rules, ?string $type = null) { - // array of all method arguments - $rules = \func_get_args(); - foreach ($rules as $rule) { $this->addRule($rule); } - } + if ($type !== null) { + $this->type = $type; + } + } /** * Add rule * @@ -63,7 +66,7 @@ public function getDescription(): string { $description = ''; foreach ($this->rules as $key => $rule) { - $description .= ++$key . '. ' . $rule->getDescription() . " \n"; + $description .= ++$key . '. ' . $rule->getDescription() . " \\n"; } return $description; @@ -97,7 +100,7 @@ public function isValid(mixed $value): bool */ public function getType(): string { - return self::TYPE_MIXED; + return $this->type; } /** diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php index d882d1bc..e565f523 100644 --- a/tests/Validator/MultipleTest.php +++ b/tests/Validator/MultipleTest.php @@ -10,11 +10,13 @@ class MultipleTest extends TestCase public function setUp(): void { - $this->validator = new Multiple(new Text(20), new URL()); + $this->validator = new Multiple([new Text(20), new URL()]); } public function testIsValid() { + $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()); + // Valid URL but invalid text length $this->assertFalse($this->validator->isValid('http://example.com/very-long-url'));