Ejemplo n.º 1
0
function cast($input_data, $fields)
{
    /*
       Checks expected type in input data values, strips affixes and casts
    */
    foreach ($input_data as $key => $value) {
        if ($fields->{$key}->optype == 'numeric' && is_string($value) || $fields->{$key}->optype != 'numeric' && !is_string($value)) {
            if ($fields->{$key}->optype == 'numeric') {
                $value = strip_affixes($value, $fields->{$key});
                if ($fields->{$key}->optype == "numeric") {
                    $input_data[$key] = floatval($value);
                } else {
                    $input_data[$key] = utf8_encode($value);
                }
            }
        }
    }
    return $input_data;
}
Ejemplo n.º 2
0
function cast($input_data, $fields)
{
    /*
       Checks expected type in input data values, strips affixes and casts
    */
    foreach ($input_data as $key => $value) {
        if ($fields->{$key}->optype == 'categorical' && count($fields->{$key}->summary->categories) == 2 && is_bool($value)) {
            # || in_array($value, array(0,1)))) {
            try {
                $booleans = array();
                $categories = array();
                foreach ($fields->{$key}->summary->categories as $index => $v) {
                    array_push($categories, $v[0]);
                }
                foreach ($categories as $category) {
                    $bool_key = in_array(trim(strtolower($category)), array("true", "1")) ? '1' : '0';
                    $booleans[$bool_key] = $category;
                }
                # converting boolean to the corresponding string
                $input_data[$key] = $booleans[strval($value)];
            } catch (Exception $e) {
                throw new Exception("Mismatch input data type in field \"" . $fields->{$key}->name . "\" for value " . json_encode($value) . ". String expected");
            }
        } else {
            if ($fields->{$key}->optype == 'numeric' && is_string($value) || $fields->{$key}->optype != 'numeric' && !is_string($value)) {
                if ($fields->{$key}->optype == 'numeric') {
                    $value = strip_affixes($value, $fields->{$key});
                    if ($fields->{$key}->optype == "numeric") {
                        $input_data[$key] = floatval($value);
                    } else {
                        $input_data[$key] = utf8_encode($value);
                    }
                }
            } else {
                if ($fields->{$key}->optype == 'numeric' && is_bool($value)) {
                    throw new Exception("Mismatch input data type in field \"" . $fields->{$key}->name . "\" for value " . json_encode($value) . ". Numeric expected");
                }
            }
        }
    }
    return $input_data;
}