diff --git a/README.md b/README.md index 0c45651..6184c56 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ V hlavičke PHP súboru: ```php use Krehak\SkFirmy\SkFirmy; +use Krehak\SkFirmy\Fields\BusinessId; // Ak budete vyhľadávať podľa IČO +use Krehak\SkFirmy\Fields\TaxId; // Ak budete vyhľadávať podľa DIČ ``` Vo vašej časti kódu: @@ -30,23 +32,21 @@ Vo vašej časti kódu: ... $skFirmy = new SkFirmy(); -$results = $skFirmy->find('[FIELD]', '[ID]')->getResults(); +$results = $skFirmy->find(FieldType('xxx'))->getResults(); print_r($results); ... ``` -Premenná `$results` bude obsahovať pole hodnôt. +#### Možnosti FieldType -#### Možnosti - -| Názov | Popis | +| Objekt | Popis | | --- | --- | -| `[FIELD]` | Políčko na vyhľadávanie (momentálne dostupné len 'ico' alebo 'dic') | -| `[ID]` | IČO alebo DIČ (podľa nastavenia [FIELD]) | +| `new BusinessId('xxx')` | Vyhľadať podľa IČO | +| `new TaxId('xxx')` | Vyhľadať podľa DIČ | -#### Návratové hodnoty (pre každý záznam) +##### Premenná `$results` bude obsahovať pole hodnôt: | Názov | Popis | | --- | --- | @@ -56,18 +56,20 @@ Premenná `$results` bude obsahovať pole hodnôt. | `zip` | PSČ | | `business_id` | IČO | | `tax_id` | DIČ | -| `vat_id` | IČ DPH | +| `vat_id` | IČ DPH (len ak je overená registrácia na DPH) | ## Príklad + ```php // index.php require_once './vendor/autoload.php'; use Krehak\SkFirmy\SkFirmy; +use Krehak\SkFirmy\Fields\BusinessId; $skFirmy = new SkFirmy(); -$results = $skFirmy->find('ico', '31322832')->getResults(); +$results = $skFirmy->find(new BusinessId('31322832'))->getResults(); echo '
';
 print_r($results);
diff --git a/composer.json b/composer.json
index 09fd0c1..134fc38 100644
--- a/composer.json
+++ b/composer.json
@@ -4,7 +4,7 @@
   "keywords": [
     "krehak", "sk-firmy", "sk firmy", "sk ico", "sk dic", "sk ic dph", "fakturacne udaje"
   ],
-  "version": "1.0.0",
+  "version": "1.1.0",
   "homepage": "https://github.com/krehak/sk-firmy",
   "license": "GPL-3.0-or-later",
   "authors": [
@@ -23,6 +23,7 @@
     "php": "^7.2.5",
     "ext-curl": "*",
     "ext-json": "*",
-    "ext-soap": "*"
+    "ext-soap": "*",
+    "ext-iconv": "*"
   }
 }
