Commando\Option::validate PHP Method

validate() public method

public validate ( mixed $value ) : boolean
$value mixed
return boolean
    public function validate($value)
    {
        if (!is_callable($this->rule)) {
            return true;
        }
        // todo add int, float and regex special case
        // todo double check syntax
        return call_user_func($this->rule, $value);
    }

Usage Example

 public function testRule()
 {
     $option = new Option('f');
     $option->setRule(function ($value) {
         return is_numeric($value);
     });
     $this->assertFalse($option->validate('a'));
     $this->assertFalse($option->validate('abc'));
     $this->assertTrue($option->validate('2'));
     $this->assertTrue($option->validate(2));
     $this->assertTrue($option->validate(0));
     $option->setValue(2);
     $caught = false;
     try {
         $option->setValue('abc');
     } catch (\Exception $e) {
         $caught = true;
     }
     $this->assertTrue($caught);
 }