Skip to content

Commit

Permalink
php语言工具lib
Browse files Browse the repository at this point in the history
  • Loading branch information
刘志永 committed Sep 5, 2021
1 parent 93136d3 commit 9dea228
Show file tree
Hide file tree
Showing 14 changed files with 488 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kernel/php/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
composer.phar
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
composer.lock

.vscode/
.idea
.DS_Store

cache/
*.cache
runtime/
21 changes: 21 additions & 0 deletions kernel/php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "anubis_openapi/kernel",
"description": "anubis openapi kernel Output Library for PHP",
"minimum-stability": "stable",
"type": "library",
"license": "Apache-2.0",
"authors": [
{
"name": "liuzhiyong",
"email": "zhiyongliu.lzy@alibaba-inc.com"
}
],
"require": {
"php": "7.3",
"monolog/monolog": "^1.0|^2.1",
"alibabacloud/tea": "^3.1.22",
},
"require-dev": {
"phpunit/phpunit": "^7.5"
}
}
14 changes: 14 additions & 0 deletions kernel/php/kernel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
composer.phar
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
composer.lock

.vscode/
.idea
.DS_Store

cache/
*.cache
runtime/
21 changes: 21 additions & 0 deletions kernel/php/kernel/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "anubis_openapi/kernel",
"description": "anubis openapi kernel Output Library for PHP",
"minimum-stability": "stable",
"type": "library",
"license": "Apache-2.0",
"authors": [
{
"name": "liuzhiyong",
"email": "zhiyongliu.lzy@alibaba-inc.com"
}
],
"require": {
"php": "7.3",
"monolog/monolog": "^1.0|^2.1",
"alibabacloud/tea": "^3.1.22",
},
"require-dev": {
"phpunit/phpunit": "^7.5"
}
}
13 changes: 13 additions & 0 deletions kernel/php/kernel/src/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace EleMe\OpenApi\Kernel;

use PHPUnit\Framework\TestCase;
final class ClientTest extends TestCase{

public function testA()
{
$a = 'a1111';
echo $a;
self::assertEquals($a,'a1111');
}
}
18 changes: 18 additions & 0 deletions kernel/php/kernel/src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


namespace EleMe\OpenApi\Kernel;
use AlibabaCloud\Tea\Model;

class Config extends Model
{
public $protocol;
public $gatewayHost;
public $appId;
public $accessToken;
public $merchantId;
public $secretKey;
public $httpProxy;
public $ignoreSSL;

}
108 changes: 108 additions & 0 deletions kernel/php/kernel/src/EasySDKKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace EleMe\OpenApi\Kernel;
use EleMe\OpenApi\Kernel\ElemeConstants;
use EleMe\OpenApi\Kernel\Util\JsonUtil;


