Skip to content

Commit

Permalink
delete: 移除 Yansongda\Pay\Direction\ArrayDirection
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Jul 1, 2023
1 parent de827f7 commit a5de890
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 154 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v3.5.0

### deleted

- deleted: 移除 `Yansongda\Pay\Direction\ArrayDirection` 类(#818)

## v3.4.2

### changed
Expand Down
32 changes: 0 additions & 32 deletions src/Direction/ArrayDirection.php

This file was deleted.

22 changes: 14 additions & 8 deletions src/Direction/CollectionDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@
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]);
}
}
9 changes: 1 addition & 8 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;
Expand Down Expand Up @@ -77,13 +76,7 @@ public function pay(array $plugins, array $params): Collection|MessageInterface|

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();
}

/**
Expand Down
81 changes: 0 additions & 81 deletions tests/Direction/ArrayDirectionTest.php

This file was deleted.

21 changes: 1 addition & 20 deletions tests/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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;
Expand Down Expand Up @@ -121,23 +120,6 @@ public function testIgniteWrongHttpClient()
$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();
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 2 additions & 5 deletions web/docs/v3/quick-start/return-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

是不是很简单方便?

0 comments on commit a5de890

Please sign in to comment.