Skip to content

Commit e3cf56f

Browse files
authored
Merge pull request nfephp-org#39 from gersonfs/metodo-is-restricted
Adicionando método para retornar se região do GTIN é restrita
2 parents 759a902 + a0bca97 commit e3cf56f

File tree

4 files changed

+147
-22
lines changed

4 files changed

+147
-22
lines changed

src/Gtin.php

+29-11
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,13 @@ protected function isPrefixValid(): bool
248248
*/
249249
protected function getPrefixRegion(string $prefix): string
250250
{
251-
$pf = (int) $prefix;
252-
foreach ($this->stdPrefixCollection as $std) {
253-
$nI = (int) $std->nIni;
254-
$nF = (int) $std->nFim;
255-
$this->validPrefix = true;
256-
$region = $std->region;
257-
if ($pf >= $nI && $pf <= $nF) {
258-
return $region;
259-
}
251+
$prefixDefinition = $this->getPrefixDefinition($prefix);
252+
if ($prefixDefinition === null) {
253+
$this->validPrefix = false;
254+
return "Not Found";
260255
}
261-
$this->validPrefix = false;
262-
return "Not Found";
256+
$this->validPrefix = true;
257+
return $prefixDefinition->getRegion();
263258
}
264259

265260
/**
@@ -281,4 +276,27 @@ protected function getCheckDigit(): int
281276
}
282277
return $dv;
283278
}
279+
280+
protected function getPrefixDefinition(string $prefix): ?PrefixDefinition
281+
{
282+
foreach ($this->stdPrefixCollection as $std) {
283+
$definition = new PrefixDefinition($std);
284+
if ($definition->hasPrefix($prefix)) {
285+
return $definition;
286+
}
287+
}
288+
289+
return null;
290+
}
291+
292+
293+
public function isRestricted(): bool
294+
{
295+
$prefixDefinition = $this->getPrefixDefinition($this->prefix);
296+
if ($prefixDefinition === null) {
297+
throw new \RuntimeException("{$this->prefix} is not a valid prefix!");
298+
}
299+
300+
return $prefixDefinition->isRestricted();
301+
}
284302
}

src/PrefixDefinition.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace NFePHP\Gtin;
4+
5+
class PrefixDefinition
6+
{
7+
private \stdClass $std;
8+
9+
public function __construct(\stdClass $std)
10+
{
11+
$this->std = $std;
12+
}
13+
14+
public function getRegion(): string
15+
{
16+
return (string)$this->std->region;
17+
}
18+
19+
public function hasPrefix(string $prefix): bool
20+
{
21+
$pf = (int) $prefix;
22+
$nI = (int) $this->std->nIni;
23+
$nF = (int) $this->std->nFim;
24+
return $pf >= $nI && $pf <= $nF;
25+
}
26+
27+
public function isRestricted(): bool
28+
{
29+
return $this->std->restricted == "1";
30+
}
31+
32+
}

tests/GtinTest.php

+27-11
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class GtinTest extends TestCase
2020
/**
2121
* Can instantiate class test
2222
*
23-
* @covers Gtin
24-
* @covers ::__contruct
23+
* @covers \NFePHP\Gtin\Gtin
24+
* @covers \NFePHP\Gtin\Gtin::__construct()
2525
*/
2626
public function testCanInstantiate(): void
2727
{
@@ -32,9 +32,9 @@ public function testCanInstantiate(): void
3232
/**
3333
* Can instantiate static class test
3434
*
35-
* @covers Gtin
36-
* @covers ::__contruct
37-
* @covers ::check
35+
* @covers \NFePHP\Gtin\Gtin
36+
* @covers \NFePHP\Gtin\Gtin::__construct()
37+
* @covers \NFePHP\Gtin\Gtin::check()
3838
*/
3939
public function testCanInstantiateStatic(): void
4040
{
@@ -45,7 +45,7 @@ public function testCanInstantiateStatic(): void
4545
/**
4646
* Region test
4747
*
48-
* @covers Gtin::getPrefixRegion
48+
* @covers \NFePHP\Gtin\Gtin::getPrefixRegion()
4949
*/
5050
public function testRegion(): void
5151
{
@@ -59,7 +59,7 @@ public function testRegion(): void
5959
/**
6060
* Check digit test
6161
*
62-
* @covers Gtin::getCheckDigit
62+
* @covers \NFePHP\Gtin\Gtin::getCheckDigit()
6363
*/
6464
public function testCheckDigit(): void
6565
{
@@ -73,8 +73,8 @@ public function testCheckDigit(): void
7373
/**
7474
* Prefix test
7575
*
76-
* @covers Gtin::getPrefix
77-
* @covers Gtin::getType
76+
* @covers \NFePHP\Gtin\Gtin::getPrefix()
77+
* @covers \NFePHP\Gtin\Gtin::getType()
7878
*/
7979
public function testPrefix(): void
8080
{
@@ -85,7 +85,7 @@ public function testPrefix(): void
8585
/**
8686
* Gtin is Valid
8787
*
88-
* @covers Gtin::isValid
88+
* @covers \NFePHP\Gtin\Gtin::isValid()
8989
*/
9090
public function testIsValid(): void
9191
{
@@ -96,7 +96,7 @@ public function testIsValid(): void
9696
/**
9797
* SEM GTIN is Valid
9898
*
99-
* @covers Gtin::isValid
99+
* @covers \NFePHP\Gtin\Gtin::isValid()
100100
*/
101101
public function testSemGetin(): void
102102
{
@@ -232,4 +232,20 @@ public function testFailGtin14BeginsZero(): void
232232

233233
Gtin::check('07890142547852')->isValid();
234234
}
235+
236+
public function test_is_restricted(): void
237+
{
238+
$gtin = new Gtin('2244000425601');
239+
$this->assertTrue($gtin->isRestricted());
240+
241+
$gtin = new Gtin('7909934485750');
242+
$this->assertFalse($gtin->isRestricted());
243+
}
244+
245+
public function test_is_restricted_invalid_prefix(): void
246+
{
247+
$this->expectErrorMessage("510 is not a valid prefix!");
248+
$gtin = new Gtin('5109907267612');
249+
$gtin->isRestricted();
250+
}
235251
}

tests/PrefixDefinitionTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace NFePHP\Gtin\Tests;
4+
5+
use NFePHP\Gtin\PrefixDefinition;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class PrefixDefinitionTest extends TestCase
9+
{
10+
11+
public function test_is_restricted(): void
12+
{
13+
$definition = new PrefixDefinition((object)[
14+
"nIni" => "000",
15+
"nFim" => "019",
16+
"restricted" => "0",
17+
"region" => "GS1 US"
18+
]);
19+
$this->assertFalse($definition->isRestricted());
20+
}
21+
22+
public function test_is_not_restricted(): void
23+
{
24+
$definition = new PrefixDefinition((object)[
25+
"nIni" => "020",
26+
"nFim" => "029",
27+
"restricted" => "1",
28+
"region" => "Números de circulação restrita dentro da região"
29+
]);
30+
$this->assertTrue($definition->isRestricted());
31+
}
32+
33+
public function test_get_region(): void
34+
{
35+
$definition = new PrefixDefinition((object)[
36+
"nIni" => "020",
37+
"nFim" => "029",
38+
"restricted" => "1",
39+
"region" => "Números de circulação restrita dentro da região"
40+
]);
41+
$this->assertEquals("Números de circulação restrita dentro da região", $definition->getRegion());
42+
}
43+
44+
public function testHasPrefix(): void
45+
{
46+
$definition = new PrefixDefinition((object)[
47+
"nIni" => "020",
48+
"nFim" => "029",
49+
"restricted" => "1",
50+
"region" => "Números de circulação restrita dentro da região"
51+
]);
52+
53+
$this->assertFalse($definition->hasPrefix("999"));
54+
$this->assertFalse($definition->hasPrefix("030"));
55+
$this->assertTrue($definition->hasPrefix("020"));
56+
$this->assertTrue($definition->hasPrefix("021"));
57+
$this->assertTrue($definition->hasPrefix("029"));
58+
}
59+
}

0 commit comments

Comments
 (0)