Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store client/server string filtering directives for realtime use

I have an application that grabs rows from external APIs, and sends them to the browser without storing in the database. The database only holds the URLs for the API requests.

The rows received from the APIs needs to be filtered per row, either server(PHP) or client side, and the filters need to be stored in the database. A filter for a row can for example be substring or replace.

I assume you never want to have javascript/PHP code in the database, but how else could this be done? Are there any appropriate javascript solutions?

What I want in my database table is this:

Row ------------------ API URL ------------------- Filter

Row 1 --------------- api.com/get_data-----------STRING FILTER HERE

Row 2 --------------- api.com/get_more_data---STRING FILTER HERE

Where the "Filter" contains instructions on how to filter the string in the API result.

like image 320
joakimdahlstrom Avatar asked Apr 29 '14 13:04

joakimdahlstrom


2 Answers

Depends on the kind of filter you want to use, storing codes in database might be needed, and I don't think there's a way around it since you don't have any restriction for the filter. However, you could try the following:

1/ Function names: if there's only a handful number of filters, you can write functions for them and store the filter name in the database.

2/ Regular Expression: RegExp is enough for doing basic substring and replace, and filtering. You can even have multiple RegExp per URL for complex filters.

like image 187
tungd Avatar answered Oct 13 '22 16:10

tungd


How about storing the class name of the filter, instantiating an instance, and then running data through the filter that way?

So you'd have filters set up like:

abstract class FilterAbs {
    abstract public function filter($to_filter);
}

class TrimFilter extends FilterAbs {
    public function filter($to_filter) {
        return trim($to_filter);
    }
}

Then your filter column would have a list of filters (e.g. "TrimFilter, EmailFilter"). You'd create instances and filter like this:

$to_filter = "blah";

$filter_col = "TrimFilter, EmailFilter";

$filters = explode(",", $filter_col);

foreach($filters as $filter_name) {
    $filter_name = trim($filter_name);
    $filter_obj = new $filter_name();
    if( !is_a($filter_obj, "FilterAbs") ) continue;
    $to_filter = $filter_obj->filter($to_filter);
}

If you needed to have params, you could store them as a json encoded array, and use ReflectionClass to create an instance using params (c.f. instantiate a class from a variable in PHP?).

like image 38
Cully Avatar answered Oct 13 '22 16:10

Cully