Skip to content

Commit 37bce6b

Browse files
committed
init
0 parents  commit 37bce6b

15 files changed

Lines changed: 602 additions & 0 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# alibaba-api
2+
Alibaba api

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "waimao/alibaba-api",
3+
"type": "library",
4+
"description": "Api for Alibaba",
5+
"keywords": ["Alibaba"],
6+
"homepage": "https://github.com/reddrake/alibaba-api",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "reddrake",
11+
"email": "eagleftf123@gmail.com"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.5",
16+
"guzzlehttp/guzzle": "^6.2"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "4.7.*"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"AlibabaApi\\": "src/"
24+
}
25+
}
26+
}

src/Client.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace AlibabaApi;
4+
5+
use AlibabaApi\Config\ConfigInterface;
6+
use AlibabaApi\Operations\OperationInterface;
7+
use AlibabaApi\Request\RequestFactory;
8+
use AlibabaApi\ResponseTransformer\ResponseTransformerFactory;
9+
10+
class Client{
11+
12+
protected $config;
13+
14+
public function __construct(ConfigInterface $config = null){
15+
$this->config = $config;
16+
}
17+
18+
public function runOperation(OperationInterface $operation, ConfigInterface $config = null)
19+
{
20+
$config = is_null($config) ? $this->config: $config;
21+
22+
if(true === is_null($config)){
23+
throw new \Exception("No configuration passed. ");
24+
}
25+
26+
$requestObject = RequestFactory::createRequest($config);
27+
28+
$response = $requestObject->perform($operation);
29+
30+
return $this->applyResponseTransformer($response, $config);
31+
}
32+
33+
protected function applyResponseTransformer($response, ConfigInterface $config)
34+
{
35+
if (true === is_null($config->getResponseTransformer())) {
36+
return $response;
37+
}
38+
39+
$responseTransformer = ResponseTransformerFactory::createResponseTransformer($config);
40+
41+
return $responseTransformer->transform($response);
42+
}
43+
}

src/Config/ConfigInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace AlibabaApi\Config;
3+
4+
interface ConfigInterface
5+
{
6+
public function getApiKey();
7+
8+
public function getApiSecret();
9+
10+
public function getAccessToken();
11+
12+
public function getRefreshToken();
13+
14+
public function getRequest();
15+
16+
public function getRequestFactory();
17+
18+
public function getResponseTransformer();
19+
20+
public function getResponseTransformerFactory();
21+
}

