1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TERYT-API |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 pudelek.org.pl |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view source file |
10
|
|
|
* that is bundled with this package in the file LICENSE |
11
|
|
|
* |
12
|
|
|
* @author Marcin Pudełek <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Created by Marcin Pudełek <[email protected]> |
18
|
|
|
* Date: 07.09.2017 |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace mrcnpdlk\Teryt\Model; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
use mrcnpdlk\Teryt\Exception\InvalidArgument; |
25
|
|
|
use mrcnpdlk\Teryt\Exception\NotFound; |
26
|
|
|
use mrcnpdlk\Teryt\NativeApi; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Commune |
30
|
|
|
* |
31
|
|
|
* @package mrcnpdlk\Teryt\Model |
32
|
|
|
*/ |
33
|
|
|
class Commune extends EntityAbstract |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* 6 (lub 7) znakowy symbol powiatu lub tercId |
37
|
|
|
* sklada sie z 2 cyfr województwa , 2 powiatu, 2 gminy i 1 rodzaj gminy (opcjonalny) |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $id; |
42
|
|
|
/** |
43
|
|
|
* Pełen identyfikator gminy |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $tercId; |
48
|
|
|
/** |
49
|
|
|
* Nazwa powiatu |
50
|
|
|
* |
51
|
|
|
* @var static |
52
|
|
|
*/ |
53
|
|
|
public $name; |
54
|
|
|
/** |
55
|
|
|
* ID typu gminy |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
public $typeId; |
60
|
|
|
/** |
61
|
|
|
* Nazwa typu gminy |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $typeName; |
66
|
|
|
/** |
67
|
|
|
* Obiekt z danymi o powiecie w którym znajduje się gmina |
68
|
|
|
* |
69
|
|
|
* @var \mrcnpdlk\Teryt\Model\District |
70
|
|
|
*/ |
71
|
|
|
public $district; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Commune constructor. |
75
|
|
|
* |
76
|
|
|
* @param string $id 6 lub 7-znakowy symbol gminy |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
* @throws InvalidArgument |
80
|
|
|
* @throws NotFound |
81
|
|
|
*/ |
82
|
1 |
|
public function find(string $id) |
83
|
|
|
{ |
84
|
1 |
|
switch (strlen($id)) { |
85
|
1 |
View Code Duplication |
case 6; |
|
|
|
|
86
|
|
|
$provinceId = substr($id, 0, 2); |
87
|
|
|
$districtId = substr($id, 2, 2); |
88
|
|
|
$communeId = substr($id, 2, 2); |
89
|
|
|
$tercId = null; |
90
|
|
|
break; |
91
|
1 |
View Code Duplication |
case 7: |
|
|
|
|
92
|
1 |
|
$provinceId = substr($id, 0, 2); |
93
|
1 |
|
$districtId = substr($id, 2, 2); |
94
|
1 |
|
$communeId = substr($id, 2, 2); |
95
|
1 |
|
$tercId = $id; |
96
|
1 |
|
break; |
97
|
|
|
default: |
98
|
|
|
throw new InvalidArgument(sprintf('CommuneSymbol malformed, it has %s chars', strlen($id))); |
99
|
|
|
break; |
100
|
|
|
} |
101
|
1 |
|
if (!$tercId) { |
|
|
|
|
102
|
|
|
foreach (NativeApi::getInstance()->PobierzListeGmin($provinceId, $districtId) as $i) { |
103
|
|
|
if ($i->districtId === $districtId) { |
104
|
|
|
$this->id = $id; |
105
|
|
|
$this->name = $i->name; |
|
|
|
|
106
|
|
|
$this->typeName = $i->typeName; |
107
|
|
|
$this->typeId = $i->communeTypeId; |
108
|
|
|
$tercId = sprintf('%s%s%s%s', $provinceId, $districtId, $communeId, $i->communeTypeId); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} else { |
113
|
1 |
|
$res = NativeApi::getInstance()->WyszukajJednostkeWRejestrze(null, NativeApi::CATEGORY_GMI_ALL, [], [$tercId]); |
114
|
1 |
|
if (!empty($res) && count($res) === 1) { |
115
|
1 |
|
$oCommune = $res[0]; |
116
|
1 |
|
$this->id = $id; |
117
|
1 |
|
$this->name = $oCommune->communeName; |
118
|
1 |
|
$this->typeName = $oCommune->communeTypeName; |
119
|
1 |
|
$this->typeId = $oCommune->communeTypeId; |
120
|
|
|
} |
121
|
|
|
} |
122
|
1 |
|
if (!$this->id) { |
123
|
|
|
throw new NotFound(sprintf('Commune [id:%s] not exists', $id)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
1 |
|
$this->id = sprintf('%s%s%s', $provinceId, $districtId, $communeId); |
128
|
1 |
|
$this->tercId = $tercId; |
129
|
1 |
|
$this->district = (new District())->find(sprintf('%s%s', $provinceId, $districtId)); |
130
|
|
|
|
131
|
1 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.