3 ms·
Have you seen guzzle's implementation of this? It's pretty useful and something I'd love to see split out of guzzle and made its own library. https://github.c
by nubs 12y ago
Have you seen guzzle's implementation of this? It's pretty useful and something I'd love to see split out of guzzle and made its own library.
https://github.com/guzzle/guzzle/blob/master/src/functions.php#L180-L209 https://github.com/guzzle/guzzle/blob/master/src/functions.p... is the actual code, although it's pulled into the guzzle collection: https://github.com/guzzle/guzzle/blob/master/src/Collection.php https://github.com/guzzle/guzzle/blob/master/src/Collection....
- chriswarbo 12y agoThat smells to me: the path argument given to "get_path" is a string of components, separated by "/". The first thing get_path does is split the path apart at "/". Why doesn't it just take an array? That way, it would be a simple reduction: function get_path($data, $path) { return array_reduce( $path, function($result, $index) { return (is_array($result) && isset($result[$index]))? $result[$index] : NULL; }, $data); } Unfortunately PHP's function namespace is separate from its value namespace. We can't define "get_path" using a couple of combinators :( Also, requiring 'strings without slashes' is a pretty bad idea in PHP in particular, since it's meant for Web programming. This means a) we tend to have / in strings due to URLs and b) someone will inevitably pass user input as one of these components, which would allow a very limited form of code injection attack.