zendframework/zend-validator

Method visibility bug

Closed this issue · 5 comments

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

/**
* Returns the length option
*
* @return int
*/
private function getLength()
{
return $this->options['length'];
}
/**
* Sets the length option
*
* @param int $length
* @return StringLength Provides a fluent interface
*/
private function setLength($length)
{
$this->options['length'] = (int) $length;
return $this;
}

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

@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

@Ocramius
The methods are used in the method isValid:

$this->setLength($this->getStringWrapper()->strlen($value));
if ($this->getLength() < $this->getMin()) {
$this->error(self::TOO_SHORT);
}
if (null !== $this->getMax() && $this->getMax() < $this->getLength()) {
$this->error(self::TOO_LONG);
}

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.

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