yehiahamid/easyparse

There is no license information available for the latest version (v1.0.8) of this package.

query sting parser for rest apis supports filterations, sorting, fields, embeds

Installs: 155

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 1

Type:package

v1.0.8 2017-07-01 20:10 UTC

This package is not auto-updated.

Last update: 2024-12-16 13:22:59 UTC


README

Build Status

easyparser is a simple query string parser for rest api php.

it's simply convert the QS to structured types.

you can parse your query string to filter, sort, get certain fields and embed resources

installation using composer

composer require yehiahamid/easyparse

Usage

//values must be inside single or double quotes if it contains white spaces or special chars 'what ever' | "what ever"
$queryStringFilters = "@filters=name eq 'what ever',surname ne 'what ever'";
// otherwise you can use use the value without quotes 
// ex : 

$queryStringFilters = "@filters=id eq 12";
$queryStringFilters = "@filters=id eq anyWordsWithoutspacesOrSpecialChars";

//fields
$queryStringFields = "@fields=name,surname,code";

//name is assc , surname desc
$queryStringOrderBy = "@orderby=name,-surname";

//embed can contains fields,filters,sort and paging 
$queryStringEmbed = "@embed=resourceOne(@fields=name,code)(@filters=nameembed eq 'what ever'),mobiles(@orderby=sortFieldOne)";

$queryString = $queryStringFilters . "&" . $queryStringFields . "&" . $queryStringOrderBy . "&" . $queryStringEmbed;

//in real app you would use $_SERVER['QUERY_STRING'];
//this is optional param if not set it will use server query string 
$parser = new yehiaHamid\easyParse\QueryStringParser($queryString);

$parser->fields(); //return array of fields | null if empty

$parser->orderBy(); //return array of order by object | null if empty
//Or you can set default value
// use - before field for desc order

$parser->orderBy("fieldOne,-field");   

$filtersResult = $parser->filters(); 
//the filter object has three fields 
// the used query string in ex : "@filters=name eq 'what ever',surname ne 'what ever'"

$filtersResult[0]->field // name
$filtersResult[0]->operator // eq
$filtersResult[0]->getOperator() // "="
$filtersResult[0]->value // 'what ever'


$filtersResult[1]->field // surname
$filtersResult[1]->operator // ne
$filtersResult[1]->getOperator() // "!="
$filtersResult[1]->value // 'what ever'

//Or you can set default value for each of the following 
$parser->offset();
$parser->limit();
$parser->perPage();
$parser->page();

// this new function return the value of required key , the key shouldn't be in reserved key words
// reserved key words "field", "fields", "embed", "embeds", "filter", "filters", "orderby", "orderBy", "direction", "search"
$parser->get("key");
// ex: 

$qs = "@anthThing=the value";
$parser->get("anthThing"); //returns the value
// also you can pass second param for default value


$parser->search();


//embed 
$embedResult = $parser->embed(); //return array of embed each object contains filters , sort , fields | empty array 
// the used query string in ex : 
//"@embed=resourceOne(@fields=name,code)(@filters=nameembed eq 'what ever'),mobiles(@orderby=sortFieldOne)"
$embedResult[0]->resource // resourceOne
$embedResult[0]->fields // array of fields  [name,code]
$embedResult[0]->filters // array of filters or empty array 

$embedResult[1]->resource // mobiles
$embedResult[1]->orderBy // array of order by objects 

available filtering operator