Skip to content
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

Closed
quicksketch opened this issue Jun 6, 2012 · 6 comments
Closed

Recurly_CurrencyList should implement Iterator interface #37

quicksketch opened this issue Jun 6, 2012 · 6 comments
Labels
V2 V2 Client

Comments

@quicksketch
Copy link

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:

<?php
foreach ($plan->unit_amount_in_cents as $currency_code => $currency) {
  print $currency->amount() . ' ' . $currency_code
}
?>

Which would print out

50.00 USD
40.00 EUR
30.00 GBP
etc.

@kainosnoema
Copy link

@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: $plan->unit_amount_in_cents['USD'] is almost as easy as the example you gave above.

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.

@quicksketch
Copy link
Author

This info can be obtained by logging into your Recurly account.

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.

@kainosnoema
Copy link

@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.

@quicksketch
Copy link
Author

but would it be possible to check whether Recurly_CurrencyList implements Iterator before calling reset() on it?

Yep! I thought the same thing after posting my comment. I updated it so that others don't blindly copy/paste the previous version.

@kainosnoema
Copy link

@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.

@kainosnoema
Copy link

Just pulled in commits from pull #33, which fixes this.

@bhelx bhelx added the V2 V2 Client label Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
V2 V2 Client
Projects
None yet
Development

No branches or pull requests

3 participants