Boolean filtering
- Posted by thomasw at 00:18:40 // //
- Announcements, Filter
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
