Skip to content

Commit 1c2ba38

Browse files
committed
[INC] TimeZoneByUF and UFList classes
1 parent 5fb17ab commit 1c2ba38

8 files changed

+216
-21
lines changed

LICENSE.md

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
GNU LESSER GENERAL PUBLIC LICENSE
2-
Version 3, 29 June 2007
1+
#GNU LESSER GENERAL PUBLIC LICENSE
2+
Version 3, 29 June 2007
33

44
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
55
Everyone is permitted to copy and distribute verbatim copies
@@ -166,8 +166,8 @@ Library.
166166

167167
======================= OR ==============================
168168

169-
GNU GENERAL PUBLIC LICENSE
170-
Version 3, 29 June 2007
169+
#GNU GENERAL PUBLIC LICENSE
170+
Version 3, 29 June 2007
171171

172172
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
173173
Everyone is permitted to copy and distribute verbatim copies
@@ -840,3 +840,27 @@ may consider it more useful to permit linking proprietary applications with
840840
the library. If this is what you want to do, use the GNU Lesser General
841841
Public License instead of this License. But first, please read
842842
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
843+
844+
======================= OR ==============================
845+
846+
#The MIT License
847+
848+
Further resources on the MIT License Copyright 2009 nfephp.org
849+
850+
Permission is hereby granted, free of charge, to any person obtaining a copy of
851+
this software and associated documentation files (the "Software"), to deal in
852+
the Software without restriction, including without limitation the rights to
853+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
854+
of the Software, and to permit persons to whom the Software is furnished to do
855+
so, subject to the following conditions:
856+
857+
The above copyright notice and this permission notice shall be included in all
858+
copies or substantial portions of the Software.
859+
860+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
861+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
862+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
863+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
864+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
865+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
866+
DEALINGS IN THE SOFTWARE.

composer.json

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nfephp-org/sped-common",
33
"type": "library",
4-
"description": "sped-common é parte do NFePHP.",
4+
"description": "sped-common é parte do projeto NFePHP.",
55
"keywords": [
66
"nfe",
77
"sped",
@@ -10,7 +10,8 @@
1010
"homepage": "https://github.com/nfephp-org/sped-common",
1111
"license": [
1212
"GPL-3.0+",
13-
"LGPL-3.0+"
13+
"LGPL-3.0+",
14+
"MIT"
1415
],
1516
"authors": [
1617
{
@@ -25,22 +26,12 @@
2526
}
2627
],
2728
"require": {
28-
"php": "~5.5|~7.0",
29+
"php": "~5.6|~7.0",
2930
"ext-dom": "*",
30-
"ext-xml": "*",
31-
"ext-libxml": "*",
32-
"ext-xmlreader": "*",
3331
"ext-curl": "*",
3432
"ext-soap": "*",
3533
"ext-openssl": "*",
36-
"ext-zip": "*",
37-
"ext-zlib": "*",
38-
"ext-fileinfo": "*",
39-
"ext-iconv": "*",
40-
"ext-mbstring": "*",
41-
"psr/log": "^1.0",
42-
"zendframework/zend-servicemanager": "^2.5",
43-
"zendframework/zend-mail": "^2.5"
34+
"psr/log": "^1.0"
4435
},
4536
"require-dev": {
4637
"phpunit/phpunit": "4.*",

src/Keys.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function build($cUF, $ano, $mes, $cnpj, $mod, $serie, $numero, $tp
4646
$tpEmis,
4747
$codigo
4848
);
49-
return $key . self::verifyingDigit($chave);
49+
return $key . self::verifyingDigit($key);
5050
}
5151

5252
/**

src/TimeZoneByUF.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace NFePHP\Common;
4+
5+
use NFePHP\Common\UFList;
6+
7+
class TimeZoneByUF
8+
{
9+
protected static $tzd = [
10+
'AC'=>'America/Rio_Branco',
11+
'AL'=>'America/Maceio',
12+
'AM'=>'America/Manaus',
13+
'AP'=>'America/Belem',
14+
'BA'=>'America/Bahia',
15+
'CE'=>'America/Fortaleza',
16+
'DF'=>'America/Sao_Paulo',
17+
'ES'=>'America/Sao_Paulo',
18+
'GO'=>'America/Sao_Paulo',
19+
'MA'=>'America/Fortaleza',
20+
'MG'=>'America/Sao_Paulo',
21+
'MS'=>'America/Campo_Grande',
22+
'MT'=>'America/Cuiaba',
23+
'PA'=>'America/Belem',
24+
'PB'=>'America/Fortaleza',
25+
'PE'=>'America/Recife',
26+
'PI'=>'America/Fortaleza',
27+
'PR'=>'America/Sao_Paulo',
28+
'RJ'=>'America/Sao_Paulo',
29+
'RN'=>'America/Fortaleza',
30+
'RO'=>'America/Porto_Velho',
31+
'RR'=>'America/Boa_Vista',
32+
'RS'=>'America/Sao_Paulo',
33+
'SC'=>'America/Sao_Paulo',
34+
'SE'=>'America/Maceio',
35+
'SP'=>'America/Sao_Paulo',
36+
'TO'=>'America/Araguaina'
37+
];
38+
39+
/**
40+
* Return timezone string
41+
* @param string $uf
42+
* @return string
43+
*/
44+
public static function get($uf)
45+
{
46+
$uf = strtoupper($uf);
47+
if (is_numeric($uf)) {
48+
$uf = UFList::getUFByCode($uf);
49+
}
50+
//only for validation, if $uf dont exists throws exception
51+
$code = UFList::getCodeByUF($uf);
52+
return self::$tzd[$uf];
53+
}
54+
}

