September 07, 2011

Cross browser Leading Zeros with JavaScript

This is a short entry for further reference

Left padding an integer with zeros was never so easy. Can't believe this ended up working.

function LeadWithZeros(num, pad) {
    return (Array(pad).join('0') + num).slice(-pad);
}

LeadWithZeros(5, 3) === '005';
LeadWithZeros(65, 3) === '065';
LeadWithZeros(120, 3) === '120';

March 15, 2011

!isset, is_null, empty: The Good, the Bad and the Ugly

Hi everyone, you programmers trying to grasp the intricacies of PHP. As coders, our raw material is, and will be, information and knowledge. But lack of knowledge does not give us some information, too? Is of this absence that we will discuss today: How to know if we don't know something in PHP. And our main tools are isset, is_null and empty. Want to know more about them? Which one is The Good, The Bad and The Ugly?

March 04, 2011

Exchanging currencies with Zend_Currency_CurrencyInterface

As many companies offering online services do, we handle multiple currencies. This often involves querying external services to know the real value of the currency exchange. Previously, this logic was scattered all over the code, even copy-pasted in multiple files, and is pretty hard to mantain, let alone to test.

So, in my work detecting and extracting patterns from old spaghetti code, I came with the following solution: A Service layer.

January 19, 2011

How to detect if a class implements an Interface?

I'm in the real need to know if, given a string className or object, it implements a particular interface. I'm specially interested in the string className part.

During the search, many seemingly valid options appeared: instanceof, is_subclass_of, is_a, ReflectionClass::ImplementsInterface, class_implements… Which one to use? Let's do some testing!

December 17, 2010

The Coder Zone Stats 2010

It's a fact: 2010 comes to an end in 2 weeks, and it's time for me to take stock of the year, get some numbers, think about them… As I did last year.

December 16, 2010

What my htaccess looks like in Zend Framework

While coding under Zend Framework there's a standard for what htaccess files should look like. Using mod_rewrite every request is checked against the filesystem for existance, and then served. If it does not exists, the control passes to index.php who starts the Zend Application.

Since, even nowadays, every disk access is paid in valuable time, I made my own htaccess, optimized for disk avoidance whenever possible. After all, it's only a convention to follow, isn't it?

October 26, 2010

Simplest Credit Card Validator

Searching for a simple credit card validation algorithm? The Luhn algorithm is a simple checksum that validates most identification numbers, mainly used in credit card ones. It's implementation is quite simple. Need one? Just come along!