public function testBuild()
 {
     $options = array('conditions' => array(array('test_field', '>', 50)), 'values' => array('test_field' => 75));
     $expected = "return ((75 > 50));";
     $this->assertEqual($expected, EvalComparation::build($options));
     $options = array('conditions' => array(array('test_field', '>', 50), '||', array('test_field', '==', 31)), 'values' => array('test_field' => 75));
     $expected = "return ((75 > 50) || (75 == 31));";
     $this->assertEqual($expected, EvalComparation::build($options));
     $options = array('conditions' => array(array('test_field', '==', true), '&&', array('test_field_2', '===', null)), 'values' => array('test_field' => 'test', 'test_field_2' => 'test_2'));
     $expected = "return (('test' == true) && ('test_2' === null));";
     $this->assertEqual($expected, EvalComparation::build($options));
     $options = array('conditions' => array(array('test_field', '==', true), '&&', array(array('test_field_2', '===', null), '||', array('test_field_2', '===', false))), 'values' => array('test_field' => 'test', 'test_field_2' => 'test_2'));
     $expected = "return (('test' == true) && (('test_2' === null) || ('test_2' === false)));";
     $this->assertEqual($expected, EvalComparation::build($options));
 }
Example #2
0
        }
    }
    return false;
};
/**
 * Works same as Lithium's `inRange` validator but require conditions `true` to continue
 * @see \li3_validators\extensions\util\EvalComparation::build()
 */
$customValidators['conditionalInRange'] = function ($value, $format, $options) {
    $options += array('upper' => null, 'lower' => null, 'conditions' => array());
    $conditions = true;
    if (!is_numeric($value)) {
        return false;
    }
    if (!empty($options['conditions'])) {
        $conditions = eval(EvalComparation::build($options));
        if (!$conditions) {
            return true;
        }
    }
    switch (true) {
        case !is_null($options['upper']) && !is_null($options['lower']):
            return $value > $options['lower'] && $value < $options['upper'];
        case !is_null($options['upper']):
            return $value < $options['upper'];
        case !is_null($options['lower']):
            return $value > $options['lower'];
    }
    return is_finite($value);
};
/**