Skip to content

Commit

Permalink
clear code
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Krehák committed Sep 29, 2020
1 parent f0fa9b7 commit 49e89d9
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 108 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 |
| --- | --- |
Expand All @@ -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 '<pre>';
print_r($results);
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:/krehak/sk-firmy",
"license": "GPL-3.0-or-later",
"authors": [
Expand All @@ -23,6 +23,7 @@
"php": "^7.2.5",
"ext-curl": "*",
"ext-json": "*",
"ext-soap": "*"
"ext-soap": "*",
"ext-iconv": "*"
}
}
11 changes: 11 additions & 0 deletions examples/hladat_podla_dic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Krehak\SkFirmy\Fields\TaxId;
use Krehak\SkFirmy\SkFirmy;

$skFirmy = new SkFirmy();
$results = $skFirmy->find(new TaxId('2020372640'))->getResults();

echo '<pre>';
print_r($results);
echo '</pre>';
11 changes: 11 additions & 0 deletions examples/hladat_podla_ico.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Krehak\SkFirmy\Fields\BusinessId;
use Krehak\SkFirmy\SkFirmy;

$skFirmy = new SkFirmy();
$results = $skFirmy->find(new BusinessId('31322832'))->getResults();

echo '<pre>';
print_r($results);
echo '</pre>';
9 changes: 9 additions & 0 deletions src/Fields/BusinessId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Krehak\SkFirmy\Fields;

final class BusinessId extends FieldType {
public function getName(): string {
return 'ico';
}
}
7 changes: 7 additions & 0 deletions src/Fields/FieldInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Krehak\SkFirmy\Fields;

interface FieldInterface {
public function getName(): string;
}
15 changes: 15 additions & 0 deletions src/Fields/FieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Krehak\SkFirmy\Fields;

abstract class FieldType implements FieldInterface {
private $value;

public function __construct($value) {
$this->value = $value;
}

public function getValue(): string {
return $this->value;
}
}
9 changes: 9 additions & 0 deletions src/Fields/TaxId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Krehak\SkFirmy\Fields;

final class TaxId extends FieldType {
public function getName(): string {
return 'dic';
}
}
89 changes: 51 additions & 38 deletions src/Libs/FindInOrsr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,52 @@

namespace Krehak\SkFirmy\Libs;

use Krehak\SkFirmy\Fields\BusinessId;
use Krehak\SkFirmy\Fields\FieldType;

class FindInOrsr {
private $fieldToFind = 'ICO';
private $urlDomainMain = 'http://www.orsr.sk/hladaj_ico.asp?{field}={search}&SID=0';
private $urlDomainDetail = 'http://www.orsr.sk/vypis.asp?ID={id}&SID=2&P=0';
private $htmlFieldId = 'IČO';
private $htmlFieldName = 'Oddiel';
private $htmlFieldAddress = 'Sídlo';

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;
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);

Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
70 changes: 46 additions & 24 deletions src/Libs/FindInRegisterUz.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit 49e89d9

Please sign in to comment.