Sunday
20
September
2009

Compression and Decompression for ZF

With Zend Framework Release 1.10 there will be a way to handle compression and decompression.

The new components Zend_Filter_Compress and Zend_Filter_Decompress provide a unified API for several compression formats.

Actually the formats BZ2, GZ, LZF, RAR, TAR and ZIP are implemented:

You can not only work with Strings, but also with Files and also with complete Directories.
Each format is provided as standalone Adapter. This way we can easily add support for a new compression format by adding a new Adapter.

So let’s go a little bit into details…
To compress a string with BZ2 we would code the following lines:

$filter = new Zend_Filter_Compress('bz2');
$input  = "Uncompressed content";
$output = $filter->filter($input);

$output will have stored the compressed string.
To reverse the output and get the uncompressed string simply do the following:

$filter = new Zend_Filter_Decompress('bz2');
$result = $filter->filter($output);

As you can see compression and decompression is really very simple.

Now when you want to compress and decompress files, it is as simple as compressing strings.
Simply give the filename instead of a string and tell the adapter where to store the archive.

$filter = new Zend_Filter_Compress(array(
    'adapter' => 'bz2'
    'options' => array(
        'archive' => 'myarchive.bz2'
    )
);
$result = $filter->filter('\\path\to\myfile.ext');

The file myfile.ext will be compressed and stored into myarchive.bz2.
You can even compress complete directories. But be aware, that when you do this on the base directory of your server, your server will be stressed for a very long time.

As you can attach this filters to any component which accepts filters, you could very easily compress any given input to store it anywhere else.

Let’s expect you have a textarea element where your costumer provides a book review. To save space you could easily attach this filter to the element and store the compressed string into the database. When you want to display the content to your user again simply decompress it before rendering.

$element = new Zend_Form_Element_TextArea('myarea');
$element->addFilter('Compress', array('zip'));

There are many places where compression or decompression can be useful. In all this cases you can now use Zend_Filter_Compress.

When you find this component useful feel free to use it within your own application.
Have fun with Zend Framework

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Saturday
19
September
2009

Zend_Validate_PostCode

Did you ever want to know if a given input is a valid postalcode ?
If you did, then there is a solution for you within Zend Framework 1.10.

I added a new component Zend_Validate_PostCode.
Zend_Validate_PostCode is a validator which can automatically detect the postal code of the users country.

It takes the full qualified locale to set the right format:

$valid = new Zend_Validate_PostCode('de_AT');
$valid->isValid('1234'); // returns true

So how does this work. Zend_Validate_PostCode extracts the country from the full qualified locale.
It uses therefor eighter the set application wide locale, the users locale or a given locale.
Zend_Validate_PostCode knows the notation for all existing postal codes from about 150 different countries. It has therefor all postal codes stored as regex.

Note that small locales only hold the language and do not contain any regional informations.

Maybe you would want to accept only partitial postal codes.
In US for example postal codes are detected with this regex:
d{5}([ -]d{4})?

Now you may want to limit the official codes to 5000-5999. You can simply define your own regex:

$valid = new Zend_Validate_PostCode('5d{3}');
$valid->isValid('1234'); // returns false

Another note:
Of course Zend_Validate_PostCode is not able to detect if a given postcode exists or not.
1234 may exist but 1233 not. Zend_Validate_PostCode detects only if the notation is correct. As postcodes change multiple times a year it would be an very expensive task to collect them and keep them up to date.

I hope you find this validator usefull and have use for it.

More to come soon

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
Friday
18
September
2009

Zend_Validate_Callback

I just added a new component to Zend Framework which will be available to public as with release 1.10.

Zend_Validate_Callback:
Several components within Zend Framework use Validators to check data or input.
This validator can be used to add a self defined method as validator.

How does this work:

$valid = new Zend_Validate_Callback('is_int');
	
$valid->isValid(123); // returns true
$valid->isValid('test'); // returns false

Of course this is only a very small example…
You can use self written functions or class methods as validators.

PHP5.3 makes live even easier… you can also use Closures or Invokes. See this example for closures:

$valid = new Zend_Validate_Callback(
    function ($value) {
        if (is_int($value)) {
            return true;
        }
        return false;
    }
);

This can be very handy for small self written functions.
Using this validator you should be aware of some rules:

* A validator should always return true or false
* It should not throw an exception
* When possible use native php methods

I hope you find this validator usefull and have use for it.

More to come soon

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top