heyrocker / IfpaApi

A PHP wrapper class to simplify access the IFPA API
The Unlicense
5 stars 1 forks source link

Reformat results to be associative based on IDs #3

Open heyrocker opened 10 years ago

heyrocker commented 10 years ago

In getCalendarEvents() I reformatted the events array so that they were associative instead of the API's default indexed arrays, based on the event ID. I'd like to do that all over the place, it can make the results easier to handle on the calling side.

heyrocker commented 10 years ago

I also made a request to Brian to change this at the API level, so maybe that will happen before I refactor everything to do it here.

haugstrup commented 10 years ago

This is very nice for PHP developers, but kind of a pain for anyone else. They'll end up with an object where they have to iterate over properties. It'll feel very unnatural I'm afraid

heyrocker commented 10 years ago

Won't they end up with an object like that anyways? I admit, I have zero experience using JSON in any other language.

haugstrup commented 10 years ago

I only have experience with scripting languages (Ruby andPython).

If a json object is returned, yes. But other scripting languages have way better array handling/list comprehension than PHP. So it's very simple to e.g. grab a specific player from an array of player objects. It's only in PHP where this is kind of clunky (I'd probably write a helper function that uses array_filter and returns the first element. Ruby has a find function that will return the first element in an rray that matches a truth test).

If you use an object with player_id as the key you still have to be careful to check for presence all the time to avoid undefined index errors (you can't just do if ($players[53]) it has to be if(isset($players[53]))

I would personally prefer to keep lists and lists and not make them into objects. PHP is the odd-man-out because it doesn't distinguish between an array and a hashmap/assoc. array. In other languages you have different methods available on the two types of data structures.

heyrocker commented 10 years ago

Yeah you have to check isset() on indexes, but on the other hand, if there is a specific index you know is in the array, you don't have to iterate through to pull it out. So I feel like that is a wash from a generalized functionality standpoint. However the points about other languages are well taken. I've directed Brian to this discussion so he can take these arguments into account for the API, but I'm still going to do this mod internally for the PHP API since it does make so much sense for PHP.

slamtilt commented 10 years ago

I didn't realize when you asked me Greg, but it does appear to affect other platform / tools as well. I've been playing around with Appery.io (phonegap app maker) and it had issues with one of the resources which I had doing associative arrays. I've also seen it a few other products as well.

I'll leave it as is for now, but if there is anything else you guys would like to see LMK.