diff --git a/examples/hladat_podla_dic.php b/examples/hladat_podla_dic.php
new file mode 100644
index 0000000..067bfd2
--- /dev/null
+++ b/examples/hladat_podla_dic.php
@@ -0,0 +1,11 @@
+find(new TaxId('2020372640'))->getResults();
+
+echo '
';
+print_r($results);
+echo '
'; diff --git a/examples/hladat_podla_ico.php b/examples/hladat_podla_ico.php new file mode 100644 index 0000000..970e92f --- /dev/null +++ b/examples/hladat_podla_ico.php @@ -0,0 +1,11 @@ +find(new BusinessId('31322832'))->getResults(); + +echo '
';
+print_r($results);
+echo '
'; diff --git a/src/Fields/BusinessId.php b/src/Fields/BusinessId.php new file mode 100644 index 0000000..3538110 --- /dev/null +++ b/src/Fields/BusinessId.php @@ -0,0 +1,9 @@ +value = $value; + } + + public function getValue(): string { + return $this->value; + } +} diff --git a/src/Fields/TaxId.php b/src/Fields/TaxId.php new file mode 100644 index 0000000..1bcb324 --- /dev/null +++ b/src/Fields/TaxId.php @@ -0,0 +1,9 @@ +getAllResults($search); - $results = []; - - foreach($items as $id) { - $detail = $this->getResultDetail($id); - - if(!is_null($detail)) { - $results[$detail['business_id']] = $detail; + private const URL_FIND_BUSINESS_ID = 'http://www.orsr.sk/hladaj_ico.asp?ICO={search}&SID=0'; + private const URL_FIND_NAME = 'http://www.orsr.sk/hladaj_subjekt.asp?OBMENO={search}&PF=0&SID=0&S=on'; + private const URL_DETAIL = 'http://www.orsr.sk/vypis.asp?ID={id}&SID=2&P=0'; + private const HTML_FIELD_ID = 'IČO'; + private const HTML_FIELD_NAME = 'Oddiel'; + private const HTML_FIELD_ADDRESS = 'Sídlo'; + + public function find(FieldType $field): array { + if( + $field instanceof BusinessId + ) { + $items = $this->getAllResults($field); + $results = []; + + foreach($items as $id) { + $detail = $this->getResultDetail($id); + + if(!is_null($detail)) { + $results[$detail['business_id']] = $detail; + } } + + return $results; } - return $results; + return []; } - private function getAllResults(string $search): array { + private function getAllResults(FieldType $field): array { + if($field instanceof BusinessId) $baseUrl = self::URL_FIND_BUSINESS_ID; + else $baseUrl = null; + $data = [ - 'field' => $this->fieldToFind, - 'search' => $search + 'search' => $field->getValue() ]; $request = new Request(); - $url = $request->buildUrl($this->urlDomainMain, $data); - $request->setConnection($url, true); - $response = $request->getResponse(); + $url = Request::buildUrl($baseUrl, $data); + $response = $request + ->setEncoding('CP1250') + ->setConnection($url) + ->getResponse(); preg_match_all('/vypis\.asp\?ID=([0-9a-z]+)/mi', $response,$found); @@ -51,7 +64,7 @@ private function getResultDetail(string $id): ?array { ]; $request = new Request(); - $url = $request->buildUrl($this->urlDomainDetail, $data); + $url = $request->buildUrl(self::URL_DETAIL, $data); $request->setConnection($url, true); $response = $request->getResponse(); @@ -84,28 +97,28 @@ private function parseDetail($detailHtml): ?array { $founds[$field] = $values; } - if(!array_key_exists($this->htmlFieldId, $founds)) return null; + if(!array_key_exists(self::HTML_FIELD_ID, $founds)) return null; - $foundico = preg_replace("/[^0-9]/", "", $founds[$this->htmlFieldId][0]); + $foundico = preg_replace("/[^0-9]/", "", $founds[self::HTML_FIELD_ID][0]); $return = []; - $return['name'] = $founds[$this->htmlFieldName][0]; - unset($founds[$this->htmlFieldAddress][count($founds[$this->htmlFieldAddress]) - 1]); + $return['name'] = $founds[self::HTML_FIELD_NAME][0]; + unset($founds[self::HTML_FIELD_ADDRESS][count($founds[self::HTML_FIELD_ADDRESS]) - 1]); - if(count($founds[$this->htmlFieldAddress]) == 1) { + if(count($founds[self::HTML_FIELD_ADDRESS]) == 1) { $return['street'] = ''; - $return['city'] = $founds[$this->htmlFieldAddress][0]; + $return['city'] = $founds[self::HTML_FIELD_ADDRESS][0]; $return['zip'] = ''; - } elseif(count($founds[$this->htmlFieldAddress]) == 2) { + } elseif(count($founds[self::HTML_FIELD_ADDRESS]) == 2) { $return['street'] = ''; - $return['city'] = $founds[$this->htmlFieldAddress][0]; - $return['zip'] = $founds[$this->htmlFieldAddress][1]; + $return['city'] = $founds[self::HTML_FIELD_ADDRESS][0]; + $return['zip'] = $founds[self::HTML_FIELD_ADDRESS][1]; } else { - $return['zip'] = $founds[$this->htmlFieldAddress][count($founds[$this->htmlFieldAddress]) - 1]; - unset($founds[$this->htmlFieldAddress][count($founds[$this->htmlFieldAddress]) - 1]); - $return['city'] = $founds[$this->htmlFieldAddress][count($founds[$this->htmlFieldAddress]) - 1]; - unset($founds[$this->htmlFieldAddress][count($founds[$this->htmlFieldAddress]) - 1]); - $return['street'] = implode(' ', $founds[$this->htmlFieldAddress]); + $return['zip'] = $founds[self::HTML_FIELD_ADDRESS][count($founds[self::HTML_FIELD_ADDRESS]) - 1]; + unset($founds[self::HTML_FIELD_ADDRESS][count($founds[self::HTML_FIELD_ADDRESS]) - 1]); + $return['city'] = $founds[self::HTML_FIELD_ADDRESS][count($founds[self::HTML_FIELD_ADDRESS]) - 1]; + unset($founds[self::HTML_FIELD_ADDRESS][count($founds[self::HTML_FIELD_ADDRESS]) - 1]); + $return['street'] = implode(' ', $founds[self::HTML_FIELD_ADDRESS]); } $return['business_id'] = $foundico; diff --git a/src/Libs/FindInRegisterUz.php b/src/Libs/FindInRegisterUz.php index e7b9f0f..4f29db6 100644 --- a/src/Libs/FindInRegisterUz.php +++ b/src/Libs/FindInRegisterUz.php @@ -2,41 +2,55 @@ namespace Krehak\SkFirmy\Libs; +use Krehak\SkFirmy\Fields\BusinessId; +use Krehak\SkFirmy\Fields\FieldType; +use Krehak\SkFirmy\Fields\TaxId; + class FindInRegisterUz { - private $fieldToFind = 'ico'; - private $urlDomainMain = 'http://www.registeruz.sk/cruz-public/api/uctovne-jednotky?zmenene-od=2000-01-01&pokracovat-za-id=1&max-zaznamov=1&{field}={search}'; - private $urlDomainDetail = 'http://www.registeruz.sk/cruz-public/api/uctovna-jednotka?id={id}'; + private const URL_MAIN = 'http://www.registeruz.sk/cruz-public/api/uctovne-jednotky?zmenene-od=2000-01-01&pokracovat-za-id=1&max-zaznamov=100&{field}={search}'; + private const URL_DETAIL = 'http://www.registeruz.sk/cruz-public/api/uctovna-jednotka?id={id}'; + private const FIELD_STATE = 'stav'; + private const INVALID_STATES = ['ZMAZANÉ']; + private $validator; public function __construct() { $this->validator = new TaxIdValidator(); } - public function find(string $search): array { - $items = $this->getAllResults($search); - $results = []; - - foreach($items as $id) { - $detail = $this->getResultDetail($id); - - if(!is_null($detail)) { - $results[$detail['business_id']] = $detail; + public function find(FieldType $field): array { + if( + $field instanceof BusinessId || + $field instanceof TaxId + ) { + $items = $this->getAllResults($field); + $results = []; + + foreach($items as $id) { + $detail = $this->getResultDetail($id); + + if(!is_null($detail)) { + $results[$detail['business_id']] = $detail; + } } + + return $results; } - return $results; + return []; } - private function getAllResults(string $search): ?array { + private function getAllResults(FieldType $field): ?array { $data = [ - 'field' => $this->fieldToFind, - 'search' => $search + 'field' => $field->getName(), + 'search' => $field->getValue() ]; $request = new Request(); - $url = $request->buildUrl($this->urlDomainMain, $data); - $request->setConnection($url); - $response = $request->getResponse(); + $url = Request::buildUrl(self::URL_MAIN, $data); + $response = $request + ->setConnection($url) + ->getResponse(); $json = json_decode($response); @@ -47,13 +61,13 @@ private function getAllResults(string $search): ?array { return null; } - private function getResultDetail(string $id): array { + private function getResultDetail(string $id): ?array { $data = [ 'id' => $id ]; $request = new Request(); - $url = $request->buildUrl($this->urlDomainDetail, $data); + $url = $request->buildUrl(self::URL_DETAIL, $data); $request->setConnection($url); $response = $request->getResponse(); @@ -63,10 +77,18 @@ private function getResultDetail(string $id): array { return $this->parseDetail($json); } - return []; + return null; } - private function parseDetail(object $detailObject): array { + private function parseDetail(object $detailObject): ?array { + if(property_exists($detailObject, self::FIELD_STATE)) { + $state = $detailObject->{self::FIELD_STATE}; + + if(in_array($state, self::INVALID_STATES)) { + return null; + } + } + $return = []; $return['name'] = $detailObject->nazovUJ; $return['street'] = $detailObject->ulica; @@ -75,9 +97,9 @@ private function parseDetail(object $detailObject): array { $return['business_id'] = $detailObject->ico; $taxId = (string)$detailObject->dic; + $return['tax_id'] = $taxId; if($this->isTaxIdValid($taxId)) { - $return['tax_id'] = $taxId; $return['vat_id'] = $this->getVatId($taxId); } diff --git a/src/Libs/Request.php b/src/Libs/Request.php index e89012e..6b835ba 100644 --- a/src/Libs/Request.php +++ b/src/Libs/Request.php @@ -3,16 +3,23 @@ namespace Krehak\SkFirmy\Libs; class Request { + private $encoding = null; private $content; public function getResponse(): ?string { return $this->content; } + + public function setEncoding(?string $encoding): Request { + $this->encoding = $encoding; + + return $this; + } - public function setConnection(string $url, bool $isCp1250Encoding = false): void { + public function setConnection(string $url): Request { $curlSession = curl_init(); curl_setopt($curlSession, CURLOPT_URL, $url); - curl_setopt($curlSession, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)"); + curl_setopt($curlSession, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)'); curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST,false); curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER,false); @@ -20,24 +27,26 @@ public function setConnection(string $url, bool $isCp1250Encoding = false): void curl_setopt($curlSession, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curlSession, CURLOPT_CONNECTTIMEOUT, 5); - if($isCp1250Encoding) { - $this->content = iconv('CP1250', 'UTF-8', curl_exec($curlSession)); - } else { + if(is_null($this->encoding)) { $this->content = curl_exec($curlSession); + } else { + $this->content = iconv($this->encoding, 'UTF-8', curl_exec($curlSession)); } curl_close($curlSession); + + return $this; } - public function buildUrl(string $url, array $options): string { + public static function buildUrl(string $url, array $options): string { foreach($options as $key => $value) { - $url = str_replace("{{$key}}", $this->clearValue($value), $url); + $url = str_replace("{{$key}}", self::clearValue($value), $url); } return $url; } - private function clearValue(string $value): string { + private static function clearValue(string $value): string { return str_replace(' ', '', trim($value)); } } diff --git a/src/Libs/TaxIdValidator.php b/src/Libs/TaxIdValidator.php index daefd19..0e505fc 100644 --- a/src/Libs/TaxIdValidator.php +++ b/src/Libs/TaxIdValidator.php @@ -5,19 +5,19 @@ use SoapClient; class TaxIdValidator { - private $countryCode = 'SK'; - private $urlTaxationEU = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'; + private const COUNTRY_CODE = 'SK'; + private const TAXATION_URL = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'; public function validate(string $taxId): bool { try { - $client = new SoapClient($this->urlTaxationEU); + $client = new SoapClient(self::TAXATION_URL); - $obj3 = $client->checkVat(array( - 'countryCode' => $this->countryCode, + $result = $client->checkVat(array( + 'countryCode' => self::COUNTRY_CODE, 'vatNumber' => $taxId )); - if($obj3->valid) { + if($result->valid) { return true; } } catch (\SoapFault $e) { @@ -27,6 +27,6 @@ public function validate(string $taxId): bool { } public function getVatId(string $taxId): string { - return "{$this->countryCode}{$taxId}"; + return self::COUNTRY_CODE . $taxId; } } diff --git a/src/SkFirmy.php b/src/SkFirmy.php index 47ee246..3a8bbb5 100644 --- a/src/SkFirmy.php +++ b/src/SkFirmy.php @@ -2,37 +2,26 @@ namespace Krehak\SkFirmy; +use Krehak\SkFirmy\Fields\FieldType; use Krehak\SkFirmy\Libs\FindInOrsr; use Krehak\SkFirmy\Libs\FindInRegisterUz; use Krehak\SkFirmy\Libs\Results; class SkFirmy { - private const FIELD_BUSINESS_ID = 'ico'; - private const FIELD_TAX_ID = 'dic'; private $results; public function __construct() { $this->results = new Results(); } - public function find(string $field, string $search): SkFirmy { - $field = strtolower($field); - - if($field === self::FIELD_BUSINESS_ID) { - $orsr = new FindInOrsr(); - - $this->results->append( - $orsr->find($search) - ); - } - - if($field === self::FIELD_BUSINESS_ID || $field === self::FIELD_TAX_ID) { - $registerUz = new FindInRegisterUz(); + public function find(FieldType $field): SkFirmy { + $orsr = new FindInOrsr(); + $results = $orsr->find($field); + $this->results->append($results); - $this->results->append( - $registerUz->find($search) - ); - } + $registerUz = new FindInRegisterUz(); + $results = $registerUz->find($field); + $this->results->append($results); return $this; }