Phalcon PHP – i’m lovin it

Since i gave up working with Symfony 1.4, i was searching for a new framework. Because i couldn’t find one that i like, i ended up in developing a small framework by myself. Searching for some solutions related to ORM and ODM, i read some posts related to Phalcon.

Honestly, in a few minutes, i felt in love with this framework. Mainly because of the architecture (very similar with what i developed for myself) and because it is written in C, and it comes as a php extension.

The learning curve should be very fast for any developer that has the basic knowledge about MVC pattern and is familiar with namespaces. Also, the documentation is awesome + that there are a tons of examples on Github.

I am testing it under Ubuntu, Nginx + PHP-FPM – it is incredibly fast. Anyway, there are a lot of improvements that has to be made, but they are moving fast.

All in all, i’m lovin it !

Installing Redis + Php Redis Extension + igbinary in Ubuntu 12.04

This is a quick example of how to install Redis server, Redis PHP extension + Igbinary in Ubuntu 12.04. All steps requires sudo access.

1. Install Redis server

Because the official Ubuntu PPA is not updated with the latest version of Redis, we need to add another repository:

$ sudo add-apt-repository ppa:chris-lea/redis-server
$ sudo apt-get update
$ sudo apt-get install redis-server

If “add-apt-repository” fails, try to run this first:

$ sudo apt-get install python-software-properties

2. Install igbinary

$ sudo pecl install igbinary

3. Install PHP Redis extension

$ cd
$ git clone https://github.com/nicolasff/phpredis.git
$ cd phpredis
$ phpize
$ ./configure --enable-redis-igbinary
$ sudo su
$ make && make install

There is a repository that has the php redis helper: https://github.com/ukko/phpredis-phpdoc

Hope this was heplful 🙂

m.bitpress.ro

Latest developed project: http://m.bitpress.ro/ – mobile version of www.bitpress.ro. Developed from scratch, with my own mini php framework. It uses jQuery Mobile framework, Redis and Local Storage. This is a beta release so let’s hope that we will do more. Next target: Android App.

PHP Credit Card Validator – Class

If you ever need to validate credit card numbers, here is a simple validator class. Of course you can use it as simple as just defining validation rules in one array and use the php biult-in preg_match function to validate. But for the sake of good OOP practice, we need a class 🙂 . Well, i hope that this will help you. So, first we create the validator class:

setCreditCardRules();
    }

    /**
     * Set the rules for credit card validation
     *
     * @author  [email protected]
     * @since   May 2, 2012
     */
    private  function setCreditCardRules()
    {
        $this->rules['creditCard'] = array(
            'amex'     => '/^3[4|7]\\d{13}$/',
            'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
            'diners'   => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
            'disc'     => '/^(?:6011|650\\d)\\d{12}$/',
            'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
            'enroute'  => '/^2(?:014|149)\\d{11}$/',
            'jcb'      => '/^(3\\d{4}|2100|1800)\\d{11}$/',
            'maestro'  => '/^(?:5020|6\\d{3})\\d{12}$/',
            'mc'       => '/^5[1-5]\\d{14}$/',
            'solo'     => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
            'switch'   => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})' .
                          '\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4]' .
                          '[0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
            'visa'     => '/^4\\d{12}(\\d{3})?$/',
            'voyager'  => '/^8699[0-9]{11}$/',
            'fast'     => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3' .
                          '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'
            );
    }

    /**
     * Get available rules for credit card validation. In case you need
     * them to pass to another object(s) and you don't want to use
     * the validator
     *
     * @author  [email protected]
     * @since   May 2, 2012
     * @return  array
     */
    public function getCreditCardRules()
    {
        return $this->rules['creditCard'];
    }

    /**
     * The validator
     *
     * @author  [email protected]
     * @since   May 2, 2012
     * @param   string $cardType Card type as defined in setCreditCardRules()
     * @param   numeric $cardNumber Credit card number
     * @throws  \Exception
     */
    public function isValid($cardType, $cardNumber)
    {
        if(!isset($this->rules['creditCard'][$cardType]))
        {
            throw new \Exception('Invalid card type');
        }

        return preg_match($this->rules['creditCard'][$cardType], $cardNumber);
    }

}
?>

Easy, no ? Using it is very, very simple:

isValid($cardType, $cardNumber))
    {
        echo 'Credit card number is valid';
    }
    else
    {
        echo 'Invalid credit card number';
    }
?>

Hope it is useful for you. Cheers !

Symfony 1.4 validate form fields depending on the other fields value

If you are working with Symfony, you might need from time to time validate a field, depending on the other filed value. There are a few ways to accomplish this. One quick method is to override the bind function:

validatorSchema["secondAddress"]->setOption('required', false);
  }

  return parent::bind($taintedValues, $taintedFiles);
}
?>

So, if user is sending the first address, we don’t need the second address. Of course this is just a dummy example 🙂