diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f3fc79..67a38e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v3.5.0 + +### deleted + +- deleted: 移除 `Yansongda\Pay\Direction\ArrayDirection` 类(#818) + ## v3.4.2 ### changed diff --git a/src/Direction/ArrayDirection.php b/src/Direction/ArrayDirection.php deleted file mode 100644 index 2bfe1a2..0000000 --- a/src/Direction/ArrayDirection.php +++ /dev/null @@ -1,32 +0,0 @@ -getBody(); - - if (!is_null($result = $packer->unpack($body))) { - return $result; - } - - throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]); - } -} diff --git a/src/Direction/CollectionDirection.php b/src/Direction/CollectionDirection.php index 21562db..9e417ce 100644 --- a/src/Direction/CollectionDirection.php +++ b/src/Direction/CollectionDirection.php @@ -7,21 +7,27 @@ namespace Yansongda\Pay\Direction; use Psr\Http\Message\ResponseInterface; use Yansongda\Pay\Contract\DirectionInterface; use Yansongda\Pay\Contract\PackerInterface; -use Yansongda\Pay\Exception\ContainerException; -use Yansongda\Pay\Exception\ServiceNotFoundException; -use Yansongda\Pay\Pay; +use Yansongda\Pay\Exception\Exception; +use Yansongda\Pay\Exception\InvalidResponseException; use Yansongda\Supports\Collection; class CollectionDirection implements DirectionInterface { /** - * @throws ContainerException - * @throws ServiceNotFoundException + * @throws InvalidResponseException */ public function parse(PackerInterface $packer, ?ResponseInterface $response): Collection { - return new Collection( - Pay::get(ArrayDirection::class)->parse($packer, $response) - ); + if (is_null($response)) { + throw new InvalidResponseException(Exception::RESPONSE_NONE); + } + + $body = (string) $response->getBody(); + + if (!is_null($result = $packer->unpack($body))) { + return new Collection($result); + } + + throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]); } } diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 7867c01..f7e0880 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -12,7 +12,6 @@ use Yansongda\Pay\Contract\HttpClientInterface; use Yansongda\Pay\Contract\PluginInterface; use Yansongda\Pay\Contract\ProviderInterface; use Yansongda\Pay\Contract\ShortcutInterface; -use Yansongda\Pay\Direction\ArrayDirection; use Yansongda\Pay\Event; use Yansongda\Pay\Exception\ContainerException; use Yansongda\Pay\Exception\Exception; @@ -77,13 +76,7 @@ abstract class AbstractProvider implements ProviderInterface Event::dispatch(new Event\PayFinish($rocket)); - $destination = $rocket->getDestination(); - - if (ArrayDirection::class === $rocket->getDirection() && $destination instanceof Collection) { - return $destination->toArray(); - } - - return $destination; + return $rocket->getDestination(); } /** diff --git a/tests/Direction/ArrayDirectionTest.php b/tests/Direction/ArrayDirectionTest.php deleted file mode 100644 index 37a7909..0000000 --- a/tests/Direction/ArrayDirectionTest.php +++ /dev/null @@ -1,81 +0,0 @@ -parser = new ArrayDirection(); - } - - public function testResponseNull() - { - self::expectException(InvalidResponseException::class); - self::expectExceptionCode(Exception::RESPONSE_NONE); - - $this->parser->parse(new JsonPacker(), null); - } - - public function testWrongFormat() - { - self::expectException(InvalidResponseException::class); - self::expectExceptionCode(Exception::UNPACK_RESPONSE_ERROR); - - $response = new Response(200, [], '{"name": "yansongda"}a'); - - $this->parser->parse(new JsonPacker(), $response); - } - - public function testNormal() - { - $response = new Response(200, [], '{"name": "yansongda"}'); - - $result = $this->parser->parse(new JsonPacker(), $response); - - self::assertEquals(['name' => 'yansongda'], $result); - } - - public function testReadContents() - { - $response = new Response(200, [], '{"name": "yansongda"}'); - - $response->getBody()->read(2); - - $result = $this->parser->parse(new JsonPacker(), $response); - - self::assertEquals(['name' => 'yansongda'], $result); - } - - public function testQueryBody() - { - $response = new Response(200, [], 'name=yansongda&age=29'); - - $result = $this->parser->parse(new QueryPacker(), $response); - - self::assertEqualsCanonicalizing(['name' => 'yansongda', 'age' => '29'], $result); - } - - public function testJsonWith() - { - $url = 'https://yansongda.cn?name=yansongda&age=29'; - - $response = new Response(200, [], json_encode(['h5_url' => $url])); - - $result = $this->parser->parse(new JsonPacker(), $response); - - self::assertEquals('https://yansongda.cn?name=yansongda&age=29', $result['h5_url']); - } -} diff --git a/tests/Provider/AbstractProviderTest.php b/tests/Provider/AbstractProviderTest.php index a3ea4d1..7c487b5 100644 --- a/tests/Provider/AbstractProviderTest.php +++ b/tests/Provider/AbstractProviderTest.php @@ -14,7 +14,6 @@ use Yansongda\Pay\Contract\HttpClientInterface; use Yansongda\Pay\Contract\PackerInterface; use Yansongda\Pay\Contract\PluginInterface; use Yansongda\Pay\Contract\ShortcutInterface; -use Yansongda\Pay\Direction\ArrayDirection; use Yansongda\Pay\Direction\CollectionDirection; use Yansongda\Pay\Direction\NoHttpRequestDirection; use Yansongda\Pay\Exception\Exception; @@ -121,23 +120,6 @@ class AbstractProviderTest extends TestCase $provider->ignite($rocket); } - public function testArrayDirection() - { - $response = new Response(200, [], '{"name":"yansongda"}'); - - $http = Mockery::mock(Client::class); - $http->shouldReceive('sendRequest')->andReturn($response); - - Pay::set(HttpClientInterface::class, $http); - - $plugin = [BarPlugin::class]; - - $provider = new FooProviderStub(); - $result = $provider->pay($plugin, []); - - self::assertIsArray($result); - } - public function testNoCommonPlugins() { $provider = new Foo2ProviderStub(); @@ -212,8 +194,7 @@ class BarPlugin implements PluginInterface { public function assembly(Rocket $rocket, Closure $next): Rocket { - $rocket->setDirection(ArrayDirection::class) - ->setRadar(new Request('get', '')); + $rocket->setRadar(new Request('get', '')); $rocket = $next($rocket); diff --git a/web/docs/v3/quick-start/return-format.md b/web/docs/v3/quick-start/return-format.md index 35416fd..2ca2db9 100644 --- a/web/docs/v3/quick-start/return-format.md +++ b/web/docs/v3/quick-start/return-format.md @@ -48,15 +48,12 @@ ThinkPHP 框架在 [https://github.com/top-think/framework/pull/2614](https://gi ## array -API 调用场景下的返回类型,`array` 和 `Collection` 是可以自定义的,默认情况下均返回 `Collection` 实例。 +API 调用场景下的返回类型,默认情况下均返回 `Collection` 实例。 如果想返回 array 类型的数据,只需要 ```php -use Yansongda\Pay\Contract\DirectionInterface; -use Yansongda\Pay\Direction\ArrayDirection; - -Pay::set(DirectionInterface::class, ArrayDirection::class); +$collection->toArray(); ``` 是不是很简单方便?