-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recurly_CurrencyList should implement Iterator interface #37
Comments
@quicksketch that would probably be useful to have, but remember that the only currencies provided will be ones supported by your site. This info can be obtained by logging into your Recurly account. Simply iterating over the currency strings ('USD', 'EUR', 'GBP', etc) and accessing the values in the Recurly_CurrencyList using ArrayAccess like so: Thanks for the suggestion though, we'll add it to our to-do list. You're welcome to submit a pull-request if you'd like to get it in quickly. |
Right, but if you're building integration with a CMS (Drupal in this case), I have no idea what currencies the users of my Drupal module will be using. I suppose I could ask them to list the currencies manually in an administrative interface, but that seems unnecessary considering the Recurly API. As an interesting workaround I found that calling reset() on the object will give me the array of currencies that I can loop over. I can't say I understand why this works though. To prevent problems with Iterator is actually implemented, I added a ternary operator, since reset() on an actual array would give you the first item, not the array itself. <?php
$currencies = in_array('Iterator', class_implements($plan->unit_amount_in_cents)) ? $plan->unit_amount_in_cents : reset($plan->unit_amount_in_cents);
foreach ($currencies as $currency) {
print $currency->amount() . ' ' . $currency->currencyCode;
}
?> EDIT: Updated example with ternary. |
@quicksketch great example of a use-case where having an iterable list is important. I think that's something we'd like to support. I can't for the life of me understand why reset() gives you an array, but would it be possible to check whether Recurly_CurrencyList implements Iterator before calling reset() on it? That would allow you to ensure forwards compatibility when we fix that. |
Yep! I thought the same thing after posting my comment. I updated it so that others don't blindly copy/paste the previous version. |
@quicksketch pull #33 has a great solution to this, and I've asked @nicholasrawlings if he wouldn't mind adding the fix to Recurly_CurrencyList as well. |
Just pulled in commits from pull #33, which fixes this. |
Right now I can't find a way to see which currencies are inside the Recurly_CurrencyList class. Because $currencies is declared as private, there's no way I can get a list of which currencies are in the list. I suppose I could caste to a string and then explode on commas, but that seems awfully hackish.
I think it would be best if Recurly_CurrencyList implemented the Iterator interface (http://us.php.net/manual/en/class.iterator.php), so you could do things like this:
Which would print out
50.00 USD
40.00 EUR
30.00 GBP
etc.
The text was updated successfully, but these errors were encountered: