Wednesday
13
January
2010

Boolean filtering

Hy fellows,

another one of my new components has been moved to core…

Zend_Filter_Boolean

You say that you don’t need this filter? You can simply cast to boolean?
Let me describe you why you are wrong.

As you can imagine Zend_Filter_Boolean is a filter. Like all other filters it can be used in combination with other components which allow to add filters on themself. For example Zend_Form, Zend_File, Zend_Dojo and so on…

So far so good.
But what’s the benefit of Zend_Filter_Boolean?

Let’s take a look into examples:

$form->addFilter('Boolean');

Not very exciting…

You think this could also be done by using the following?

$value = (boolean) $form->element->getValue();

Doesn’t look as nice as the above ;-)

Now let’s try something which is not possible by a simple cast. Expect that we’re reading data from database. Our table could could look like this:

Col User | Col Info
---------+---------
patric   | 0
ralph    | 1
thomas   | -1
fred     | false
bruno    | unknown

So 0 would mean “FALSE”, other integers would mean “TRUE”, and anything different would mean “NOT SET”. Still, default is true, so -1 would also point to true. Now the filter within your model would look like this:

$filter = new Zend_Filter_Boolean(Zend_Filter_Boolean::INTEGER);

This would work like a cast, but only on integer values. You may note the “unknown” string in our table.
Now the filtered data would be returned like this:

Col User | Col Info
---------+---------
patric   | FALSE
ralph    | TRUE
thomas   | TRUE
fred     | TRUE
bruno    | unknown

Nice isn’t it? Note the TRUE for fred because we filtered only integers and said that all other types have to be seen as true.

Ok… your model can not act on “unknown”? Also no problem for us. Let’s cast to boolean:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::INTEGER, 'casting' => true));

Now our above data would would be returned like this:

Col User | Col Info
---------+---------
patric   | FALSE
ralph    | TRUE
thomas   | TRUE
fred     | TRUE
bruno    | TRUE

As you can see our “unknown” value is casted. But different to a simple case also the value “false” from fred is filtered to “true” because it’s not an integer value.

What could this also used for?
Expect we have an form element with 3 states (3 radiobuttons): “1”, “0” and “don’t know”. And we want to have these 3 states also within our model as “true”, “false” and “-1”. On “-1” we want take another action like opening a subform to show other questions. To make our model simple you could add a boolean filter:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::BOOLEAN));

Now we will receive “true”, “false” and “-1” from our form.

Still not fancy enough for you?
Let’s expect you have an online agreement… and your user must enter “yes” and “no” instead of just clicking on a button.
Even this can be done with a single line:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::YES));

Now what happens?
Our user enters “Yes” and the filter will then return “true” for your model. And what’s really fancy about this, is that it works on any language:

$filter = new Zend_Filter_Boolean(array('type' => Zend_Filter_Boolean::YES, 'locale' => 'de'));

Now it would work on “Ja” and “Nein”.

Isn’t that cool? ;-)
Ok… now which types are recognised and can be differed by this filter:

Type         |  true  | false   | examples for unrecognised values
             |  when  |  when   | (or true on casting)
-------------+--------+---------+---------------------------------
BOOLEAN      |   TRUE |   FALSE | 1, 0, 'false'
INTEGER      |      1 |       0 | FALSE, 'true', 0.0, 1.2
FLOAT        |    1.0 |     0.0 | TRUE, 1, 0
STRING       |    --- |      '' | TRUE, 0, 1.2, 'false'
ZERO         |    '1' |     '0' | 0, 1, FALSE, 'true'
EMPTY_ARRAY  |    --- | array() | 0, 1.0, TRUE
NULL         |    --- |    NULL | FALSE, 'true', 0
FALSE_STRING | 'true' | 'false' | TRUE, FALSE, 0, '0'
YES          |  'Yes' |    'No' | TRUE, 'false', 0 (localized!)

Note that the last constant YES works on the language you’ve set. ‘Ja’ for german, ‘Si’ for italic and so on.
I am sure that you will find a useful scenario for this feature in your environment ;-)
The benefit to seperate between different types on casting can sometimes be really useful.

For further informations take a look into the manual.
I hope you find my new component for Zend Framework useful in your own application.

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
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