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 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 30
TFone
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 30
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 8
 getFormated
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 22
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
42class TFone extends TEdit
43{
44    /**
45     * @param string  $name
46     * @param string  $value
47     * @param boolean $required
48     */
49    public function __construct( $strName,$strValue=null,$boolRequired=null )
50    {
51        parent::__construct($strName, $strValue, 15, $boolRequired);
52        $this->setFieldType('fone');
53        $this->addEvent('onkeyup', 'fwFormatarTelefone(this)');
54        // aplicar formatação ao exibir.
55        $js = new TElement('script');
56        $js->setProperty('type', "text/javascript");
57        $js->add('fwFormatarTelefone(fwGetObj("'.$this->getId().'"));');
58        $this->add($js);
59    }
60    public function getFormated()
61    {
62        if ( $this->getValue() )
63        {
64            $value = preg_replace("/[^0-9]/", "", $this->getValue());
65            // nao pode comecar com zero
66            while ( substr($value, 0, 1) === "0" )
67            {
68                $value = substr($value, 1);
69            }
70            if ( ! $value ) // nenhum valor informado
71            {
72                return null;
73            }
74            if ( strlen($value) < 5 ) // não precisa formatar
75            {
76                return $value;
77            }
78            if ( substr($value, 0, 4) == '0800' )
79            {
80                $value = substr($value, 0, 4)." ". substr($value, 4, 3)." ".substr($value, 7);
81            }
82            else if ( strlen($value) < 8 )
83            {
84                $value = substr($value, 0, 3).'-'.substr($value, 3);
85            }
86            else if ( strlen($value) == 8 )
87            {
88                $value = substr($value, 0, 4).'-'.substr($value, 4);
89            }
90            elseif ( strlen($value) == 9 )
91            {
92                $value = '('.substr($value, 0, 2).') '.substr($value, 2, 3).'-'.substr($value, 5);
93            }
94            elseif ( strlen($value) == 10)
95            {
96                $value = '('.substr($value, 1, 2).') '.substr($value, 3, 3).'-'.substr($value, 6);
97            }
98            elseif ( strlen($value) > 10 )
99            {
100                $value = '('.substr($value, 1, 2).') '.substr($value, 3, 4).'-'.substr($value, 7);
101            }
102            return $value;
103        }
104    }
105}
106?>