Tuesday
12
January
2010

Currencies within the view

Today the last missing sheep of my new Zend_Currency implementation has been moved to core.

It’s the view helper for currencies which was missing.
Zend_View_Helper_Currency

Together with the new Zend_Currency implementation it simplifies rendering for all using currencies within their application. Let’s take a short look into an working example…

// Bootstrap
$currency = new Zend_Currency(array('value' => 100, 'locale' => 'fr_FR'));
Zend_Registry::set('Zend_Currency', $currency);

We prepared our currency to be used application wide.
Now the view:

// View
$this->currency();

This would output “100,00 €” (or something related).
Really simple, isn’t it ?

Of course this would not work when you have multiple currencies which you are working with.
Then you must work with other registry keys and give the wished instance.

// Model
$instance_1 = new Zend_Currency(array('value' => 100, 'locale' => 'fr_FR'));
$instance_2 = new Zend_Currency(array('value' => 500, 'locale' => 'en_US'));

And in the view:

// View
$this->currency($instance_1);
$this->currency($instance_2);

Please look into the manual for further informations.
There’s nothing more to say than enjoy my new currency implementation for Zend Framework. ;-)

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Wednesday
6
January
2010

Working with currencies

Ever had to work with currencies in your application?

Well, then there are good news for you:
For Zend Framework 1.10 there is a completly rewritten implementation of Zend_Currency available. The two new main features are calculation and exchanging.

What is meant with these:
Zend_Currency is now able not only to render a currency but you can also calculate currency values with it. Several methods have been added to support you by this task.

$currency = new Zend_Currency(
    array(
        'value' => 1000,
        'currency' => 'EUR',
    )
);
	
$currency->add(4000)->div(5);
print $currency;

As you can see the usage is quite simple…
‘value’ defines the actual amount (money value) this currency holds. When no value is given it defaults to 0. And for calculation there are several convinient methods. These include mathematical methods like:
add(), sub(), mul(), div() and mod().
And there are also comparing methods like:
compare(), equals(), isMore(), isLess()
All of them should provide you with all what you need to calculate currencies.

Until now it was quite simple.
But what should be done when you have to calculate different currencies.
Expect you have USD and EUR. What ever you want to do before processing calculation we need eighter to convert USD to EUR or EUR to USD. As the exchange rates differ from day to day it’s not possible to add fixed rates to Zend_Currency.

But also for this task there is a solution.
As mentioned before Zend_Currency adds a new feature exchanging.
This means that it allows to use Exchange Services. The idea behind is that there are several available online exchange services which can convert a currency into another. By using such a service Zend_Currency is able to convert currencies when needed.

See this simple class as example:

class MyService implements Zend_Currency_CurrencyInterface
{
    public function getRate($from, $to)
    {
        // request the online service
        // and return only a exchange rate for $from Currency to $to Currency
    }
}

Now, to use this exchange service within Zend_Currency you can simply use the ’service’ property.

$currency_2 = new Zend_Currency(
    array(
        'value' => 1000,
        'currency' => 'USD',
        'service' => 'MyService',
    )
);
	
$currency_2->add($currency);

What happens now in the last line from our example?
The add() method detcts that two different currencies are involved. USD and EUR. It will now call the getRate() method of our service to get an exchange rate from EUR to USD.

So what we get in return is “1000 USD + 1000 EUR”… when EUR have an exchange rate of 1.5 we would see 2.500 USD in return.

As you see the usage is really simple and now Zend_Currency is really a useful component for currency handling within your application.

Additionally I also reworked the manual for Zend_Currency completly… now there are 10 sections (instead of 2) with several small examples. It should be much more descriptive.

I hope you like this new (reworked) component.

Happy frameworking,

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top