first commit
This commit is contained in:
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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user