first commit
This commit is contained in:
21
lib/Service/Common/Authority/MockAuthorityReqRegistry.php
Normal file
21
lib/Service/Common/Authority/MockAuthorityReqRegistry.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Yeepay\Yop\Sdk\Service\Common\Authority;
|
||||
|
||||
use Yeepay\Yop\Sdk\Auth\AuthorizationReq;
|
||||
use Yeepay\Yop\Sdk\Auth\AuthorizationReqRegistry;
|
||||
use Yeepay\Yop\Sdk\Auth\AuthorizationReqSupport;
|
||||
|
||||
class MockAuthorityReqRegistry implements AuthorizationReqRegistry
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $operationId string
|
||||
* @return AuthorizationReq
|
||||
*/
|
||||
public function getAuthorizationReq($operationId)
|
||||
{
|
||||
return AuthorizationReqSupport::getAuthorizationReq('YOP-RSA2048-SHA256');
|
||||
}
|
||||
|
||||
}
|
||||
154
lib/Service/Common/Model/YopRequest.php
Normal file
154
lib/Service/Common/Model/YopRequest.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace Yeepay\Yop\Sdk\Service\Common\Model;
|
||||
|
||||
use Yeepay\Yop\Sdk\Model\BaseRequest;
|
||||
|
||||
class YopRequest extends BaseRequest
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $apiUri;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $method;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $parameters = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $multipartFiles = [];
|
||||
|
||||
/**
|
||||
* @var string | resource
|
||||
*/
|
||||
private $content;
|
||||
|
||||
/**
|
||||
* YopRequest constructor.
|
||||
* @param string $apiUri
|
||||
* @param string $method
|
||||
*/
|
||||
public function __construct($apiUri, $method)
|
||||
{
|
||||
$this->apiUri = $apiUri;
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getApiUri()
|
||||
{
|
||||
return $this->apiUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $parameters
|
||||
* @return YopRequest
|
||||
*/
|
||||
public function setParameters($parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $values
|
||||
* @return YopRequest
|
||||
*/
|
||||
public function addParameters($name, array $values)
|
||||
{
|
||||
$this->parameters[$name] = $values;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @return YopRequest
|
||||
*/
|
||||
public function addParameter($name, $value)
|
||||
{
|
||||
if (isset($this->parameters[$name])) {
|
||||
$values = $this->parameters[$name];
|
||||
$values[] = $value;
|
||||
} else {
|
||||
$this->parameters[$name] = [$value];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getMultipartFiles()
|
||||
{
|
||||
return $this->multipartFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string|resource $file
|
||||
* @return YopRequest
|
||||
*/
|
||||
public function addMultiPartFile($name, $file)
|
||||
{
|
||||
if (isset($this->multipartFiles[$name])) {
|
||||
$values = $this->multipartFiles[$name];
|
||||
$values[] = $file;
|
||||
} else {
|
||||
$this->multipartFiles[$name] = [$file];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return resource|string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource|string $content
|
||||
* @return YopRequest
|
||||
*/
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->content = $content;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
78
lib/Service/Common/Model/YopRequestMarshaller.php
Normal file
78
lib/Service/Common/Model/YopRequestMarshaller.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Yeepay\Yop\Sdk\Service\Common\Model;
|
||||
|
||||
use Yeepay\Yop\Sdk\Exception\YopClientException;
|
||||
use Yeepay\Yop\Sdk\Http\ContentType;
|
||||
use Yeepay\Yop\Sdk\Http\Headers;
|
||||
use Yeepay\Yop\Sdk\Internal\DefaultRequest;
|
||||
use Yeepay\Yop\Sdk\Internal\Request;
|
||||
use Yeepay\Yop\Sdk\Model\Transform\RequestMarshaller;
|
||||
|
||||
class YopRequestMarshaller implements RequestMarshaller
|
||||
{
|
||||
|
||||
/**
|
||||
* @param YopRequest $request
|
||||
* @return Request
|
||||
* @throws YopClientException
|
||||
*/
|
||||
public function marshal($request)
|
||||
{
|
||||
$parts = explode('/', $request->getApiUri());
|
||||
$result = new DefaultRequest($parts[3]);
|
||||
$result->setResourcePath($request->getApiUri());
|
||||
$result->setHttpMethod($request->getMethod());
|
||||
if ($parts[1] == 'yos') {
|
||||
$result->setYosFlag(true);
|
||||
}
|
||||
if (!empty($request->getMultipartFiles())) {
|
||||
foreach ($request->getMultipartFiles() as $key => $values) {
|
||||
foreach ($values as $value) {
|
||||
$result->addMultiPartFile($key, $value);
|
||||
}
|
||||
}
|
||||
if (!empty($request->getParameters())) {
|
||||
$result->setParameters($this->marshalParameters($request->getParameters()));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
if (!empty($request->getParameters())) {
|
||||
$result->setParameters($this->marshalParameters($request->getParameters()));
|
||||
$result->addHeader(Headers::CONTENT_TYPE, ContentType::APPLICATION_FORM_URLENCODED);
|
||||
|
||||
return $result;
|
||||
}
|
||||
if (!empty($request->getContent())) {
|
||||
if (is_string($request->getContent())) {
|
||||
$result->addHeader(Headers::CONTENT_TYPE, ContentType::APPLICATION_JSON);
|
||||
} else {
|
||||
$result->addHeader(Headers::CONTENT_TYPE, ContentType::APPLICATION_OCTET_STREAM);
|
||||
}
|
||||
$result->setContent(\GuzzleHttp\Psr7\stream_for($request->getContent()));
|
||||
|
||||
return $result;
|
||||
}
|
||||
throw new YopClientException("empty request");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $parameters
|
||||
* @return array
|
||||
*/
|
||||
private function marshalParameters(array $parameters)
|
||||
{
|
||||
$targetParameters = [];
|
||||
foreach ($parameters as $key => $values) {
|
||||
$targetValues = [];
|
||||
foreach ($values as $value) {
|
||||
$targetValues[] = strval($value);
|
||||
}
|
||||
$targetParameters[$key] = $targetValues;
|
||||
}
|
||||
|
||||
return $targetParameters;
|
||||
}
|
||||
|
||||
}
|
||||
36
lib/Service/Common/Model/YopResponse.php
Normal file
36
lib/Service/Common/Model/YopResponse.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Yeepay\Yop\Sdk\Service\Common\Model;
|
||||
|
||||
use Yeepay\Yop\Sdk\Model\BaseResponse;
|
||||
|
||||
class YopResponse extends BaseResponse
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $data;
|
||||
|
||||
function getResultClass()
|
||||
{
|
||||
return "object";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $result
|
||||
*/
|
||||
function setResult($result)
|
||||
{
|
||||
$this->data = $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getResult()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
}
|
||||
19
lib/Service/Common/Model/YopResponseUnMarshaller.php
Normal file
19
lib/Service/Common/Model/YopResponseUnMarshaller.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Yeepay\Yop\Sdk\Service\Common\Model;
|
||||
|
||||
use Yeepay\Yop\Sdk\Model\BaseResponse;
|
||||
use Yeepay\Yop\Sdk\Model\Transform\BaseResponseUnMarshaller;
|
||||
|
||||
class YopResponseUnMarshaller extends BaseResponseUnMarshaller
|
||||
{
|
||||
|
||||
/**
|
||||
* @return BaseResponse
|
||||
*/
|
||||
protected function getResponseInstance()
|
||||
{
|
||||
return new YopResponse();
|
||||
}
|
||||
|
||||
}
|
||||
59
lib/Service/Common/YopClient.php
Normal file
59
lib/Service/Common/YopClient.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Yeepay\Yop\Sdk\Service\Common;
|
||||
|
||||
use Yeepay\Yop\Sdk\Client\ClientExecutionParams;
|
||||
use Yeepay\Yop\Sdk\Client\ClientHandler;
|
||||
use Yeepay\Yop\Sdk\Client\ClientParams;
|
||||
use Yeepay\Yop\Sdk\Exception\YopClientException;
|
||||
use Yeepay\Yop\Sdk\Model\Transform\YosDownloadResponseUnMarshaller;
|
||||
use Yeepay\Yop\Sdk\Model\YosDownloadResponse;
|
||||
use Yeepay\Yop\Sdk\Service\Common\Model\YopRequest;
|
||||
use Yeepay\Yop\Sdk\Service\Common\Model\YopRequestMarshaller;
|
||||
use Yeepay\Yop\Sdk\Service\Common\Model\YopResponse;
|
||||
use Yeepay\Yop\Sdk\Service\Common\Model\YopResponseUnMarshaller;
|
||||
|
||||
class YopClient
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ClientHandler
|
||||
*/
|
||||
private $clientHandler;
|
||||
|
||||
/**
|
||||
* YopClient constructor.
|
||||
* @param ClientParams $clientParams
|
||||
*/
|
||||
function __construct(ClientParams $clientParams)
|
||||
{
|
||||
$this->clientHandler = new ClientHandler($clientParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param YopRequest $request
|
||||
* @return YopResponse
|
||||
* @throws YopClientException
|
||||
*/
|
||||
public function sendRequest(YopRequest $request)
|
||||
{
|
||||
$clientExecutionParams = new ClientExecutionParams($request, new YopRequestMarshaller(),
|
||||
new YopResponseUnMarshaller());
|
||||
|
||||
return $this->clientHandler->execute($clientExecutionParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param YopRequest $request
|
||||
* @return YosDownloadResponse
|
||||
* @throws YopClientException
|
||||
*/
|
||||
public function sendDownloadRequest(YopRequest $request)
|
||||
{
|
||||
$clientExecutionParams = new ClientExecutionParams($request, new YopRequestMarshaller(),
|
||||
new YosDownloadResponseUnMarshaller());
|
||||
|
||||
return $this->clientHandler->execute($clientExecutionParams);
|
||||
}
|
||||
|
||||
}
|
||||
64
lib/Service/Common/YopClientBuilder.php
Normal file
64
lib/Service/Common/YopClientBuilder.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Yeepay\Yop\Sdk\Service\Common;
|
||||
|
||||
use Yeepay\Yop\Sdk\Client\ClientParams;
|
||||
use Yeepay\Yop\Sdk\Client\Support\ClientParamsSupport;
|
||||
use Yeepay\Yop\Sdk\Config\AppSdkConfig;
|
||||
use Yeepay\Yop\Sdk\Config\AppSdkConfigProvider;
|
||||
use Yeepay\Yop\Sdk\Config\DefaultAppSdkConfigProvider;
|
||||
use Yeepay\Yop\Sdk\Exception\YopClientException;
|
||||
use Yeepay\Yop\Sdk\Service\Common\Authority\MockAuthorityReqRegistry;
|
||||
|
||||
class YopClientBuilder
|
||||
{
|
||||
|
||||
private static $authorizationReqRegistry;
|
||||
|
||||
public static function __init()
|
||||
{
|
||||
self::$authorizationReqRegistry = new MockAuthorityReqRegistry();
|
||||
}
|
||||
|
||||
/**
|
||||
* @var ClientParams
|
||||
*/
|
||||
private $clientParams;
|
||||
|
||||
/**
|
||||
* YopClientBuilder constructor.
|
||||
* @param ClientParams $clientParams
|
||||
*/
|
||||
public function __construct(ClientParams $clientParams)
|
||||
{
|
||||
$this->clientParams = $clientParams;
|
||||
}
|
||||
|
||||
public function build()
|
||||
{
|
||||
return new YopClient($this->clientParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $config AppSdkConfig|array|AppSdkConfigProvider
|
||||
* @return YopClientBuilder
|
||||
* @throws YopClientException
|
||||
*/
|
||||
public static function builder($config)
|
||||
{
|
||||
$appSdkConfigProvider = null;
|
||||
if ($config instanceof AppSdkConfigProvider) {
|
||||
$appSdkConfigProvider = $config;
|
||||
} else {
|
||||
$appSdkConfigProvider = new DefaultAppSdkConfigProvider($config);
|
||||
}
|
||||
$clientParams = ClientParamsSupport::generateClientParams($appSdkConfigProvider);
|
||||
$clientParams->setAuthorizationReqRegistry(self::$authorizationReqRegistry);
|
||||
|
||||
return new YopClientBuilder($clientParams);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
YopClientBuilder::__init();
|
||||
|
||||
Reference in New Issue
Block a user