zendframework / zend-validator

Validator component from Zend Framework
BSD 3-Clause "New" or "Revised" License
181 stars 136 forks source link

Method visibility bug #236

Closed allen05ren closed 5 years ago

allen05ren commented 6 years ago

getLength() and setLength() visibility shold be public ?

https://github.com/zendframework/zend-validator/blob/87e743d11d49a157011a49a962c25e7a11adc7c5/src/StringLength.php#L182-L202

Ocramius commented 6 years ago

Probably not: if they are unused, they should instead be dropped.

froschdesign commented 6 years ago

@allen05ren

getLength() and setLength() visibility shold be public ?

Not needed, because the description of validator is:

This validator allows you to validate if a given string is between a defined length.

If you need a strict length, then set the min and max values to the same value:

$validator = new \Zend\Validator\StringLength(
    [
        'min' => 3,
        'max' => 3,
    ]
);

var_dump($validator->isValid('foo')); // true
var_dump($validator->isValid('fo')); // false
var_dump($validator->isValid('foobar')); // false
froschdesign commented 6 years ago

@Ocramius The methods are used in the method isValid:

https://github.com/zendframework/zend-validator/blob/87e743d11d49a157011a49a962c25e7a11adc7c5/src/StringLength.php#L220-L227

froschdesign commented 6 years ago

But the documentation is misleading here:

https://docs.zendframework.com/zend-validator/validators/string-length/#supported-options

The following options are supported for Zend\Validator\StringLength:

  • encoding: Sets the ICONV encoding to use with the string.
  • min: Sets the minimum allowed length for a string.
  • max: Sets the maximum allowed length for a string.
  • length: Holds the actual length of the string.

"Holds" is correct, but it can not be set or is public.

allen05ren commented 6 years ago

@froschdesign Yes, i see. You are right, it can not be set or is public.