Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 51
TCpf
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 11
506
0.00% covered (danger)
0.00%
0 / 51
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 9
 show
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
 getFormated
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 getValue
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 validate
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 24
 setInvalidMessage
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getInvalidMessage
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setAlwaysValidate
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getAlwaysValidate
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 setCallback
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getCallback
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
1<?php
2/*
3 * Formdin Framework
4 * Copyright (C) 2012 Ministério do Planejamento
5 * Criado por Luís Eugênio Barbosa
6 * Essa versão é um Fork https://github.com/bjverde/formDin
7 *
8 * ----------------------------------------------------------------------------
9 * This file is part of Formdin Framework.
10 *
11 * Formdin Framework is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License version 3
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License version 3
21 * along with this program; if not,  see <http://www.gnu.org/licenses/>
22 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
23 * Fifth Floor, Boston, MA  02110-1301, USA.
24 * ----------------------------------------------------------------------------
25 * Este arquivo é parte do Framework Formdin.
26 *
27 * O Framework Formdin é um software livre; você pode redistribuí-lo e/ou
28 * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
29 * do Software Livre (FSF).
30 *
31 * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
32 * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
33 * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
34 * para maiores detalhes.
35 *
36 * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
37 * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
38 * ou escreva para a Fundação do Software Livre (FSF) Inc.,
39 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
40 */
41
42/**
43* Classe para entrada de dados tipo CPF
44*/
45class TCpf extends TMask
46{
47    private $invalidMessage;
48    private $alwaysValidate;
49    private $callback;
50
51    /**
52    * Metódo construtor
53    *
54    * @param string $strName
55    * @param string $strValue
56    * @param boolean $boolRequired
57    * @return TCpf
58    */
59    public function __construct($strName,$strValue=null,$boolRequired=null,$strInvalidMessage=null, $boolAwaysValidate = null,$strJsCallback=null)
60    {
61        parent::__construct($strName,$strValue,'',$boolRequired);
62        $this->setFieldType('cpf');
63        $this->setEvent('onblur','if(!fwValidarCpf(this,event)){return false}');
64        $this->setEvent('onkeyup','fwFormatarCpf(this,event)');
65        $this->setSize(15);
66        $this->setInvalidMessage($strInvalidMessage);
67        $this->setAlwaysValidate($boolAwaysValidate);
68        $this->setCallback($strJsCallback);
69    }
70
71    public function show($print=true)
72    {
73        $this->setAttribute('meta-invalid-message',$this->getInvalidMessage());
74        $this->setAttribute('meta-always-validate',$this->getAlwaysValidate());
75        $this->setAttribute('meta-callback',$this->getCallback());
76        return parent::show($print);
77    }
78
79    /**
80    * Retorna o CPF formatado
81    *
82    */
83    public function getFormated()
84    {
85        if( $this->getValue())
86        {
87            $cpf = @preg_replace("/[^0-9]/","",$this->getValue());
88            return substr($cpf,0,3).".".substr($cpf,3,3).".".substr($cpf,6,3)."-".substr($cpf,9,2);
89        }
90        return $this->getValue();
91    }
92
93    /**
94    * Retorna o CPF sem formatação
95    *
96    */
97    public function getValue()
98    {
99        return @preg_replace("/[^0-9]/","",$this->value);
100    }
101    /**
102    * Validar o cpf
103    *
104    */
105    public function validate()
106    {
107        if( ! parent::validate() )
108        {
109            return false;
110        }
111        $dv         = false;
112        $cpf         = preg_replace("/[^0-9]/","",$this->getValue());
113        if($cpf=='')
114        {
115            return true;
116        }
117        $cpf_dv     = substr($cpf,-2);
118        $controle     = '';
119        // evitar sequencias de número. Ex:11111111111
120        for ( $i = 0; $i < 10; $i++ )
121        {
122            if( $cpf == str_repeat($i,11))
123            {
124                $cpf_dv = '99'; // causar erro de validação
125                break;
126            }
127        }
128
129        for ( $i = 0; $i < 2; $i++ ) {
130            $soma = 0;
131            for ( $j = 0; $j < 9; $j++ )
132            $soma += substr($cpf,$j,1)*(10+$i-$j);
133            if ( $i == 1 ) $soma += $digito * 2;
134            $digito = ($soma * 10) % 11;
135            if ( $digito == 10 ) $digito = 0;
136            $controle .= $digito;
137        }
138        if ( $controle != $cpf_dv )
139        {
140            $this->setCss('border','1px solid #ff0000'); //#176 relacionado com FormDin4.js
141            //$this->setClass('fwFieldRequiredBoarder');
142            $this->setError('Cpf '.$this->getFormated().' está digitado incorretamente!');
143        }
144        return ( (string)$this->getError()==="" );
145    }
146    public function setInvalidMessage($strNewValue=null)
147    {
148        $this->invalidMessage = $strNewValue;
149    }
150
151    public function getInvalidMessage()
152    {
153        return $this->invalidMessage;
154    }
155
156    public function setAlwaysValidate($boolNewValue=null)
157    {
158        $this->alwaysValidate = $boolNewValue;
159    }
160
161    public function getAlwaysValidate()
162    {
163        return ($this->alwaysValidate===true) ? 'true' :'false' ;
164    }
165
166    public function setCallback($strJsFunction=null)
167    {
168        $this->callback = $strJsFunction;
169    }
170
171    public function getCallback()
172    {
173        return $this->callback;
174    }
175
176
177
178}
179?>