class EasySDKKernel{
private $config;

private $textParams;

private $bizParams;

public function __construct($config)
{
$this->config = $config;
}
public function getTimestamp(){
$time = explode (" ", microtime () );
$time = $time [1] . ($time [0] * 1000);
$time2 = explode ( ".", $time );
$time = $time2 [0];
return $time;
}

public function getConfig($key)
{
return $this->config->$key;
}

public function toUrlEncodedRequestBody($bizParams)
{
$sortedMap = $this->getSortedMap(null, $bizParams, null);
if (empty($sortedMap)) {
return null;
}
return $this->buildQueryString($sortedMap);
}
public function readAsJson($response){
$responseBody = (string)$response->getBody();
return $responseBody ;
}

public function toRespModel($respMap){
$code = $respMap[ElemeConstants::CODE];
$msg = $respMap[ElemeConstants::MSG];
if ($code == '200'){
$data = $respMap[ElemeConstants::BIZ_CONTENT_FIELD];
$model = json_decode($data, true);
return $model;
}
throw new Exception("接口访问异常,code:".$code.",msg:".$msg);
}

private function buildQueryString(array $sortedMap)
{
$requestUrl = null;
foreach ($sortedMap as $sysParamKey => $sysParamValue) {
$requestUrl .= "$sysParamKey=" . urlencode($this->characet($sysParamValue, ElemeConstants::DEFAULT_CHARSET)) . "&";
}
$requestUrl = substr($requestUrl, 0, -1);
return $requestUrl;

}

private function getSortedMap($systemParams, $bizParams, $textParams)
{
$this->textParams = $textParams;
$this->bizParams = $bizParams;
if ($textParams != null && $this->optionalTextParams != null) {
$this->textParams = array_merge($textParams, $this->optionalTextParams);
} else if ($textParams == null) {
$this->textParams = $this->optionalTextParams;
}
if ($bizParams != null && $this->optionalBizParams != null) {
$this->bizParams = array_merge($bizParams, $this->optionalBizParams);
} else if ($bizParams == null) {
$this->bizParams = $this->optionalBizParams;
}
$json = new JsonUtil();
if ($this->bizParams != null) {
$bizParams = $json->toJsonString($this->bizParams);
}
$sortedMap = $systemParams;
if (!empty($bizParams)) {
$sortedMap[ElemeConstants::BIZ_CONTENT_FIELD] = json_encode($bizParams, JSON_UNESCAPED_UNICODE);
}
if (!empty($this->textParams)) {
if (!empty($sortedMap)) {
$sortedMap = array_merge($sortedMap, $this->textParams);
} else {
$sortedMap = $this->textParams;
}
}
return $sortedMap;
}

public function sign($systemParams, $bizParams, $textParams, $privateKey)
{
$sortedMap = $this->getSortedMap($systemParams, $bizParams, $textParams);
$requestUrl = $this->buildQueryString($sortedMap);
$encodeStr = $privateKey.$requestUrl;
return hash(sha256,$encodeStr);
}


}
14 changes: 14 additions & 0 deletions kernel/php/kernel/src/ElemeConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace EleMe\OpenApi\Kernel;


class ElemeConstants
{
const BIZ_CONTENT_FIELD = "business_data";
const CODE = "code";
const MSG = "msg";
const DEFAULT_CHARSET = 'UTF-8';

}
56 changes: 56 additions & 0 deletions kernel/php/kernel/src/Util/JsonUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php


namespace EleMe\OpenApi\Kernel\Util;


use AlibabaCloud\Tea\Model;

class JsonUtil
{
public function toJsonString(array $input)
{
$result = [];
foreach ($input as $k => $v) {
if ($v instanceof Model) {
$result[$k] = $this->getTeaModelMap($v);
} else {
$result[$k] = $v;
}
}
return $result;
}

private function getTeaModelMap(Model $teaModel)
{
$result = [];
foreach ($teaModel as $k => $v) {
if ($v instanceof Model) {
$k = $this->toUnderScore($k);
$result[$k] = $this->getTeaModelMap($v);
} else {
if (empty($result)) {
$k = $this->toUnderScore($k);
$result[$k] = $v;
} else {
$k = $this->toUnderScore($k);
$result[$k] = $v;
}
}
}
return $result;
}

/**
* 驼峰命名转下划线命名
* @param $str
* @return string
*/
private function toUnderScore($str)
{
$dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
return '_' . strtolower($matchs[0]);
}, $str);
return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
}
}
13 changes: 13 additions & 0 deletions kernel/php/src/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace EleMe\OpenApi\Kernel;

use PHPUnit\Framework\TestCase;
final class ClientTest extends TestCase{

public function testA()
{
$a = 'a1111';
echo $a;
self::assertEquals($a,'a1111');
}
}
18 changes: 18 additions & 0 deletions kernel/php/src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


namespace EleMe\OpenApi\Kernel;
use AlibabaCloud\Tea\Model;

class Config extends Model
{
public $protocol;
public $gatewayHost;
public $appId;
public $accessToken;
public $merchantId;
public $secretKey;
public $httpProxy;
public $ignoreSSL;

}
Loading

0 comments on commit 9dea228

Please sign in to comment.