Skip to content

Commit 8770d92

Browse files
committed
Merge pull request #12 from jkuchar/feature-11-make-ares-record-immutable
Make data objects immutable
2 parents d085b4e + 54705ce commit 8770d92

File tree

4 files changed

+67
-97
lines changed

4 files changed

+67
-97
lines changed

src/Ares.php

+25-31
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,17 @@ public function findByIdentificationNumber($id)
105105
throw new AresException('IČ firmy nebylo nalezeno.');
106106
}
107107

108-
$record = new AresRecord();
108+
$record = new AresRecord(
109+
$id,
110+
strval($elements->DIC), // taxId
111+
strval($elements->OF), // companyName
112+
strval($elements->AA->NU), // street
113+
strval($elements->AA->CD), // house number
114+
strval($elements->AA->CO) ?: null, // house orientation number
115+
strval($elements->AA->NCO) ? strval($elements->AA->N.' - '.strval($elements->AA->NCO)) : strval($elements->AA->N), // town
116+
strval($elements->AA->PSC) // ZIP
117+
);
109118

110-
$record->setCompanyId($id);
111-
$record->setTaxId(strval($elements->DIC));
112-
$record->setCompanyName(strval($elements->OF));
113-
$record->setStreet(strval($elements->AA->NU));
114-
115-
if (strval($elements->AA->CO)) {
116-
$record->setStreetHouseNumber(strval($elements->AA->CD));
117-
$record->setStreetOrientationNumber(strval($elements->AA->CO));
118-
} else {
119-
$record->setStreetHouseNumber(strval($elements->AA->CD));
120-
}
121-
122-
if (strval($elements->AA->NCO)) {
123-
$record->setTown(strval($elements->AA->N.' - '.strval($elements->AA->NCO)));
124-
} else {
125-
$record->setTown(strval($elements->AA->N));
126-
}
127-
128-
$record->setZip(strval($elements->AA->PSC));
129119
} else {
130120
throw new AresException('Databáze ARES není dostupná.');
131121
}
@@ -175,15 +165,16 @@ public function findInResById($id)
175165
$elements = $data->children($ns['D'])->Vypis_RES;
176166

177167
if (strval($elements->ZAU->ICO) === $id) {
178-
$record = new AresRecord();
179-
$record->setCompanyId(strval($id));
180-
$record->setTaxId($this->findVatById($id));
181-
$record->setCompanyName(strval($elements->ZAU->OF));
182-
$record->setStreet(strval($elements->SI->NU));
183-
$record->setStreetHouseNumber(strval($elements->SI->CD));
184-
$record->setStreetOrientationNumber(strval($elements->SI->CO));
185-
$record->setTown(strval($elements->SI->N));
186-
$record->setZip(strval($elements->SI->PSC));
168+
$record = new AresRecord(
169+
strval($id),
170+
$this->findVatById($id),
171+
strval($elements->ZAU->OF),
172+
strval($elements->SI->NU),
173+
strval($elements->SI->CD),
174+
strval($elements->SI->CO),
175+
strval($elements->SI->N),
176+
strval($elements->SI->PSC)
177+
);
187178
} else {
188179
throw new AresException('IČ firmy nebylo nalezeno.');
189180
}
@@ -231,13 +222,15 @@ public function findVatById($id)
231222
$vatResponse = simplexml_load_string($vatRequest);
232223

