Terc::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
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
 * @author  Marcin Pudełek <[email protected]>
12
 */
13
14
/**
15
 * Created by Marcin.
16
 * Date: 06.09.2017
17
 * Time: 20:51
18
 */
19
20
namespace mrcnpdlk\Teryt\Model;
21
22
use mrcnpdlk\Teryt\Exception;
23
24
/**
25
 * Class Terc
26
 */
27
class Terc
28
{
29
    /**
30
     * Dwuznakowy id wojewodztwa
31
     *
32
     * @var string|null
33
     */
34
    private $provinceId;
35
    /**
36
     * Dwuznakowy id powiatu
37
     *
38
     * @var string|null
39
     */
40
    private $districtId;
41
    /**
42
     * dwuznakowy id gminy
43
     *
44
     * @var string|null
45
     */
46
    private $communeId;
47
    /**
48
     * Jednoznakowy id rodzaju gminy
49
     *
50
     * @var string|null
51
     */
52
    private $communeTypeId;
53
    /**
54
     * Identyfikator gminy wraz z RODZ
55
     *
56
     * @var int|null
57
     */
58
    private $tercId;
59
60
    /**
61
     * Terc constructor.
62
     *
63
     * @param int|null $tercId
64
     *
65
     * @throws \mrcnpdlk\Teryt\Exception
66
     */
67 5
    public function __construct(int $tercId = null)
68
    {
69 5
        $this->setTercId($tercId);
70 5
    }
71
72
    /**
73
     * @return string|null
74
     */
75 2
    public function getCommuneId()
76
    {
77 2
        return $this->communeId;
78
    }
79
80
    /**
81
     * @return string|null
82
     */
83 2
    public function getCommuneTypeId()
84
    {
85 2
        return $this->communeTypeId;
86
    }
87
88
    /**
89
     * @return string|null
90
     */
91 2
    public function getDistrictId()
92
    {
93 2
        return $this->districtId;
94
    }
95
96
    /**
97
     * @return string|null
98
     */
99 2
    public function getProvinceId()
100
    {
101 2
        return $this->provinceId;
102
    }
103
104
    /**
105
     * @return int|null
106
     */
107 4
    public function getTercId()
108
    {
109 4
        return $this->tercId;
110
    }
111
112
    /**
113
     * Ustawienie idk-ów
114
     * tercId jest generowany w locie
115
     *
116
     * @param string $provinceId
117
     * @param string $districtId
118
     * @param string $communeId
119
     * @param string $communeTypeId
120
     *
121
     * @return static
122
     */
123 4
    public function setIds(string $provinceId, string $districtId, string $communeId, string $communeTypeId)
124
    {
125 4
        $this->provinceId    = str_pad($provinceId, 2, '0', \STR_PAD_LEFT);
126 4
        $this->districtId    = str_pad($districtId, 2, '0', \STR_PAD_LEFT);
127 4
        $this->communeId     = str_pad($communeId, 2, '0', \STR_PAD_LEFT);
128 4
        $this->communeTypeId = $communeTypeId;
129 4
        $this->tercId        = (int)sprintf('%s%s%s%s',
130 4
            $this->provinceId,
131 4
            $this->districtId,
132 4
            $this->communeId,
133 4
            $this->communeTypeId);
134
135 4
        return $this;
136
    }
137
138
    /**
139
     * Ustawienie tercId
140
     * Pozostałe pola są generowanie w locie
141
     *
142
     * @param int|null $tercId
143
     *
144
     * @throws Exception
145
     *
146
     * @return static
147
     */
148 5
    public function setTercId(int $tercId = null)
149
    {
150 5
        if ($tercId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tercId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
151 2
            $this->tercId = $tercId;
152 2
            $sTercId      = str_pad((string)$tercId, 7, '0', \STR_PAD_LEFT);
153 2
            if (strlen($sTercId) > 7) {
154
                throw new Exception(sprintf('TercId [%s] malformed', $sTercId));
155
            }
156 2
            $this->provinceId    = substr($sTercId, 0, 2);
157 2
            $this->districtId    = substr($sTercId, 2, 2);
158 2
            $this->communeId     = substr($sTercId, 4, 2);
159 2
            $this->communeTypeId = substr($sTercId, 6, 1);
160
        }
161
162 5
        return $this;
163
    }
164
}
165