Skip to content

Commit

Permalink
feat: unpack 支持参数
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Jan 11, 2024
1 parent cdcfa4f commit c16766a
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v1.0.7

### added

- feat: `unpack` 支持参数(!8)

## v1.0.6

### chore
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/DirectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface DirectionInterface
{
public function guide(PackerInterface $packer, ?ResponseInterface $response): mixed;
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): mixed;
}
4 changes: 2 additions & 2 deletions src/Direction/CollectionDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class CollectionDirection implements DirectionInterface
/**
* @throws InvalidResponseException
*/
public function guide(PackerInterface $packer, ?ResponseInterface $response): Collection
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): Collection
{
if (is_null($response)) {
throw new InvalidResponseException(Exception::RESPONSE_EMPTY, '响应异常: 响应为空,不能进行 direction');
}

$body = (string) $response->getBody();

if (!is_null($result = $packer->unpack($body))) {
if (!is_null($result = $packer->unpack($body, $params))) {
return new Collection($result);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Direction/NoHttpRequestDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class NoHttpRequestDirection implements DirectionInterface
{
public function guide(PackerInterface $packer, ?ResponseInterface $response): ?ResponseInterface
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): ?ResponseInterface
{
return $response;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Direction/OriginResponseDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OriginResponseDirection implements DirectionInterface
/**
* @throws InvalidResponseException
*/
public function guide(PackerInterface $packer, ?ResponseInterface $response): ?ResponseInterface
public function guide(PackerInterface $packer, ?ResponseInterface $response, array $params = []): ?ResponseInterface
{
if (is_null($response)) {
throw new InvalidResponseException(Exception::RESPONSE_EMPTY, '响应异常: 响应为空,不能进行 direction');
Expand Down
2 changes: 1 addition & 1 deletion src/Packer/QueryPacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function pack(null|array|Collection $payload, ?array $params = null): str

public function unpack(string $payload, ?array $params = null): array
{
return Arr::wrapQuery($payload, !empty($params['_raw']));
return Arr::wrapQuery($payload, !empty($params['_unpack_raw']));
}
}
3 changes: 2 additions & 1 deletion src/Plugin/ParserPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
$response = $rocket->getDestination();
$direction = get_direction($rocket->getDirection());
$packer = get_packer($rocket->getPacker());
$params = $rocket->getParams();

if (!is_null($response) && !($response instanceof ResponseInterface)) {
throw new InvalidParamsException(Exception::PARAMS_PARSER_DIRECTION_INVALID, '参数异常: 解析插件中 `Rocket` 的 `destination` 只能是 `null` 或者 `ResponseInterface`');
}

$rocket->setDestination($direction->guide($packer, $response));
$rocket->setDestination($direction->guide($packer, $response, $params));

Logger::debug('[ParserPlugin] 插件装载完毕', ['rocket' => $rocket]);

Expand Down
11 changes: 11 additions & 0 deletions tests/Direction/CollectionDirectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ public function testJsonWith()

self::assertEquals('https://yansongda.cn?name=yansongda&age=29', $result['h5_url']);
}

public function testUnpackRaw()
{
$str = "accessType=0&bizType=000000&encoding=utf-8&merId=777290058167151&orderId=refundpay20240105165842&origQryId=052401051658427862748&queryId=052401051658427863998&respCode=00&respMsg=成功[0000000]&signMethod=01&txnAmt=1&txnSubType=00&txnTime=20240105165842&txnType=04&version=5.1.0&signPubKeyCert=-----BEGIN CERTIFICATE-----\r\nMIIEYzCCA0ugAwIBAgIFEDkwhTQw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NTAwMy5jcmwwCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBTmzk7XEM/J/sd+wPrMils3\r\n9rJ2/DAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwQwDQYJKoZIhvcNAQEF\r\nBQADggEBAJLbXxbJaFngROADdNmNUyVxPtbAvK32Ia0EjgDh/vjn1hpRNgvL4flH\r\nNsGNttCy8afLJcH8UnFJyGLas8v/P3UKXTJtgrOj1mtothv7CQa4LUYhzrVw3UhL\r\n4L1CTtmE6D1Kf3+c2Fj6TneK+MoK9AuckySjK5at6a2GQi18Y27gVF88Nk8bp1lJ\r\nvzOwPKd8R7iGFotuF4/8GGhBKR4k46EYnKCodyIhNpPdQfpaN5AKeS7xeLSbFvPJ\r\nHYrtBsI48jUK/WKtWBJWhFH+Gty+GWX0e5n2QHXHW6qH62M0lDo7OYeyBvG1mh9u\r\nQ0C300Eo+XOoO4M1WvsRBAF13g9RPSw=\r\n-----END CERTIFICATE-----&signature=c++EAuubwRkvr2MVyM9zyjbdH3RMRK/L1ttftpJ4fkl4ZSY1BjyRbTj5fx/2+Z/eH4dqPNfFEQt8egVVWhF/k7PaD8tLTaueeUIPwyjnEIWmqNtVbJtzKexCouGc8wtYDHZYxTJTgo6BW7GEgO5xD6Qpxq801Bb9Zto8uhn4BUP4HI7UsxHHIzP9JYhL2cqz2B8gb3AJHpLMEBpYv+Kb3mwq8ZFgpGaieCAFFGGWImUx1+MgCzLFoe3SKlTF13nbr39Cd3AHuDJnbN+uG1N6AwUtLu12Zzq/6SM+/dqiE0v5SpvB/PeRj9KQeiGDRg/ho9larqB+D3y0FjU13EeHng==";

$response = new Response(200, [], $str);

$result = $this->parser->guide(new QueryPacker(), $response, ['_unpack_raw' => 1]);

self::assertIsObject(openssl_pkey_get_public($result->get('signPubKeyCert')));
}
}
2 changes: 1 addition & 1 deletion tests/Packer/QueryPackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testUnpackRaw()
{
$str = "accessType=0&bizType=000000&encoding=utf-8&merId=777290058167151&orderId=refundpay20240105165842&origQryId=052401051658427862748&queryId=052401051658427863998&respCode=00&respMsg=成功[0000000]&signMethod=01&txnAmt=1&txnSubType=00&txnTime=20240105165842&txnType=04&version=5.1.0&signPubKeyCert=-----BEGIN CERTIFICATE-----\r\nMIIEYzCCA0ugAwIBAgIFEDkwhTQwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UEBhMC\r\nQ04xMDAuBgNVBAoTJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhv\r\ncml0eTEXMBUGA1UEAxMOQ0ZDQSBURVNUIE9DQTEwHhcNMjAwNzMxMDExOTE2WhcN\r\nMjUwNzMxMDExOTE2WjCBljELMAkGA1UEBhMCY24xEjAQBgNVBAoTCUNGQ0EgT0NB\r\nMTEWMBQGA1UECxMNTG9jYWwgUkEgT0NBMTEUMBIGA1UECxMLRW50ZXJwcmlzZXMx\r\nRTBDBgNVBAMMPDA0MUA4MzEwMDAwMDAwMDgzMDQwQOS4reWbvemTtuiBlOiCoeS7\r\nveaciemZkOWFrOWPuEAwMDAxNjQ5NTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC\r\nAQoCggEBAMHNa81t44KBfUWUgZhb1YTx3nO9DeagzBO5ZEE9UZkdK5+2IpuYi48w\r\neYisCaLpLuhrwTced19w2UR5hVrc29aa2TxMvQH9s74bsAy7mqUJX+mPd6KThmCr\r\nt5LriSQ7rDlD0MALq3yimLvkEdwYJnvyzA6CpHntP728HIGTXZH6zOL0OAvTnP8u\r\nRCHZ8sXJPFUkZcbG3oVpdXQTJVlISZUUUhsfSsNdvRDrcKYY+bDWTMEcG8ZuMZzL\r\ng0N+/spSwB8eWz+4P87nGFVlBMviBmJJX8u05oOXPyIcZu+CWybFQVcS2sMWDVZy\r\nsPeT3tPuBDbFWmKQYuu+gT83PM3G6zMCAwEAAaOB9DCB8TAfBgNVHSMEGDAWgBTP\r\ncJ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ho9larqB+D3y0FjU13EeHng==";

$wrapQuery = $this->packer->unpack($str, ['_raw' => 1]);
$wrapQuery = $this->packer->unpack($str, ['_unpack_raw' => 1]);

self::assertIsObject(openssl_pkey_get_public($wrapQuery['signPubKeyCert']));
}
Expand Down

0 comments on commit c16766a

Please sign in to comment.