Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Should Regex constructor parameter be mandatory? #200

@mrobinsonuk

Description

@mrobinsonuk

I can obtain a Regex validator from the ValidatorPluginManager like so:

$validatorManager = $container->get('ValidatorManager');
$validator = $validatorManager->get('regex', ['pattern' => '/abcd/i']);

However, if I attempt to obtain a Regex validator without the options (to configure it later) I received a PHP Fatal error:

$validatorManager = $container->get('ValidatorManager');
$validator = $validatorManager->get('regex');
$validator->setPattern('/abcd/i');
PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function Zend\Validator\Regex::__construct(), 0 passed in <REDACTED>/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php on line 30 and exactly 1 expected in <REDACTED>/vendor/zendframework/zend-validator/src/Regex.php:51
Stack trace:
#0 <REDACTED>/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php(30): Zend\Validator\Regex->__construct()
#1 <REDACTED>/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(758): Zend\ServiceManager\Factory\InvokableFactory->__invoke(Object(Zend\ServiceManager\ServiceManager), 'Zend\\Validator\\...', NULL)
#2 <REDACTED>/vendor/zendframework/zend-servicemanager/src/Servic in <REDACTED>/vendor/zendframework/zend-validator/src/Regex.php on line 51

Both the approaches of passing options to the ValidatorPluginManager and configuring the validator after invocation work fine for all validators except Regex. From what I can tell, the Regex validator is the only one in the zendframework/zend-validator package where the constructor parameter is mandatory:

/**
 * Sets validator options
 *
 * @param  string|Traversable $pattern
 * @throws Exception\InvalidArgumentException On missing 'pattern' parameter
 */
public function __construct($pattern)
{
    // ...
}

For all the other validators the contructor parameters are optional, which is exactly how the AbstractValidator describes it:

/**
 * Abstract constructor for all validators
 * A validator should accept following parameters:
 *  - nothing f.e. Validator()
 *  - one or multiple scalar values f.e. Validator($first, $second, $third)
 *  - an array f.e. Validator(array($first => 'first', $second => 'second', $third => 'third'))
 *  - an instance of Traversable f.e. Validator($config_instance)
 *
 * @param array|Traversable $options
 */
public function __construct($options = null)
{
    // ...
}

Everything works fine using the first approach above, and it may be a deliberate design decision for the Regex contructor parameter to be mandatory, but it appeared out of place to me so I thought it worth mentioning in case this was an oversight.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions