public static function validate($cif)
    {
        $cifCodes = 'JABCDEFGHI';

        if (9 !== strlen($cif)) {
            return false;
        }
        $cif = strtoupper(trim($cif));
        $sum = (string) Nif::getCifSum($cif);

        $n = (10 - substr($sum, -1)) % 10;

        if (preg_match('/^[ABCDEFGHJKNPQRSUVW]{1}/', $cif)) {
            if (in_array($cif[0], array('A', 'B', 'E', 'H'))) {
                // Numerico
                return $cif[8] == $n;
            } elseif (in_array($cif[0], array('K', 'P', 'Q', 'S'))) {
                // Letras
                return $cif[8] == $cifCodes[$n];
            } else {
                // Alfanumérico
                if (is_numeric($cif[8])) {
                    return $cif[8] == $n;
                } else {
                    return $cif[8] == $cifCodes[$n];
                }
            }
        }

        return false;
    }