ioseb / uri-template

PHP extension implementation of RFC-6570(URI Template) in C - https://datatracker.ietf.org/doc/html/rfc6570
http://pecl.php.net/package/uri_template
Other
72 stars 3 forks source link

Nested arrays and * expansion #2

Closed mtdowling closed 10 years ago

mtdowling commented 11 years ago

It looks like adding nested arrays with * variable expansion will throw a notice and convert the expansion to 'Array':

echo uri_template('http://foo!{foo*}', array('foo' => array('baz' => array('bar' => 123))));
PHP Notice:  Array to string conversion in php shell code on line 1
http://foo!baz=Array

I would expect this to use PHP's query string formatting to output the following:

http://foo!baz%5Bbar%5D=123

That's the current behavior of Guzzle's URI template expansion: https://github.com/guzzle/guzzle/issues/90

ioseb commented 11 years ago

@mtdowling

hmm... Sounds interesting, but i'm sure it's something proprietary. There are a lot of issues with nested arrays of arrays(or objects):

  1. It adds needless complexity to the implementation;
  2. Adds proprietary non interoperable behaviour;

Current implementation is in tact with spec which doesn't define similar features. URI templates are not local to PHP and thus i'd prefer to ignore such proprietary things. Furthermore, what should be expected result of this:

echo uri_template('http://foo!{foo*}', array(
  'foo' => array(
    'baz' => array(
      'bar' => (object) array(
        'prop1' => array(1, 2, array('key1' => 'foo bar prop1', 17), 4),
        'prop2' => null
      )
    )
  )
));

Here is a uritemplate-test project which is maintained by URI Template spec authors. All of tests are already integrated in Pecl URI Template extension and none of these tests contain such nested structures. I'd be happy to move this discussion further, we can ask spec authors regarding this.

ioseb commented 10 years ago

@mtdowling i'm closing this ticket, we can reopen it if need be.

webdobe commented 8 years ago

Just out of curiosity how is this "proprietary"? The request is for the same functionality that http_build_query() provides. Is it proprietary only to php? or... I believe other languages do this as well...

Example #3 http_build_query() with complex arrays

<?php
$data = array('user'=>array('name'=>'Bob Smith',
                            'age'=>47,
                            'sex'=>'M',
                            'dob'=>'5/12/1956'),
              'pastimes'=>array('golf', 'opera', 'poker', 'rap'),
              'children'=>array('bobby'=>array('age'=>12,
                                               'sex'=>'M'),
                                'sally'=>array('age'=>8,
                                               'sex'=>'F')),
              'CEO');

echo http_build_query($data, 'flags_');
?>

this will output : (word wrapped for readability)

user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M& user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera& pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12& children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8& children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO

ioseb commented 8 years ago

@webdobe Maybe this(or similar) behaviour is implemented in another languages, these are not defined by IETF RFCs(do not confuse with PHP RFCs). URI Template extension implements IETF RFC-6570 that also depends on IETF RFC-3986. I do believe implementation should conform to these specs despite the fact that it is implemented for PHP.

P.S. URI encoded by http_build_query(...) won't be parsed as a nested associative array in other platforms.