src/UFList.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace NFePHP\Common;
4+
5+
use \InvalidArgumentException;
6+
7+
class UFList
8+
{
9+
protected static $uflist = [
10+
12=>'AC',
11+
27=>'AL',
12+
13=>'AM',
13+
16=>'AP',
14+
29=>'BA',
15+
23=>'CE',
16+
53=>'DF',
17+
32=>'ES',
18+
52=>'GO',
19+
21=>'MA',
20+
31=>'MG',
21+
50=>'MS',
22+
51=>'MT',
23+
15=>'PA',
24+
25=>'PB',
25+
26=>'PE',
26+
22=>'PI',
27+
41=>'PR',
28+
33=>'RJ',
29+
24=>'RN',
30+
11=>'RO',
31+
14=>'RR',
32+
43=>'RS',
33+
42=>'SC',
34+
28=>'SE',
35+
35=>'SP',
36+
17=>'TO'
37+
];
38+
39+
/**
40+
* Returns abbreviation of state from your IBGE code
41+
* @param int $code
42+
* @return string
43+
* @throws InvalidArgumentException
44+
*/
45+
public static function getUFByCode($code)
46+
{
47+
if (!key_exists($code, self::$uflist)) {
48+
throw new \InvalidArgumentException("cUF incorreto!");
49+
}
50+
return self::$uflist[$code];
51+
}
52+
53+
/**
54+
* Returns IBGE code from abbreviation of state
55+
* @param string $uf
56+
* @return int
57+
* @throws InvalidArgumentException
58+
*/
59+
public static function getCodeByUF($uf)
60+
{
61+
$uf = strtoupper($uf);
62+
$codelist = array_flip(self::$uflist);
63+
if (!key_exists($uf, $codelist)) {
64+
throw new \InvalidArgumentException("UF incorreto!");
65+
}
66+
return $codelist[$uf];
67+
}
68+
}

src/Validator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace NFePHP\Common;
44

55
/**
6-
* Validation class of XML structures with respect to its established and defined structure
7-
* in an XSD file
6+
* Validation class of XML structures with respect to its established
7+
* and defined structure in an XSD file
88
* @category NFePHP
99
* @package NFePHP\Common\Validator
1010
* @copyright Copyright (c) 2008-2016

tests/TimeZoneByUFTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace NFePHP\Common\Tests;
4+
5+
use NFePHP\Common\TimeZoneByUF;
6+
7+
class TimeZoneByUFTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testget()
10+
{
11+
$tzd = TimeZoneByUF::get('SP');
12+
$this->assertEquals('America/Sao_Paulo', $tzd);
13+
}
14+
15+
public function testgetByCode()
16+
{
17+
$tzd = TimeZoneByUF::get(35);
18+
$this->assertEquals('America/Sao_Paulo', $tzd);
19+
}
20+
}

tests/UFListTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace NFePHP\Common\Tests;
4+
5+
use NFePHP\Common\UFList;
6+
7+
class UFListTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testgetUFByCode()
10+
{
11+
$uf = UFList::getUFByCode(35);
12+
$this->assertEquals('SP', $uf);
13+
}
14+
15+
/**
16+
* @expectedException InvalidArgumentException
17+
*/
18+
public function testgetUFByCodeFail()
19+
{
20+
$uf = UFList::getUFByCode(77);
21+
}
22+
23+
public function testgetUFByUF()
24+
{
25+
$code = UFList::getCodeByUF('Sp');
26+
$this->assertEquals(35, $code);
27+
}
28+
29+
/**
30+
* @expectedException InvalidArgumentException
31+
*/
32+
public function testgetUFByUFFail()
33+
{
34+
$code = UFList::getCodeByUF('aa');
35+
}
36+
37+
38+
}

0 commit comments

Comments
 (0)