233224
if ($vatResponse) {
234-
$record = new TaxRecord();
225+
235226
$ns = $vatResponse->getDocNamespaces();
236227
$data = $vatResponse->children($ns['are']);
237228
$elements = $data->children($ns['dtt'])->V->S;
238229

239230
if (strval($elements->ico) === $id) {
240-
$record->setTaxId(str_replace('dic=', 'CZ', strval($elements->p_dph)));
231+
$record = new TaxRecord(
232+
str_replace('dic=', 'CZ', strval($elements->p_dph))
233+
);
241234
} else {
242235
throw new AresException('DIČ firmy nebylo nalezeno.');
243236
}
@@ -300,6 +293,7 @@ public function findByName($name, $city = null)
300293

301294
$records = new AresRecords();
302295
foreach ($elements as $element) {
296+
// TODO: What is this?
303297
$record = new AresRecord();
304298
$record->setCompanyId(strval($element->ico));
305299
$record->setTaxId(

src/Ares/AresRecord.php

+21-62
Original file line numberDiff line numberDiff line change
@@ -15,89 +15,64 @@ class AresRecord
1515
/**
1616
* @var int
1717
*/
18-
public $companyId;
18+
private $companyId;
1919

2020
/**
2121
* @var string
2222
*/
23-
public $taxId;
23+
private $taxId;
2424

2525
/**
2626
* @var string
2727
*/
28-
public $companyName;
28+
private $companyName;
2929

3030
/**
3131
* @var string
3232
*/
33-
public $street;
33+
private $street;
3434

3535
/**
3636
* @var string
3737
*/
38-
public $streetHouseNumber;
38+
private $streetHouseNumber;
3939

4040
/**
4141
* @var string
4242
*/
43-
public $streetOrientationNumber;
43+
private $streetOrientationNumber;
4444

4545
/**
4646
* @var string
4747
*/
48-
public $town;
48+
private $town;
4949

5050
/**
5151
* @var string
5252
*/
53-
public $zip;
53+
private $zip;
5454

5555
/**
56-
* @param $companyId
56+
* AresRecord constructor.
57+
* @param int $companyId
58+
* @param string $taxId
59+
* @param string $companyName
60+
* @param string $street
61+
* @param string $streetHouseNumber
62+
* @param string $streetOrientationNumber
63+
* @param string $town
64+
* @param string $zip
5765
*/
58-
public function setCompanyId($companyId)
66+
public function __construct($companyId, $taxId, $companyName, $street, $streetHouseNumber, $streetOrientationNumber, $town, $zip)
5967
{
6068
$this->companyId = $companyId;
61-
}
62-
63-
/**
64-
* @param $companyName
65-
*/
66-
public function setCompanyName($companyName)
67-
{
68-
$this->companyName = $companyName;
69-
}
70-
71-
/**
72-
* @param $taxId
73-
*/
74-
public function setTaxId($taxId)
75-
{
7669
$this->taxId = !empty($taxId) ? $taxId : null;
77-
}
78-
79-
/**
80-
* @param $street
81-
*/
82-
public function setStreet($street)
83-
{
70+
$this->companyName = $companyName;
8471
$this->street = $street;
85-
}
86-
87-
/**
88-
* @param $streetHouseNumber
89-
*/
90-
public function setStreetHouseNumber($streetHouseNumber)
91-
{
9272
$this->streetHouseNumber = !empty($streetHouseNumber) ? $streetHouseNumber : null;
93-
}
94-
95-
/**
96-
* @param $streetOrientationNumber
97-
*/
98-
public function setStreetOrientationNumber($streetOrientationNumber)
99-
{
10073
$this->streetOrientationNumber = !empty($streetOrientationNumber) ? $streetOrientationNumber : null;
74+
$this->town = $town;
75+
$this->zip = $zip;
10176
}
10277

10378
/**
@@ -113,22 +88,6 @@ public function getStreetWithNumbers()
11388
$this->streetHouseNumber);
11489
}
11590

116-
/**
117-
* @param $town
118-
*/
119-
public function setTown($town)
120-
{
121-
$this->town = $town;
122-
}
123-
124-
/**
125-
* @param $zip
126-
*/
127-
public function setZip($zip)
128-
{
129-
$this->zip = $zip;
130-
}
131-
13291
/**
13392
* @return mixed
13493
*/

src/Ares/AresRecords.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
*
1010
* @author Dennis Fridrich <[email protected]>
1111
*/
12-
final class AresRecords implements \ArrayAccess, \IteratorAggregate
12+
final class AresRecords implements \ArrayAccess, \IteratorAggregate, \Countable
1313
{
1414
/**
1515
* @var AresRecord[]
1616
*/
17-
public $array = [];
17+
private $array = [];
1818

1919
/**
2020
* @param mixed $offset
@@ -72,4 +72,12 @@ public function getIterator()
7272
{
7373
return new ArrayIterator($this->array);
7474
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function count()
80+
{
81+
return count($this->array);
82+
}
7583
}

src/Ares/TaxRecord.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,25 @@ class TaxRecord
1212
/**
1313
* @var string
1414
*/
15-
public $taxId = null;
15+
private $taxId = null;
1616

1717
/**
18+
* TaxRecord constructor.
1819
* @param string $taxId
1920
*/
20-
public function setTaxId($taxId)
21+
public function __construct($taxId)
2122
{
2223
$this->taxId = !empty($taxId) ? $taxId : null;
2324
}
2425

26+
/**
27+
* @return string
28+
*/
29+
public function getTaxId()
30+
{
31+
return $this->taxId;
32+
}
33+
2534
/**
2635
* @return string
2736
*/

0 commit comments

Comments
 (0)