├── .gitignore
├── .codeclimate.yml
├── tests
├── ExampleTest.php
├── bootstrap.php
├── GetValueTest.php
├── AbstractTest.php
├── ClientTest.php
├── LoginTest.php
├── SearchTest.php
└── ReportTest.php
├── src
├── Exception
│ ├── SearchException.php
│ ├── InvalidKeyException.php
│ └── RegonException.php
├── Enum
│ └── GetValue.php
├── Transport.php
├── Client.php
└── Service.php
├── .travis.yml
├── phpunit.xml
├── composer.json
├── wsdl
├── UslugaBIRzewnPubl_xsd3.xsd
├── UslugaBIRzewnPubl_xsd0.xsd
├── UslugaBIRzewnPubl_xsd1.xsd
├── UslugaBIRzewnPubl_xsd2.xsd
├── UslugaBIRzewnPubl.xsd
└── UslugaBIRzewnPubl_wsdl0.xsd
├── LICENSE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea/
2 | /vendor/
3 | composer.lock
4 |
--------------------------------------------------------------------------------
/.codeclimate.yml:
--------------------------------------------------------------------------------
1 | engines:
2 | fixme:
3 | enabled: true
4 | phpmd:
5 | enabled: true
6 | phpcodesniffer:
7 | enabled: true
8 | ratings:
9 | paths:
10 | - "**.php"
11 | exclude_paths:
12 | - src/Transport.php
13 |
--------------------------------------------------------------------------------
/tests/ExampleTest.php:
--------------------------------------------------------------------------------
1 | ZalogujResult = '1234';
11 | $client = $this->createClient($result);
12 | $this->assertSame('1234', $client->login());
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Exception/SearchException.php:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 | tests
12 |
13 |
14 |
15 |
16 | src
17 |
18 | src/Transport.php
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "freshmindpl/wyszukiwarkaregon",
3 | "description": "Polish REGON Internet Database",
4 | "keywords": ["GUS","REGON","NIP","CEIDG","api","BIR1"],
5 | "homepage": "https://wyszukiwarkaregon.stat.gov.pl/appBIR/index.aspx",
6 | "type": "library",
7 | "license": "MIT",
8 | "authors": [
9 | {
10 | "name": "Paweł Krzaczkowski",
11 | "email": "pawel@freshmind.pl"
12 | }
13 | ],
14 | "minimum-stability": "stable",
15 | "require": {
16 | "php": ">=5.4.0",
17 | "ext-soap": "*"
18 | },
19 | "require-dev": {
20 | "phpunit/phpunit": "~4.8",
21 | "codeclimate/php-test-reporter": "dev-master"
22 | },
23 | "autoload": {
24 | "psr-4": {
25 | "WyszukiwarkaRegon\\": "src/",
26 | "WyszukiwarkaRegon\\Tests\\": "src/tests/"
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/wsdl/UslugaBIRzewnPubl_xsd3.xsd:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Freshmind Sp. z o. o.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/tests/GetValueTest.php:
--------------------------------------------------------------------------------
1 | GetValueResult = 1;
14 | $client = $this->createClient($result);
15 | $this->assertSame(1, $client->getValue(GetValue::ERROR_CODE));
16 | }
17 |
18 | public function testGetValueError()
19 | {
20 | $result = new \stdClass();
21 | $result->GetValueResult = 1;
22 | $client = $this->createClient($result);
23 |
24 | try {
25 | $client->getValue('wrong value');
26 | } catch (RegonException $e) {
27 | $this->assertContains('Unknown getValue key', $e->getMessage());
28 | }
29 | }
30 |
31 | /**
32 | * @expectedException \WyszukiwarkaRegon\Exception\RegonException
33 | */
34 | public function testLoginException()
35 | {
36 | $sopaFault = new \SoapFault("test", "myMessage");
37 | $client = $this->createFault($sopaFault);
38 | $client->getValue(GetValue::ERROR_CODE);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/tests/AbstractTest.php:
--------------------------------------------------------------------------------
1 | getMockFromWsdl('wsdl/UslugaBIRzewnPubl.xsd', 'UslugaBIRzewnPubl');
16 |
17 | return $client;
18 | }
19 |
20 | /**
21 | * @param \stdClass $response
22 | * @return Client
23 | */
24 | protected function createClient(\stdClass $response)
25 | {
26 | $client = $this->getClient();
27 |
28 | $client->expects($this->any())
29 | ->method('__soapCall')
30 | ->will($this->returnValue($response));
31 |
32 | return new Client([
33 | 'client' => $client
34 | ]);
35 | }
36 |
37 | /**
38 | * @param \SoapFault $fault
39 | * @return Client
40 | */
41 | protected function createFault(\SoapFault $fault)
42 | {
43 | $client = $this->getMockFromWsdl('wsdl/UslugaBIRzewnPubl.xsd', 'UslugaBIRzewnPubl');
44 |
45 | $client->expects($this->any())
46 | ->method('__soapCall')
47 | ->will($this->throwException($fault));
48 |
49 | return new Client([
50 | 'client' => $client
51 | ]);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/tests/ClientTest.php:
--------------------------------------------------------------------------------
1 | createClient(new \stdClass());
10 | $this->assertSame(get_class($client), 'WyszukiwarkaRegon\\Client');
11 | }
12 |
13 | public function testClientSetSession()
14 | {
15 | $client = $this->createClient(new \stdClass());
16 | $client = $client->setSession('1234567890');
17 | $this->assertSame(get_class($client), 'WyszukiwarkaRegon\\Client');
18 | }
19 |
20 | public function testClientSetKey()
21 | {
22 | $client = $this->createClient(new \stdClass());
23 | $client = $client->setKey('1234567890');
24 | $this->assertSame(get_class($client), 'WyszukiwarkaRegon\\Client');
25 | }
26 |
27 | public function testClientSetSandbox()
28 | {
29 | $client = $this->createClient(new \stdClass());
30 | $client = $client->sandbox();
31 | $this->assertSame(get_class($client), 'WyszukiwarkaRegon\\Client');
32 | }
33 | /**
34 | * @expectedException \PHPUnit_Framework_Error
35 | */
36 | public function testClientCallBadMethod()
37 | {
38 | $client = $this->createClient(new \stdClass());
39 | /** @noinspection PhpUndefinedMethodInspection */
40 | $client->nonexistent();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/tests/LoginTest.php:
--------------------------------------------------------------------------------
1 | ZalogujResult = 'aaaabbbbccccdddd';
11 | $client = $this->createClient($result);
12 | $this->assertSame('aaaabbbbccccdddd', $client->login());
13 | }
14 |
15 | /**
16 | * @expectedException \WyszukiwarkaRegon\Exception\RegonException
17 | */
18 | public function testLoginException()
19 | {
20 | $sopaFault = new \SoapFault("test", "myMessage");
21 | $client = $this->createFault($sopaFault);
22 | $client->login();
23 | }
24 |
25 | public function testLogoutSuccess()
26 | {
27 | $result = new \stdClass();
28 | $result->WylogujResult = true;
29 | $client = $this->createClient($result);
30 | $this->assertTrue($client->logout());
31 | }
32 |
33 | /**
34 | * @expectedException \WyszukiwarkaRegon\Exception\RegonException
35 | */
36 | public function testLogoutException()
37 | {
38 | $sopaFault = new \SoapFault("test", "myMessage");
39 | $client = $this->createFault($sopaFault);
40 | $client->logout();
41 | }
42 |
43 | /**
44 | * @expectedException \WyszukiwarkaRegon\Exception\InvalidKeyException
45 | */
46 | public function testLoginInvalidKeyException()
47 | {
48 | $result = new \stdClass();
49 | $result->ZalogujResult = '';
50 | $client = $this->createClient($result);
51 | $client->login();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Transport.php:
--------------------------------------------------------------------------------
1 | location, 0);
17 | $header[] = new \SoapHeader(
18 | 'http://www.w3.org/2005/08/addressing',
19 | 'Action',
20 | $this->getAction($function_name),
21 | 0
22 | );
23 |
24 | $this->__setSoapHeaders(null);
25 | $this->__setSoapHeaders($header);
26 |
27 | return parent::__soapCall(
28 | $function_name,
29 | $arguments,
30 | $options,
31 | $input_headers,
32 | $output_headers
33 | );
34 | }
35 |
36 | /**
37 | * {@inheritdoc}
38 | */
39 | public function __doRequest($request, $location, $action, $version, $one_way = 0)
40 | {
41 | $location = $this->location;
42 | $response = parent::__doRequest($request, $location, $action, $version);
43 | if (!$response) {
44 | return $response;
45 | }
46 | $matches = array();
47 | preg_match('//s', $response, $matches);
48 |
49 | return $matches[0];
50 | }
51 |
52 | /**
53 | * @param string $function_name
54 | * @return string
55 | */
56 | private function getAction($function_name)
57 | {
58 | switch ($function_name) {
59 |
60 | case 'GetValue':
61 | case 'PobierzCaptcha':
62 | case 'SprawdzCaptcha':
63 | $prefix = 'http://CIS/BIR/2014/07/IUslugaBIR';
64 | break;
65 |
66 | default:
67 | $prefix = 'http://CIS/BIR/PUBL/2014/07/IUslugaBIRzewnPubl';
68 | }
69 |
70 | return $prefix . '/' . $function_name;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/wsdl/UslugaBIRzewnPubl_xsd0.xsd:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/src/Client.php:
--------------------------------------------------------------------------------
1 | service = new Service();
29 |
30 | foreach ($config as $key => $value) {
31 | if (method_exists($this, 'set' . ucfirst($key))) {
32 | $this->{'set' . ucfirst($key)}($value);
33 | }
34 | }
35 | }
36 |
37 | /**
38 | * @param string $name
39 | * @param array $arguments
40 | * @return mixed
41 | */
42 | public function __call($name, array $arguments)
43 | {
44 | if (!is_callable([$this->service, $name])) {
45 | trigger_error(
46 | "Call to undefined method " . __CLASS__ . "::$name()",
47 | E_USER_ERROR
48 | );
49 | }
50 |
51 | return call_user_func_array([$this->service, $name], $arguments);
52 | }
53 |
54 | /**
55 | * Set session sid
56 | *
57 | * @param string $sid
58 | * @return $this
59 | */
60 | public function setSession($sid)
61 | {
62 | $this->service->setSession($sid);
63 |
64 | return $this;
65 | }
66 |
67 | /**
68 | * Enable sanbox service
69 | *
70 | * @return $this
71 | */
72 | public function sandbox()
73 | {
74 | $this->service->setSandbox();
75 |
76 | return $this;
77 | }
78 |
79 | /**
80 | * Set Api key
81 | *
82 | * @param string $key
83 | * @return $this
84 | */
85 | public function setKey($key)
86 | {
87 | $this->service->setKey($key);
88 |
89 | return $this;
90 | }
91 |
92 | /**
93 | * @param \SoapClient $client
94 | * @return $this
95 | */
96 | public function setClient(\SoapClient $client)
97 | {
98 | $this->service->setTransport($client);
99 |
100 | return $this;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/wsdl/UslugaBIRzewnPubl_xsd1.xsd:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/wsdl/UslugaBIRzewnPubl_xsd2.xsd:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/tests/SearchTest.php:
--------------------------------------------------------------------------------
1 | 1234567890
16 | )
17 | );
18 |
19 | $result = new \stdClass();
20 | $result->DaneSzukajResult = json_encode($data);
21 | $client = $this->createClient($result);
22 | $result = $client->search([]);
23 |
24 | $this->assertArrayHasKey(0, $result);
25 | $this->assertArrayHasKey('Regon', $result[0]);
26 | }
27 |
28 | public function testSearchXmlSuccess()
29 | {
30 | $data = <<
32 |
33 | 12312312312321
34 |
35 |
36 | EOD;
37 |
38 | $result = new \stdClass();
39 | $result->DaneSzukajResult = $data;
40 | $client = $this->createClient($result);
41 | $result = $client->search([]);
42 |
43 | $this->assertArrayHasKey(0, $result);
44 | $this->assertArrayHasKey('Regon', $result[0]);
45 | $this->assertSame('12312312312321', $result[0]['Regon']);
46 | }
47 |
48 | public function testSearchXmlFailure()
49 | {
50 | $data = <<
52 | 12312312312321
53 |
54 |
55 | EOD;
56 |
57 | $result = new \stdClass();
58 | $result->DaneSzukajResult = $data;
59 | $client = $this->createClient($result);
60 | $response = $client->search([]);
61 | $this->assertEmpty($response);
62 | }
63 |
64 | /**
65 | * @expectedException \WyszukiwarkaRegon\Exception\RegonException
66 | */
67 | public function testSearchFault()
68 | {
69 | $sopaFault = new \SoapFault("test", "myMessage");
70 | $client = $this->createFault($sopaFault);
71 | $client->search([]);
72 | }
73 |
74 | public function testSearchException()
75 | {
76 | $mock = $this->getClient();
77 | $mock->expects($this->any())
78 | ->method('__soapCall')
79 | ->willReturnCallback([$this, 'mockSearchErrorOk']);
80 |
81 | $client = new Client([
82 | 'client' => $mock
83 | ]);
84 |
85 | try {
86 | $client->search([]);
87 | } catch (SearchException $e) {
88 | $this->assertSame($e->getCode(), 1);
89 | $this->assertSame($e->getMessage(), 'error message');
90 | }
91 | }
92 |
93 | public function testSearchExceptionEmptyGetValue()
94 | {
95 | $mock = $this->getClient();
96 | $mock->expects($this->any())
97 | ->method('__soapCall')
98 | ->willReturnCallback([$this, 'mockSearchErrorEmpty']);
99 |
100 | $client = new Client([
101 | 'client' => $mock
102 | ]);
103 |
104 | try {
105 | $client->search([]);
106 | } catch (SearchException $e) {
107 | $this->assertSame($e->getCode(), 7);
108 | $this->assertSame($e->getMessage(), '');
109 | }
110 | }
111 |
112 | /**
113 | * @param string $method
114 | * @param array $params
115 | * @return \stdClass
116 | */
117 | public function mockSearchErrorOk($method, array $params)
118 | {
119 | $return = new \stdClass();
120 |
121 | switch ($method) {
122 | case 'DaneSzukaj':
123 | $return->DaneSzukajResult = null;
124 | break;
125 |
126 | case 'GetValue':
127 | switch ($params[0]['pNazwaParametru']) {
128 | case GetValue::ERROR_CODE:
129 | $return->GetValueResult = 1;
130 | break;
131 | case GetValue::ERROR_MESSAGE:
132 | $return->GetValueResult = 'error message';
133 | break;
134 | }
135 | break;
136 | }
137 |
138 | return $return;
139 | }
140 |
141 | /**
142 | * @param string $method
143 | * @param array $params
144 | * @return \stdClass
145 | */
146 | public function mockSearchErrorEmpty($method, array $params)
147 | {
148 | $return = new \stdClass();
149 |
150 | switch ($method) {
151 | case 'DaneSzukaj':
152 | $return->DaneSzukajResult = null;
153 | break;
154 |
155 | case 'GetValue':
156 | switch ($params[0]['pNazwaParametru']) {
157 | case GetValue::ERROR_CODE:
158 | $return->GetValueResult = null;
159 | break;
160 | case GetValue::ERROR_MESSAGE:
161 | $return->GetValueResult = null;
162 | break;
163 | }
164 | break;
165 | }
166 |
167 | return $return;
168 | }
169 | }
170 |
--------------------------------------------------------------------------------
/tests/ReportTest.php:
--------------------------------------------------------------------------------
1 | 1234567890
16 | )
17 | );
18 |
19 | $result = new \stdClass();
20 | $result->DanePobierzPelnyRaportResult = json_encode($data);
21 | $client = $this->createClient($result);
22 | $result = $client->report('123456', 'ReportName');
23 |
24 | $this->assertArrayHasKey(0, $result);
25 | $this->assertArrayHasKey('Regon', $result[0]);
26 | }
27 |
28 | public function testReportXmlSuccess()
29 | {
30 | $data = <<
32 |
33 | 12312312312321
34 |
35 |
36 | EOD;
37 |
38 | $result = new \stdClass();
39 | $result->DanePobierzPelnyRaportResult = $data;
40 | $client = $this->createClient($result);
41 | $result = $client->report('123456', 'ReportName');
42 |
43 | $this->assertArrayHasKey(0, $result);
44 | $this->assertArrayHasKey('Regon', $result[0]);
45 | $this->assertSame('12312312312321', $result[0]['Regon']);
46 | }
47 |
48 | public function testReportXmlFailure()
49 | {
50 | $data = <<
52 | 12312312312321
53 |
54 |
55 | EOD;
56 |
57 | $result = new \stdClass();
58 | $result->DaneSzukajResult = $data;
59 | $client = $this->createClient($result);
60 | $response = $client->search([]);
61 | $this->assertEmpty($response);
62 | }
63 |
64 | /**
65 | * @expectedException \WyszukiwarkaRegon\Exception\RegonException
66 | */
67 | public function testReportFault()
68 | {
69 | $sopaFault = new \SoapFault("test", "myMessage");
70 | $client = $this->createFault($sopaFault);
71 | $client->report('123456', 'ReportName');
72 | }
73 |
74 | public function testReportException()
75 | {
76 | $mock = $this->getClient();
77 | $mock->expects($this->any())
78 | ->method('__soapCall')
79 | ->willReturnCallback([$this, 'mockSearchErrorOk']);
80 |
81 | $client = new Client([
82 | 'client' => $mock
83 | ]);
84 |
85 | try {
86 | $client->report('123456', 'ReportName');
87 | } catch (SearchException $e) {
88 | $this->assertSame($e->getCode(), 1);
89 | $this->assertSame($e->getMessage(), 'error message');
90 | }
91 | }
92 |
93 | public function testReportExceptionEmptyGetValue()
94 | {
95 | $mock = $this->getClient();
96 | $mock->expects($this->any())
97 | ->method('__soapCall')
98 | ->willReturnCallback([$this, 'mockSearchErrorEmpty']);
99 |
100 | $client = new Client([
101 | 'client' => $mock
102 | ]);
103 |
104 | try {
105 | $client->report('123456', 'ReportName');
106 | } catch (SearchException $e) {
107 | $this->assertSame($e->getCode(), 7);
108 | $this->assertSame($e->getMessage(), '');
109 | }
110 | }
111 |
112 | /**
113 | * @param string $method
114 | * @param array $params
115 | * @return \stdClass
116 | */
117 | public function mockSearchErrorOk($method, array $params)
118 | {
119 | $return = new \stdClass();
120 |
121 | switch ($method) {
122 | case 'DaneSzukaj':
123 | $return->DaneSzukajResult = null;
124 | break;
125 |
126 | case 'GetValue':
127 | switch ($params[0]['pNazwaParametru']) {
128 | case GetValue::ERROR_CODE:
129 | $return->GetValueResult = 1;
130 | break;
131 | case GetValue::ERROR_MESSAGE:
132 | $return->GetValueResult = 'error message';
133 | break;
134 | }
135 | break;
136 | }
137 |
138 | return $return;
139 | }
140 |
141 | /**
142 | * @param string $method
143 | * @param array $params
144 | * @return \stdClass
145 | */
146 | public function mockSearchErrorEmpty($method, array $params)
147 | {
148 | $return = new \stdClass();
149 |
150 | switch ($method) {
151 | case 'DaneSzukaj':
152 | $return->DaneSzukajResult = null;
153 | break;
154 |
155 | case 'GetValue':
156 | switch ($params[0]['pNazwaParametru']) {
157 | case GetValue::ERROR_CODE:
158 | $return->GetValueResult = null;
159 | break;
160 | case GetValue::ERROR_MESSAGE:
161 | $return->GetValueResult = null;
162 | break;
163 | }
164 | break;
165 | }
166 |
167 | return $return;
168 | }
169 | }
170 |
--------------------------------------------------------------------------------
/wsdl/UslugaBIRzewnPubl.xsd:
--------------------------------------------------------------------------------
1 |
2 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 | https://Wyszukiwarkaregontest.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc
145 |
146 |
147 |
148 |
149 |
150 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Polish REGON Internet Database BIR1
2 |
3 | PHP bindings for the BIR1 (Baza Internetowa REGON 1) API (https://wyszukiwarkaregon.stat.gov.pl/appBIR/index.aspx).
4 |
5 | [API Documentation](https://goo.gl/zxBf2o)
6 |
7 | [](https://packagist.org/packages/freshmindpl/wyszukiwarkaregon)
8 | [](https://travis-ci.org/freshmindpl/wyszukiwarkaregon)
9 | [](https://codeclimate.com/github/freshmindpl/wyszukiwarkaregon)
10 | [](https://codeclimate.com/github/freshmindpl/wyszukiwarkaregon/coverage)
11 |
12 | ## Installation
13 |
14 | The API client can be installed via [Composer](https://github.com/composer/composer).
15 |
16 | In your composer.json file:
17 |
18 | ```js
19 | {
20 | "require": {
21 | "freshmindpl/wyszukiwarkaregon": "~3.0"
22 | }
23 | }
24 | ```
25 |
26 | Once the composer.json file is created you can run `composer install` for the initial package install and `composer update` to update to the latest version of the API client.
27 |
28 | ## Basic Usage
29 |
30 | Remember to include the Composer autoloader in your application:
31 |
32 | ```php
33 |
38 | ```
39 |
40 | Configure your access credentials when creating a client:
41 |
42 | ```php
43 | 'aaaabbbbccccdddd' //Optional api key - required for full reports,
50 | 'session' => 'abcdefghijklmnopqrstuvwxyz' //Session id if already logged in
51 | ]);
52 | ?>
53 | ```
54 |
55 | ### Local Testing
56 |
57 | Run `phpunit` from the project root to start all tests.
58 |
59 | ### Examples
60 |
61 | #### Login
62 |
63 | ```php
64 | login();
68 | } catch (RegonException $e) {
69 | echo "There was an error.\n";
70 | }
71 |
72 | if(empty($session_id)) {
73 | // Empty session means that api key is invalid
74 |
75 | throw new \Exception('Invalid api key');
76 | }
77 |
78 | ```
79 |
80 | #### Logout
81 |
82 | ```php
83 | login();
87 | } catch (RegonException $e) {
88 | echo "There was an error.\n";
89 | }
90 |
91 | ....
92 |
93 | // Invalidate current session
94 | $client->logout();
95 |
96 | ```
97 |
98 | #### GetValue
99 |
100 | See [API Documentation](https://goo.gl/zxBf2o) for list of available params (section 2.5)
101 | ```php
102 | getValue('StatusSesji');
106 | } catch (RegonException $e) {
107 | echo "There was an error.\n";
108 | }
109 |
110 | ?>
111 | ```
112 |
113 | #### Search
114 |
115 | ```php
116 | 142396858, // 9 or 14 digits
120 | 'Krs' => null, // 10 digits
121 | 'Nip' => null, // 10 digits
122 | 'Regony9zn' => null, // Multiple 9 digits Regon's seperated by any non digit char (max 100)
123 | 'Regony14zn' => null, // Multiple 14 digits Regon's seperated by any non digit char (max 100)
124 | 'Krsy' => null, // Multiple 10 digits Krs seperated by any non digit char (max 100)
125 | 'Nipy' => null, // Multiple 10 digits Nip seperated by any non digit char (max 100)
126 | ];
127 |
128 | try {
129 | $data = $client->search($params);
130 |
131 | } catch (SearchException $e) {
132 | switch($e->getCode()) {
133 | case GetValue::SEARCH_ERROR_CAPTCHA: //Captcha resolve needed
134 | // Some code
135 | break;
136 | case GetValue::SEARCH_ERROR_INVALIDARGUMENT: //Wrong search params
137 | // Some code
138 | break;
139 | case GetValue::SEARCH_ERROR_NOTFOUND: //Empty result - no data found matching search params
140 | // Some code
141 | break;
142 | case GetValue::SEARCH_ERROR_SESSION: //Wrong session id or expired session
143 | // Some code
144 | break;
145 | }
146 | } catch (RegonException $e) {
147 | echo "There was an error.\n";
148 | }
149 | ```
150 |
151 | #### Reports
152 |
153 | See [API Documentation](https://goo.gl/zxBf2o) for list of available reports (section 2.6)
154 | ```php
155 | report($regon, 'PublDaneRaportFizycznaOsoba');
161 |
162 | } catch (SearchException $e) {
163 | switch($e->getCode()) {
164 | case GetValue::SEARCH_ERROR_CAPTCHA: //Captcha resolve needed
165 | // Some code
166 | break;
167 | case GetValue::SEARCH_ERROR_INVALIDARGUMENT: //Wrong search params
168 | // Some code
169 | break;
170 | case GetValue::SEARCH_ERROR_NOTFOUND: //Empty result - no data found matching search params
171 | // Some code
172 | break;
173 | case GetValue::SEARCH_ERROR_NOTAUTHORIZED: //Not authorized for this raport
174 | // Some code
175 | break;
176 | case GetValue::SEARCH_ERROR_SESSION: //Wrong session id or expired session
177 | // Some code
178 | break;
179 | }
180 | } catch (RegonException $e) {
181 | echo "There was an error.\n";
182 | }
183 | ```
184 |
185 | ### Full example
186 |
187 | This is a working example on how to use GUS client to query for data
188 |
189 | ```php
190 | YOUR_API_KEY
194 | ]);
195 |
196 | //Enable sandbox mode for development environment
197 | if (defined('DEVELOPMENT') && DEVELOPMENT) {
198 | $client->sandbox();
199 | }
200 |
201 | //Check if we have saved session id
202 | $session_id = $memcache->get('gus_session_id');
203 |
204 | if (!$session_id) {
205 | try {
206 | $session_id = $client->login();
207 | } catch (RegonException $e) {
208 | echo "There was an error.\n";
209 | exit;
210 | }
211 |
212 | //Save session_id for later use
213 | $memcache->save('gus_session_id', $session_id);
214 | } else {
215 |
216 | //Set current session
217 | $client->setSession($session_id);
218 | }
219 |
220 | //Try to get data
221 | try {
222 |
223 | //Get basic data
224 | $data = $client->search(['Nip' => '1234567890']);
225 |
226 | //Get full comapny report
227 | switch($data[0]['Typ']) {
228 | case 'P':
229 | case 'LP':
230 | $full = $client->report($data[0]['Regon'], 'PublDaneRaportPrawna');
231 | break;
232 |
233 | case 'F':
234 | case 'LF':
235 | $full = $client->report($data[0]['Regon'], 'PublDaneRaportDzialalnoscFizycznejCeidg');
236 | break;
237 | }
238 | } catch (SearchException $e) {
239 | switch($e->getCode()) {
240 | case GetValue::SEARCH_ERROR_CAPTCHA: //Captcha resolve needed
241 | // You need to get catpcha and show it to the user
242 | break;
243 | case GetValue::SEARCH_ERROR_INVALIDARGUMENT: //Wrong search params
244 | // Invalid argument passed to search/report method
245 | break;
246 | case GetValue::SEARCH_ERROR_NOTFOUND: //Empty result - no data found matching search params
247 | // No records where found
248 | $data = null;
249 | $full = null;
250 | break;
251 | case GetValue::SEARCH_ERROR_NOTAUTHORIZED: //Not authorized for this raport
252 | // You are not authorized to generate this report
253 | break;
254 | case GetValue::SEARCH_ERROR_SESSION: //Wrong session id or expired session
255 | // Your session has expired - You need to login again
256 | break;
257 | }
258 | } catch (RegonException $e) {
259 | echo "There was an error.\n";
260 | exit;
261 | }
262 |
263 | ```
264 |
265 | ## License
266 |
267 | MIT license. See the [LICENSE](LICENSE) file for more details.
268 |
--------------------------------------------------------------------------------
/src/Service.php:
--------------------------------------------------------------------------------
1 | streamContext = stream_context_create();
65 |
66 | $this->transport = new Transport(
67 | dirname(__DIR__) . $this->wsdl,
68 | array(
69 | 'soap_version' => SOAP_1_2,
70 | 'exception' => true,
71 | 'stream_context' => $this->streamContext,
72 | 'location' => $this->url
73 | )
74 | );
75 | }
76 |
77 | /**
78 | * @param \SoapClient $client
79 | */
80 | public function setTransport(\SoapClient $client)
81 | {
82 | $this->transport = $client;
83 | }
84 |
85 | /**
86 | * Set access key
87 | *
88 | * @param $key
89 | */
90 | public function setKey($key)
91 | {
92 | $this->key = $key;
93 | }
94 |
95 | /**
96 | * @param string $sid
97 | */
98 | public function setSession($sid)
99 | {
100 | $this->sid = $sid;
101 |
102 | // Set the HTTP headers.
103 | stream_context_set_option($this->streamContext, array('http' => array('header' => 'sid: ' . $this->sid)));
104 | }
105 |
106 | /**
107 | * Enable sandbox mode
108 | *
109 | */
110 | public function setSandbox()
111 | {
112 | $this->transport->__setLocation($this->urlSandbox);
113 | }
114 |
115 | /**
116 | * @return string
117 | */
118 | public function login()
119 | {
120 | $params = [
121 | 'pKluczUzytkownika' => $this->key
122 | ];
123 |
124 | try {
125 | $response = $this->transport->__soapCall('Zaloguj', [$params]);
126 | } catch (\SoapFault $e) {
127 | throw new RegonException($e->getMessage(), 0, $e);
128 | }
129 |
130 | if (empty($response->ZalogujResult)) {
131 | //Invalid key
132 | throw new InvalidKeyException('Invalid api key', 99);
133 | }
134 |
135 | $this->setSession($response->ZalogujResult);
136 |
137 | return $response->ZalogujResult;
138 | }
139 |
140 | /**
141 | * @return boolean
142 | */
143 | public function logout()
144 | {
145 | $params = [
146 | 'pIdentyfikatorSesji' => $this->sid
147 | ];
148 |
149 | try {
150 | $response = $this->transport->__soapCall('Wyloguj', [$params]);
151 | } catch (\SoapFault $e) {
152 | throw new RegonException($e->getMessage(), 0, $e);
153 | }
154 |
155 | $this->setSession(null);
156 |
157 | return (bool)$response->WylogujResult;
158 | }
159 |
160 | /**
161 | * @param $key
162 | * @throws \Exception
163 | */
164 | public function getValue($key)
165 | {
166 | if (!in_array($key, $this->getValueDictionary)) {
167 | throw new RegonException(
168 | 'Unknown getValue key. Supported values are ' . implode(', ', $this->getValueDictionary)
169 | );
170 | }
171 |
172 | $params = [
173 | 'pNazwaParametru' => $key
174 | ];
175 |
176 | try {
177 | $response = $this->transport->__soapCall('GetValue', [$params]);
178 | } catch (\SoapFault $e) {
179 | throw new RegonException($e->getMessage(), 0, $e);
180 | }
181 |
182 | return $response->GetValueResult;
183 | }
184 |
185 | /**
186 | * @param array $condition
187 | * @return mixed
188 | */
189 | public function search(array $condition)
190 | {
191 | $params = [
192 | 'pParametryWyszukiwania' => $condition
193 | ];
194 |
195 | try {
196 | $response = $this->transport->__soapCall('DaneSzukaj', [$params]);
197 | } catch (\SoapFault $e) {
198 | throw new RegonException($e->getMessage(), 0, $e);
199 | }
200 |
201 | if (empty($response->DaneSzukajResult)) {
202 | $message = $this->getValue(GetValue::ERROR_MESSAGE);
203 | $code = $this->getValue(GetValue::ERROR_CODE);
204 |
205 | if ($message || $code) {
206 | throw new SearchException($message, $code);
207 | }
208 |
209 | throw new SearchException('', Enum\GetValue::SEARCH_ERROR_SESSION);
210 | }
211 |
212 | return $this->normalizeResponse($response->DaneSzukajResult);
213 | }
214 |
215 | /**
216 | * @param string $regon
217 | * @param string $name
218 | * @return array|mixed
219 | */
220 | public function report($regon, $name)
221 | {
222 | $params = [
223 | 'pRegon' => $regon,
224 | 'pNazwaRaportu' => $name
225 | ];
226 |
227 | try {
228 | $response = $this->transport->__soapCall('DanePobierzPelnyRaport', [$params]);
229 | } catch (\SoapFault $e) {
230 | throw new RegonException($e->getMessage(), 0, $e);
231 | }
232 |
233 | if (empty($response->DanePobierzPelnyRaportResult)) {
234 | $message = $this->getValue(GetValue::ERROR_MESSAGE);
235 | $code = $this->getValue(GetValue::ERROR_CODE);
236 |
237 | if ($message || $code) {
238 | throw new SearchException($message, $code);
239 | }
240 |
241 | throw new SearchException('', Enum\GetValue::SEARCH_ERROR_SESSION);
242 | }
243 |
244 | return $this->normalizeResponse($response->DanePobierzPelnyRaportResult);
245 | }
246 |
247 | /**
248 | * Normalize response
249 | *
250 | * @param $response
251 | * @return array|mixed
252 | */
253 | private function normalizeResponse($response)
254 | {
255 | $json = json_decode($response, true);
256 |
257 | if ($json !== null) {
258 | return $json;
259 | }
260 |
261 | //Load as XMl
262 | $xml = @simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
263 |
264 | if (!$xml) {
265 | return [];
266 | }
267 |
268 | $array = $this->xml2array($xml);
269 |
270 | return array_values($array);
271 | }
272 |
273 | /**
274 | * Xml to array
275 | *
276 | * @param $xml
277 | * @return array
278 | */
279 | private function xml2array($xml)
280 | {
281 | $arr = array();
282 | foreach ($xml as $element) {
283 | /** @var \SimpleXmlElement $element */
284 | $tag = $element->getName();
285 | $e = get_object_vars($element);
286 | $arr[$tag] = trim($element);
287 | if (!empty($e)) {
288 | $arr[$tag] = $element instanceof \SimpleXMLElement ? $this->xml2array($element) : $e;
289 | }
290 | }
291 |
292 | return $arr;
293 | }
294 | }
295 |
--------------------------------------------------------------------------------
/wsdl/UslugaBIRzewnPubl_wsdl0.xsd:
--------------------------------------------------------------------------------
1 |
2 |
15 |
16 |
17 |
19 |
21 |
23 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
89 |
91 |
92 |
93 |
95 |
97 |
98 |
99 |
100 |
102 |
104 |
105 |
106 |
108 |
110 |
111 |
112 |
113 |
115 |
117 |
118 |
119 |
121 |
123 |
124 |
125 |
127 |
129 |
130 |
131 |
133 |
135 |
136 |
137 |
139 |
141 |
142 |
143 |
--------------------------------------------------------------------------------