Skip to content

Commit 1976ea4

Browse files
committed
Initial commit
0 parents  commit 1976ea4

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Krzysztof Graman
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 all
13+
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 THE
21+
SOFTWARE.

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "zalazdi/wfirma",
3+
"description": "wFirma PHP Library",
4+
"keywords": ["php", "wfirma"],
5+
"type": "library",
6+
"require": {
7+
"guzzlehttp/guzzle": "~6.0"
8+
},
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Krzysztof Graman",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"minimum-stability": "dev",
17+
"autoload": {
18+
"psr-4": {
19+
"Zalazdi\\wFirma\\": "src/"
20+
}
21+
},
22+
"extra": {
23+
"branch-alias": {
24+
"dev-master": "1.0.x-dev"
25+
}
26+
}
27+
}

src/Client.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Zalazdi\wFirma;
4+
5+
use function GuzzleHttp\json_decode;
6+
7+
class Client
8+
{
9+
/**
10+
* @var string API Url
11+
*/
12+
protected $url = 'https://api2.wfirma.pl/';
13+
14+
/**
15+
* @var string API username
16+
*/
17+
protected $username;
18+
19+
/**
20+
* @var string API password
21+
*/
22+
protected $password;
23+
24+
/**
25+
* @var \GuzzleHttp\Client
26+
*/
27+
protected $guzzle;
28+
29+
/**
30+
* Client constructor.
31+
*
32+
* @param string $username API username
33+
* @param string $password API password
34+
*/
35+
public function __construct($username, $password)
36+
{
37+
$this->username = $username;
38+
$this->password = $password;
39+
40+
$this->initializeGuzzle();
41+
}
42+
43+
public function execute(Query $query)
44+
{
45+
$response = $this->guzzle->request(
46+
'POST',
47+
$query->path,
48+
['json' => $query->parameters]
49+
);
50+
51+
$body = json_decode($response->getBody()->getContents(), true);
52+
53+
return $body;
54+
}
55+
56+
protected function initializeGuzzle()
57+
{
58+
$this->guzzle = new \GuzzleHttp\Client([
59+
'base_uri' => $this->url,
60+
'auth' => [$this->username, $this->password],
61+
]);
62+
}
63+
}

src/Query.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Zalazdi\wFirma;
4+
5+
class Query
6+
{
7+
/**
8+
* @var string URL
9+
*/
10+
public $path;
11+
public $parameters = [];
12+
13+
/**
14+
* Query constructor.
15+
*
16+
* @param string $module Module name
17+
* @param string $function Function name
18+
*/
19+
public function __construct($module, $function)
20+
{
21+
$this->path = $module.'/'.$function.'?inputFormat=json&outputFormat=json';
22+
}
23+
24+
/**
25+
* Add parameters
26+
*
27+
* @param array $parameters Parameters array as name => value
28+
*/
29+
public function addParameters($parameters = [])
30+
{
31+
$this->parameters = array_merge($this->parameters, $parameters);
32+
}
33+
34+
/**
35+
* Add parameter
36+
*
37+
* @param string $name Parameter name
38+
* @param string $value Parameter value
39+
*/
40+
public function addParameter($name, $value)
41+
{
42+
$this->parameters[$name] = $value;
43+
}
44+
}

src/Repositories/Repository.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Zalazdi\wFirma\Repositories;
4+
5+
use Zalazdi\wFirma\Client;
6+
use Zalazdi\wFirma\Query;
7+
8+
abstract class Repository
9+
{
10+
protected $client;
11+
12+
public $name;
13+
14+
public function __construct(Client $client)
15+
{
16+
$this->client = $client;
17+
}
18+
19+
public function get()
20+
{
21+
22+
}
23+
24+
public function find($limit = 10, $page = 1, $parameters = false)
25+
{
26+
// @ToDo Add parameters
27+
28+
$query = $this->newQuery('find');
29+
$query->addParameters([
30+
'limit' => $limit,
31+
'page' => $page
32+
]);
33+
34+
$result = $this->client->execute($query);
35+
36+
// @ToDo Return collection instead of Array
37+
if (array_get($result, 'status.code') == 'OK') {
38+
return $result[$this->name];
39+
}
40+
}
41+
42+
public function add()
43+
{
44+
45+
}
46+
47+
public function edit()
48+
{
49+
50+
}
51+
52+
public function delete()
53+
{
54+
55+
}
56+
57+
protected function newQuery($function)
58+
{
59+
return new Query($this->name, $function);
60+
}
61+
}

src/Repositories/SeriesRepository.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Zalazdi\wFirma\Repositories;
4+
5+
class SeriesRepository extends Repository
6+
{
7+
public $name = 'series';
8+
9+
}

0 commit comments

Comments
 (0)