3

I have a client calling my web service written in PHP. While calling it, the client application gives a little query string to it. I want to parse this string into an array. So with the given example query within an url, I have started doing this:

Url: $select=substringof("customer", tolower(toupper(bla))),test$filter=Name%20eq%20'test'%20and%20toupper(Name)$skip=10$top=2$orderby=day(time)%20desc

.

<?php
function parseOdataRequest($request)
{
    $expression = array();

    // Fetch parameters
    $params = explode('$', $request);

    // Leave empty parameter name alone
    unset($params[0]);
    $params = array_values($params);

    // Check every parameter
    for($i = 0; $i < count($params); $i++)
    {
        // every parameter has to be a name=value pair!
        $param = explode('=', $params[$i]);         


        if(!(count($param) == 2))
            throw new Exception("Invalid parameter. Providence must be name=value");


        // Validate parameter name
        if(in_array($param[0], $this->validParams))
        {                           
            $parseParam = 'parse' . ucfirst(strtolower($param[0]));
            $expression[$param[0]] = $this->$parseParam($param[1]);             
        }
        else            
            throw new Exception("Unkown parameter '" . $param[0] . "'");            
    }
    var_dump($expression);
}
?>

As a result, I have the following $expression:

array (size=5)
  'select' => null
  'filter' => null
  'skip' => null
  'top' => null
  'orderby' => null

What I want is something like the following:

 array (size=5)
  'select' => 
    array (size=2)
      'substringof' => 
        array (size=2)
          0 => string 'customer' (length=8)
          'tolower' => 
            array (size=1)
              'toupper' => 
                array (size=1)
                  0 => string 'bla' (length=3)
      0 => string 'test' (length=4)
  'filter' => 
    array (size=3)
      0 => 
        array (size=3)
          0 => string 'Name' (length=4)
          1 => string 'eq' (length=2)
          2 => string ''test'' (length=6)
      1 => string 'and' (length=3)
      2 => 
        array (size=3)
          'toupper' => 
            array (size=1)
              0 => string 'Name' (length=4)
          0 => string 'eq' (length=2)
          1 => string ''TEST'' (length=6)
  'skip' => int 10
  'top' => int 2
  'order' => 
    array (size=1)
      0 => 
        array (size=2)
          'day' => 
            array (size=1)
              0 => string 'time' (length=4)
          0 => string 'desc' (length=4)

I tried different things, but never was successful. I would appreciate something like an approach to solve this problem.

Marco Klein
  • 683
  • 5
  • 19
  • I have found this: https://github.com/qraftlabs/node-odata-parser This is the way I want the query to be parsed. But I don't want to install node. – Marco Klein Jan 05 '13 at 15:34
  • Considering you asked some months ago and assuming that no such Odata query parser exists in PHP already, you did you consider to write one your own based on the start you've shown in your question? Any improvements/findings so far? – hakre Jun 03 '13 at 10:28
  • Yes I did and was almost successful. It needs some improvements, but in general it is working ;) – Marco Klein Jul 05 '13 at 17:24
  • 2
    Please leave an answer explaining how you solved your question. – hakre Jul 05 '13 at 17:30

0 Answers0