Example #1
0
/**
 * Validate attribute
 *
 * @param mixed $val value to compare with (can be one-dimensional array, in this case, every item will be checked)
 * @param mixed $condition value to compare to
 * @param string $op compare operator
 * @return bool true in success, false - otherwise
 */
function fn_promotion_validate_attribute($value, $condition, $op)
{
    $result = false;
    fn_set_hook('pre_validate_promotion_attribute', $value, $condition, $op, $result);
    $val = !is_array($value) ? array($value) : $value;
    if ($op == 'neq') {
        return !in_array($condition, $val);
    }
    foreach ($val as $v) {
        $result = fn_compare_values_by_operator($v, $op, $condition);
        if ($result) {
            return true;
        }
    }
    return false;
}
Example #2
0
 /**
  * @param               $schema
  * @param               $handler_name
  * @param               $data
  * @param Callable|bool $when_found
  *
  * @return bool
  */
 protected static function findHandlerParamsAtData($schema, $handler_name, $data, $when_found = true)
 {
     if (!empty($schema[$handler_name]) && is_array($schema[$handler_name])) {
         if (in_array('*', $schema[$handler_name])) {
             if (is_callable($when_found)) {
                 call_user_func($when_found, '*', $data);
                 return;
             } else {
                 return $when_found;
             }
         }
         foreach ($schema[$handler_name] as $i => $param_name) {
             // An array with comparison condition is passed
             if (is_array($param_name)) {
                 if (!isset($param_name[0], $param_name[1])) {
                     throw new \InvalidArgumentException('Incorrect comparison condition format given');
                 }
                 list($param_name, list($comparison_operator, $right_operand)) = array($i, $param_name);
             }
             $param_name = fn_strtolower(str_replace('%', '', $param_name));
             if (isset($data[$param_name])) {
                 $value = $data[$param_name];
             } elseif (strpos($param_name, '.') !== false) {
                 $value = fn_dot_syntax_get($param_name, $data);
                 if ($value === null) {
                     unset($value);
                 }
             }
             if (isset($value) && (!isset($comparison_operator, $right_operand) || fn_compare_values_by_operator($value, $comparison_operator, $right_operand))) {
                 if (is_callable($when_found)) {
                     call_user_func($when_found, $param_name, $value);
                 } else {
                     return $when_found;
                 }
             }
         }
     }
     return false;
 }