Pop\Validator\Included::evaluate PHP Метод

evaluate() публичный Метод

Method to evaluate the validator
public evaluate ( mixed $input = null ) : boolean
$input mixed
Результат boolean
    public function evaluate($input = null)
    {
        // Set the input, if passed
        if (null !== $input) {
            $this->input = $input;
        }
        // Set the default message
        if (null === $this->defaultMessage) {
            if ($this->condition) {
                $this->defaultMessage = I18n::factory()->__('The value must be included.');
            } else {
                $this->defaultMessage = I18n::factory()->__('The value must not be included.');
            }
        }
        // If input check is an array
        if (is_array($this->input)) {
            if (!is_array($this->value)) {
                $this->value = array($this->value);
            }
            $this->result = true;
            foreach ($this->value as $value) {
                if (in_array($value, $this->input) != $this->condition) {
                    $this->result = false;
                }
            }
            // Else, if input check is a string
        } else {
            if (is_array($this->value)) {
                $this->result = in_array($this->input, $this->value) != $this->condition ? false : true;
            } else {
                if ((strpos((string) $this->input, (string) $this->value) !== false) == $this->condition) {
                    $this->result = true;
                } else {
                    $this->result = false;
                }
            }
        }
        return $this->result;
    }

Usage Example

Пример #1
0
 public function testEvaluateStringFalse()
 {
     $v = new Included(3, null, false);
     $this->assertFalse($v->evaluate(123));
     $v = new Included(4, null, false);
     $this->assertTrue($v->evaluate(123));
     $v = new Included(array(4, 5), null, false);
     $this->assertTrue($v->evaluate(123));
 }
Included