Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/RegonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ private function signUp()
* @throws EntityNotFoundException
* @throws RegonServiceCallFailedException
*/
public function findByRegon(string $regon): array
public function findByRegon(string $regon, $lng = "pl"): array
{
$this->validateRegon($regon);
return $this->findById('Regon', $regon);
return $this->findById('Regon', $regon, $lng);
}

/**
Expand All @@ -125,10 +125,21 @@ public function findByRegon(string $regon): array
* @throws EntityNotFoundException
* @throws RegonServiceCallFailedException
*/
public function findByNip(string $nip): array
public function findByNip(string $nip, $lng = "pl"): array
{
$this->validateNip($nip);
return $this->findById('Nip', $nip);
return $this->findById('Nip', $nip, $lng);
}
/**
* @param string $krs
* @return array
* @throws EntityNotFoundException
* @throws RegonServiceCallFailedException
*/
public function findByKrs(string $krs, $lng = "pl"): array
{
$this->validateKrs($krs);
return $this->findById('Krs', $krs, $lng);
}

/**
Expand All @@ -138,7 +149,7 @@ public function findByNip(string $nip): array
* @throws EntityNotFoundException
* @throws RegonServiceCallFailedException
*/
private function findById($id, $value): array
private function findById($id, $value, $lng = "pl")
{
$session = $this->signUp();
try {
Expand All @@ -147,10 +158,11 @@ private function findById($id, $value): array
$data = simplexml_load_string($result->DaneSzukajPodmiotyResult)->dane;

if (property_exists($data, 'ErrorCode')) {
if ($data->ErrorCode == "4") {
throw new EntityNotFoundException($data->ErrorMessagePL);
$error = $lng === "pl" ? $data->ErrorMessagePl : $data->ErrorMessageEn;
if ($data->ErrorCode == '4') {
throw new EntityNotFoundException($error);
}
throw new RegonServiceCallFailedException($data->ErrorMessagePl);
throw new RegonServiceCallFailedException($error);
}
return $this->toArray($data);

Expand All @@ -160,7 +172,7 @@ private function findById($id, $value): array
}

// data jako string w formacie YYYY-MM-DD
public function getCumulativeReport(string $date, string $collectiveReportType) {
public function getCumulativeReport(string $date, string $collectiveReportType, $lng = "pl"){
$this->validateCumulativeReportType($collectiveReportType);
$session = $this->signUp();
try {
Expand All @@ -170,24 +182,26 @@ public function getCumulativeReport(string $date, string $collectiveReportType)
$xmlString = $result->DanePobierzRaportZbiorczyResult;
$dataXml = simplexml_load_string($xmlString);
if (property_exists($dataXml->dane, 'ErrorCode')) {
$error = $lng === "pl" ? $dataXml->dane->ErrorMessagePl : $dataXml->dane->ErrorMessageEn;
if ($dataXml->dane->ErrorCode == "4") {
throw new EntityNotFoundException($dataXml->dane->ErrorMessagePl);
throw new EntityNotFoundException($error);
}
throw new RegonServiceCallFailedException($dataXml->dane->ErrorMessagePl);
throw new RegonServiceCallFailedException($error);
}
$data = json_decode(json_encode($dataXml), true);
return $data["dane"];
} catch (SoapFault $e) {
$this->handleSoapFault($e);
}
}

/**
* @param $regon
* @param $reportType
* @return array
* @throws RegonServiceCallFailedException|EntityNotFoundException
*/
public function getReport($regon, $reportType): array
public function getReport($regon, $reportType, $lng = "pl")
{
$this->validateRegon($regon);
$this->validateReportType($reportType);
Expand All @@ -199,7 +213,8 @@ public function getReport($regon, $reportType): array
$data = simplexml_load_string($result->DanePobierzPelnyRaportResult);

if (property_exists($data->dane, 'ErrorCode')) {
throw new RegonServiceCallFailedException($data->dane->ErrorMessagePl);
$error = $lng === "pl" ? $data->dane->ErrorMessagePl : $data->dane->ErrorMessageEn;
throw new RegonServiceCallFailedException($error);
}

//Kody PKD przychodzą raz jako obiekt, raz jako tablica obiektów, trzeba dostosować argument w toArray
Expand All @@ -226,6 +241,19 @@ private function validateNip($nip)
}
}

/**
* @param $krs
* @return void
*/
private function validateKrs($krs)
{
$pattern = '/^\d{10}$/';

if (!(preg_match($pattern, $krs))) {
throw new InvalidEntityIdentifierException('KRS', $krs);
}
}

/**
* @param $regon
* @return void
Expand Down