<?php namespace App\Utils;


class ModelUtils
{
    public static function generateFilter (array $request, array $refs) {
        $filter = [];
        foreach ($refs as $key=>$ref) {
            if (isset($request[$ref])) {
                $filter[$key] = $request[$ref];
            }
        }
        return $filter;
    }

    public static function generateSortBy (array $request, array $refs) {
        $valid_params = array_keys($refs);
        if (isset($request['sort'])) {
            $sort_array = explode(',', $request['sort']);
            foreach ($sort_array as $key=>$sort_param) {
                if (in_array($sort_param, $valid_params)) {
                    $sort_array[$key] = $refs[$sort_param];
                }
            }
            $sort_by = join(',', $sort_array);
            return $sort_by;
        } else {
            return '';
        }
    }
}