src/Config/GenericConfig.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace AlibabaApi\Config;
4+
5+
use AlibabaApi\Config\ConfigInterface;
6+
7+
class GenericConfig implements ConfigInterface
8+
{
9+
protected $apiKey;
10+
11+
protected $apiSecret;
12+
13+
protected $accessToken;
14+
15+
protected $refreshToken;
16+
17+
protected $request = '\AlibabaApi\Request\Request';
18+
19+
protected $requestFactory = null;
20+
21+
protected $responseTransformer = null;
22+
23+
protected $responseTransformerFactory = null;
24+
25+
public function getApiKey()
26+
{
27+
return $this->apiKey;
28+
}
29+
30+
public function setApiKey($apiKey)
31+
{
32+
$this->apiKey = $apiKey;
33+
34+
return $this;
35+
}
36+
37+
public function getApiSecret()
38+
{
39+
return $this->apiSecret;
40+
}
41+
42+
public function setApiSecret($apiSecret)
43+
{
44+
$this->apiSecret = $apiSecret;
45+
46+
return $this;
47+
}
48+
49+
public function getAccessToken()
50+
{
51+
return $this->accessToken;
52+
}
53+
54+
public function setAccessToken($accessToken)
55+
{
56+
$this->accessToken = $accessToken;
57+
58+
return $this;
59+
}
60+
61+
public function getRefreshToken()
62+
{
63+
return $this->refreshToken;
64+
}
65+
66+
public function setRefreshToken($refreshToken)
67+
{
68+
$this->refreshToken = $refreshToken;
69+
70+
return $this;
71+
}
72+
73+
public function getRequest()
74+
{
75+
return $this->request;
76+
}
77+
78+
public function setRequest($request)
79+
{
80+
$this->request = $request;
81+
82+
return $this;
83+
}
84+
85+
public function getRequestFactory()
86+
{
87+
return $this->requestFactory;
88+
}
89+
90+
public function setRequestFactory($callback)
91+
{
92+
if (!is_callable($callback)) {
93+
throw new \InvalidArgumentException("Given argument is not callable");
94+
}
95+
96+
$this->requestFactory = $callback;
97+
98+
return $this;
99+
}
100+
101+
public function getResponseTransformer()
102+
{
103+
return $this->responseTransformer;
104+
}
105+
106+
public function setResponseTransformer($responseTransformer)
107+
{
108+
$this->responseTransformer = $responseTransformer;
109+
110+
return $this;
111+
}
112+
113+
public function getResponseTransformerFactory()
114+
{
115+
return $this->responseTransformerFactory;
116+
}
117+
118+
public function setResponseTransformerFactory($callback)
119+
{
120+
if (!is_callable($callback)) {
121+
throw new \InvalidArgumentException("Given argument is not callable");
122+
}
123+
124+
$this->responseTransformerFactory = $callback;
125+
126+
return $this;
127+
}
128+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace AlibabaApi\Operations;
4+
5+
abstract class AbstractOperation implements OperationInterface
6+
{
7+
protected $parameter = array();
8+
9+
public function getOperationParameter()
10+
{
11+
return $this->parameter;
12+
}
13+
14+
public function __call($methodName, $parameter)
15+
{
16+
if (substr($methodName, 0, 3) == 'set') {
17+
$this->parameter[substr($methodName, 3)] = array_shift($parameter);
18+
19+
return $this;
20+
}
21+
22+
if (substr($methodName, 0, 3) == 'get') {
23+
$keyName = substr($methodName, 3);
24+
25+
return isset($this->parameter[$keyName]) ? $this->parameter[$keyName] : null;
26+
}
27+
28+
throw new \BadFunctionCallException(sprintf('The function "%s" does not exist!', $methodName));
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace AlibabaApi\Operations;
4+
5+
interface OperationInterface
6+
{
7+
public function getName();
8+
9+
public function getOperationParameter();
10+
}

src/Operations/ProductInfoById.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace AlibabaApi\Operations;
4+
5+
class ProductInfoById extends AbstractOperation
6+
{
7+
public function getName()
8+
{
9+
return 'alibaba.icbu.product.get';
10+
}
11+
12+
public function setProductId($productId)
13+
{
14+
$this->parameter['product_id'] = $productId;
15+
return $this;
16+
}
17+
18+
public function setLanguage($language = 'ENGLISH')
19+
{
20+
$this->parameter['language'] = $language;
21+
return $this;
22+
}
23+
}

src/Operations/ProductInfoList.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace AlibabaApi\Operations;
4+
5+
class ProductInfoList extends AbstractOperation
6+
{
7+
public function getName()
8+
{
9+
return 'alibaba.icbu.product.list';
10+
}
11+
12+
public function setSubject($subject)
13+
{
14+
$this->parameter['subject'] = $subject;
15+
return $this;
16+
}
17+
18+
public function setCategoryId($categoryId){
19+
$this->parameter['category_id'] = $categoryId;
20+
return $this;
21+
}
22+
public function setPageSize($pageSize)
23+
{
24+
$this->parameter['page_size'] = $pageSize;
25+
return $this;
26+
}
27+
28+
public function setCurrentPage($currentPage)
29+
{
30+
$this->parameter['current_page'] = $currentPage;
31+
return $this;
32+
}
33+
34+
public function setLanguage($language = 'ENGLISH')
35+
{
36+
$this->parameter['language'] = $language;
37+
return $this;
38+
}
39+
}

src/Operations/VasSubscribeGet.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace AlibabaApi\Operations;
4+
5+
class VasSubscribeGet extends AbstractOperation
6+
{
7+
public function getName()
8+
{
9+
return 'taobao.vas.subscribe.get';
10+
}
11+
12+
public function setArticleCode($article_code)
13+
{
14+
$this->parameter['article_code'] = $article_code;
15+
return $this;
16+
}
17+
18+
public function setNick($nick)
19+
{
20+
$this->parameter['nick'] = $nick;
21+
return $this;
22+
}
23+
}

0 commit comments

Comments
 (0)