Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.

Commit 0043a18

Browse files
author
Ioannis Lazaridis
committed
Initial commit
0 parents  commit 0043a18

21 files changed

+554
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Ioannis Lazaridis
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.

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Skroutz
2+
[![Travis](https://img.shields.io/travis/ilazaridis/skroutz.svg?style=flat-square)](https://travis-ci.org/ilazaridis/skroutz)
3+
[![Latest Stable Version](https://img.shields.io/packagist/v/ilazaridis/skroutz.svg?style=flat-square)](https://packagist.org/packages/ilazaridis/skroutz)
4+
[![Minimum PHP Version](https://img.shields.io/badge/php-%E2%89%A5%207.0-8892BF.svg?style=flat-square)](https://php.net/)
5+
6+
PHP7 Client for [skroutz](https://skroutz.gr) API
7+
## Installation
8+
```bash
9+
composer require ilazaridis/skroutz
10+
```
11+
## Usage Examples
12+
Once you have an ```Identifier``` and a ```Secret``` you are ready to generate an access token:
13+
```php
14+
<?php
15+
16+
require_once 'vendor/autoload.php';
17+
use ilazaridis\skroutz\Client as Client;
18+
19+
$client = new Client('identifier', 'secret');
20+
```
21+
and consume it:
22+
```php
23+
print_r($client->category('40')->fetch()); // get category with id=40
24+
```
25+
```fetch(bool $decode = true, string $apiVersion = '3.1')``` will return an associative array using latest version of api by default. You can pass a boolean and a string if you want to change these values.
26+
- List the children categories of a category
27+
```php
28+
$client->category('40')->children()->fetch()
29+
```
30+
- Retrieve a single shop location
31+
```php
32+
$client->shop('452')->location('2500')->fetch()
33+
```
34+
- Retrieve manufacturer's categories order by name
35+
```php
36+
$client->category('25')->manufacturers()->params(['order_by' => 'name', 'order_dir' => 'asc'])->fetch()
37+
```
38+
The query string is passd as associative array using the params() method.
39+
In case we have multiple values of the same parameter (i.e. ```filter_ids[]```), we are passing them as comma seperated values:
40+
- Filter SKUs of specific category using multiple filter ids
41+
```php
42+
$client->category('40')->skus()->params(['filter_ids[]' => '355559,6282']);
43+
```
44+
## Note
45+
Some of the API methods have not been implemented due to access restriction. Currently,
46+
the acquired permission level is ```public``` and everything on that level was implemented.

composer.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "ilazaridis/skroutz",
3+
"description": "A PHP7 library for skroutz.gr API",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Ioannis Lazaridis",
9+
"email": "ioannis.lazaridis@gmail.com"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": { "ilazaridis\\skroutz\\": "src/" }
14+
},
15+
"autoload-dev": {
16+
"psr-4": { "ilazaridis\\skroutz\\tests\\": "tests/" }
17+
},
18+
"require": {
19+
"php": ">=7.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "5.5.*"
23+
}
24+
}

phpunit.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Skroutz Test Suite">
15+
<directory suffix="Test.php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
<filter>
19+
<whitelist>
20+
<directory suffix=".php">./src/</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

src/Client.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace ilazaridis\skroutz;
2+
3+
class Client
4+
{
5+
const OATH2_LINK = 'https://www.skroutz.gr/oauth2/token?client_id=%s&client_secret=%s&grant_type=client_credentials&scope=public';
6+
private $identifier, $secret;
7+
public $token;
8+
9+
function __construct($identifier, $secret)
10+
{
11+
$this->identifier = $identifier;
12+
$this->secret = $secret;
13+
if (!in_array('curl', get_loaded_extensions())) {
14+
throw new \Exception('Curl module is not enabled!');
15+
}
16+
$this->token = $this->generateToken($this->identifier, $this->secret);
17+
}
18+
19+
private function generateToken($identifier, $secret) : string
20+
{
21+
$oathUrl = sprintf(self::OATH2_LINK, $identifier, $secret);
22+
$ch = curl_init($oathUrl);
23+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
24+
curl_setopt($ch, CURLOPT_POST, 1);
25+
$_response = curl_exec($ch);
26+
curl_close($ch);
27+
$response = json_decode($_response, true);
28+
return $response['access_token'] ?? '';
29+
}
30+
31+
public function __call($name, $arguments)
32+
{
33+
$class = '\\ilazaridis\\skroutz\\resources\\'.ucwords($name);
34+
$resourceId = isset($arguments[0]) ? '/'.$arguments[0] : '';
35+
if (class_exists($class)) {
36+
return new $class($this->token, $resourceId);
37+
}
38+
throw new \Exception("Undefined resource [{$name}] called.");
39+
}
40+
}

src/resources/Api.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Api
4+
{
5+
private $token;
6+
const API_URL = 'http://api.skroutz.gr';
7+
8+
public function __construct($token, string $id = '')
9+
{
10+
$this->token = $token;
11+
$this->url .= $id;
12+
return $this;
13+
}
14+
15+
public function fetch(bool $decode = true, string $apiVersion = '3.1')
16+
{
17+
$ch = curl_init(self::API_URL.$this->url);
18+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
19+
curl_setopt($ch,
20+
CURLOPT_HTTPHEADER,
21+
[
22+
'Accept: application/vnd.skroutz+json; version='.$apiVersion,
23+
'Authorization: Bearer '.$this->token
24+
]
25+
);
26+
$response = curl_exec($ch);
27+
curl_close($ch);
28+
if (empty($response)) {
29+
throw new \Exception("Resource does not exist");
30+
}
31+
return $decode ? json_decode($response, true) : $response;
32+
}
33+
34+
public function __call($name, $arguments)
35+
{
36+
$parameters = '';
37+
$querySegment = [];
38+
if (!empty($arguments)) {
39+
if ($name === 'params') {
40+
$multipleValues = array_filter($arguments[0], function($v) {
41+
return strpos($v, ',');
42+
});
43+
44+
foreach ($multipleValues as $k => $v) {
45+
$valuesToArray = explode(',', $v);
46+
$querySegment[] = array_map(function ($_v) use ($k) {
47+
return $k.'='.trim($_v);
48+
}, $valuesToArray);
49+
}
50+
51+
$argumentsWithoutParams = $multipleValues ? array_diff($arguments[0], $multipleValues) : $arguments[0];
52+
$params = array_map(function($e) {
53+
return implode('&', $e);
54+
}, $querySegment);
55+
$delimiter = (!empty($params) && !empty($argumentsWithoutParams)) ? '&' : '';
56+
$parameters = '?'.http_build_query($argumentsWithoutParams, '', '&').$delimiter.implode('&', $params);
57+
} else {
58+
$parameters = '/'.$arguments[0];
59+
}
60+
}
61+
$method = $name === 'params' ? '' : '/'.$name;
62+
$this->url .= $method.$parameters;
63+
return $this;
64+
}
65+
}

src/resources/Autocomplete.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Autocomplete extends Api
4+
{
5+
public $url = '/autocomplete';
6+
}

src/resources/Category.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Category extends Api
4+
{
5+
public $url = '/categories';
6+
}

src/resources/Flag.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Flag extends Api
4+
{
5+
public $url = '/flags';
6+
}

src/resources/Manufacturer.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Manufacturer extends Api
4+
{
5+
public $url = '/manufacturers';
6+
}

src/resources/Product.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Product extends Api
4+
{
5+
public $url = '/products';
6+
}

src/resources/Search.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Search extends Api
4+
{
5+
public $url = '/search';
6+
}

src/resources/Shop.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Shop extends Api
4+
{
5+
public $url = '/shops';
6+
}

src/resources/Sku.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace ilazaridis\skroutz\resources;
2+
3+
class Sku extends Api
4+
{
5+
public $url = '/skus';
6+
}

tests/CategoryTest.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php namespace ilazaridis\Tests;
2+
3+
use PHPUnit\Framework\TestCase;
4+
use ilazaridis\skroutz\Client;
5+
6+
7+
class CategoryTest extends TestCase
8+
{
9+
protected $client;
10+
11+
public function setUp()
12+
{
13+
$this->client = new Client('test', 'test');
14+
}
15+
16+
public function testListAllCategories()
17+
{
18+
$request = $this->client->category();
19+
$this->assertEquals($request->url, '/categories');
20+
}
21+
22+
public function testSingleCategory()
23+
{
24+
$request = $this->client->category('1442');
25+
$this->assertEquals($request->url, '/categories/1442');
26+
}
27+
28+
public function testCategoryParent()
29+
{
30+
$request = $this->client->category('1442')->parent();
31+
$this->assertEquals($request->url, '/categories/1442/parent');
32+
}
33+
34+
public function testCategoryRoot()
35+
{
36+
$request = $this->client->category()->root();
37+
$this->assertEquals($request->url, '/categories/root');
38+
}
39+
40+
public function testCategoryChildren()
41+
{
42+
$request = $this->client->category('252')->children();
43+
$this->assertEquals($request->url, '/categories/252/children');
44+
}
45+
46+
public function testCategorySpecs()
47+
{
48+
$request = $this->client->category('40')->specifications();
49+
$this->assertEquals($request->url, '/categories/40/specifications');
50+
}
51+
52+
public function testCategorySpecsIncGroup()
53+
{
54+
$request = $this->client->category('40')->specifications()->params(['include' => 'group']);
55+
$this->assertEquals($request->url, '/categories/40/specifications?include=group');
56+
}
57+
58+
public function testCategoryManufacturers()
59+
{
60+
$request = $this->client->category('25')->manufacturers();
61+
$this->assertEquals($request->url, '/categories/25/manufacturers');
62+
}
63+
64+
public function testCategoryManufacturersOrder()
65+
{
66+
$request = $this->client->category('25')->manufacturers()->params(['order_by' => 'name', 'order_dir' => 'asc']);
67+
$this->assertEquals($request->url, '/categories/25/manufacturers?order_by=name&order_dir=asc');
68+
}
69+
70+
public function testCategorySku()
71+
{
72+
$request = $this->client->category('40')->skus()->params(['order_by' => 'rating']);
73+
$this->assertEquals($request->url, '/categories/40/skus?order_by=rating');
74+
}
75+
76+
public function testCategorySkuFilterByManufacturer()
77+
{
78+
$request = $this->client->category('40')->skus()->params(['manufacturer_ids[]' => '28,2']);
79+
$this->assertEquals($request->url, '/categories/40/skus?manufacturer_ids[]=28&manufacturer_ids[]=2');
80+
}
81+
82+
public function testCategorySkuFilterByFilters()
83+
{
84+
$request = $this->client->category('40')->skus()->params(['filter_ids[]' => '355559, 6282']);
85+
$this->assertEquals('/categories/40/skus?filter_ids[]=355559&filter_ids[]=6282', $request->url);
86+
}
87+
88+
public function testCategorySkuFilterManufacturersMeta()
89+
{
90+
//http://api.skroutz.gr/categories/40/skus?include_meta=applied_filters&filter_ids[]=6282&manufacturer_ids[]=28
91+
$request = $this->client->category('40')->skus()->params(['include_meta' => 'applied_filters', 'filter_ids[]' => '355559,6282', 'manufacturer_ids[]' => '26,24']);
92+
$this->assertEquals('/categories/40/skus?include_meta=applied_filters&filter_ids[]=355559&filter_ids[]=6282&manufacturer_ids[]=26&manufacturer_ids[]=24', $request->url);
93+
}
94+
}

0 commit comments

Comments
 (0)