Skip to content

Commit

Permalink
Fixed response formatting feature. #661
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Oct 22, 2017
1 parent 873ba27 commit 664c4a1
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 117 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<exclude>
<directory suffix="ServiceProvider.php">src/</directory>
<directory suffix="Exception.php">src/</directory>
<directory suffix="Helpers.php">src/</directory>
<directory suffix="Helpers.php">src/Kernel/Support</directory>
<directory>src/Encryption</directory>
<directory>src/Support</directory>
</exclude>
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function request(string $url, string $method = 'GET', array $options = []

$response = $this->performRequest($url, $method, $options);

return $returnRaw ? $response : $this->resolveResponse($response, $this->app->config->get('response_type', 'array'));
return $returnRaw ? $response : $this->resolveResponse($response, $this->app->config->get('response_type'));
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/Kernel/Contracts/Arrayable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Kernel\Contracts;

use ArrayAccess;

/**
* Interface Arrayable.
*
* @author overtrue <[email protected]>
*/
interface Arrayable extends ArrayAccess
{
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray();
}
43 changes: 43 additions & 0 deletions src/Kernel/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,46 @@
*/

namespace EasyWeChat\Kernel;

use EasyWeChat\Kernel\Contracts\Arrayable;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Support\Arr;
use EasyWeChat\Kernel\Support\Collection;

function data_get($data, $key, $default = null)
{
switch (true) {
case is_array($data):
return Arr::get($data, $key, $default);
case $data instanceof Collection:
return $data->get($key, $default);
case $data instanceof Arrayable:
return Arr::get($data->toArray(), $key, $default);
case $data instanceof \ArrayIterator:
return $data->getArrayCopy()[$key] ?? $default;
case $data instanceof \ArrayAccess:
return $data[$key] ?? $default;
case $data instanceof \IteratorAggregate:
return $data->getIterator()->getArrayCopy()[$key] ?? $default;
default:
throw new RuntimeException(sprintf('Can\'t access data with key "%s"', $key));
}
}

function data_to_array($data)
{
switch (true) {
case is_array($data):
return $data;
case $data instanceof Collection:
return $data->all();
case $data instanceof Arrayable:
return $data->toArray();
case $data instanceof \IteratorAggregate:
return $data->getIterator()->getArrayCopy();
case $data instanceof \ArrayIterator:
return $data->getArrayCopy();
default:
throw new RuntimeException(sprintf('Can\'t transform data to array'));
}
}
66 changes: 66 additions & 0 deletions src/Kernel/Support/ArrayAccessible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Kernel\Support;

use ArrayAccess;
use ArrayIterator;
use EasyWeChat\Kernel\Contracts\Arrayable;
use IteratorAggregate;

/**
* Class ArrayAccessible.
*
* @author overtrue <[email protected]>
*/
class ArrayAccessible implements ArrayAccess, IteratorAggregate, Arrayable
{
private $array;

public function __construct(array $array = [])
{
$this->array = $array;
}

public function offsetExists($offset)
{
return array_key_exists($offset, $this->array);
}

public function offsetGet($offset)
{
return $this->array[$offset];
}

public function offsetSet($offset, $value)
{
if (null === $offset) {
$this->array[] = $value;
} else {
$this->array[$offset] = $value;
}
}

public function offsetUnset($offset)
{
unset($this->array[$offset]);
}

public function getIterator()
{
return new ArrayIterator($this->array);
}

public function toArray()
{
return $this->array;
}
}
3 changes: 2 additions & 1 deletion src/Kernel/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
use ArrayAccess;
use ArrayIterator;
use Countable;
use EasyWeChat\Kernel\Contracts\Arrayable;
use IteratorAggregate;
use JsonSerializable;
use Serializable;

/**
* Class Collection.
*/
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable, Arrayable
{
/**
* The collection data.
Expand Down
22 changes: 0 additions & 22 deletions src/Kernel/Support/LICENSE

This file was deleted.

2 changes: 0 additions & 2 deletions src/Kernel/Support/README.md

This file was deleted.

26 changes: 0 additions & 26 deletions src/Kernel/Support/composer.json

This file was deleted.

21 changes: 13 additions & 8 deletions src/Kernel/Traits/HasHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace EasyWeChat\Kernel\Traits;

use EasyWeChat\Kernel\Contracts\Arrayable;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\Kernel\Http\Response;
use EasyWeChat\Kernel\Support\Collection;
use GuzzleHttp\Client;
Expand Down Expand Up @@ -187,29 +189,32 @@ public function getHandlerStack(): HandlerStack

/**
* @param \Psr\Http\Message\ResponseInterface $response
* @param string $type
* @param string|null $type
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
protected function resolveResponse(ResponseInterface $response, string $type)
protected function resolveResponse(ResponseInterface $response, $type = null)
{
$response = Response::buildFromPsrResponse($response);
$response->getBody()->rewind();

switch ($type) {
switch ($type ?? 'array') {
case 'collection':
return $response->toCollection();
case 'array':
return $response->toArray();
case 'object':
return $response->toObject();
case 'raw':
return $response;
default:
$response->getBody()->rewind();
if (class_exists($type)) {
return new $type($response);
if (!is_subclass_of($type, Arrayable::class)) {
throw new InvalidConfigException(sprintf('Config key "response_type" classname must be an instanceof %s', Arrayable::class));
}

return $response;
return new $type($response);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/OfficialAccount/Material/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function get(string $mediaId)
return StreamResponse::buildFromPsrResponse($response);
}

return $this->resolveResponse($response, $this->app['config']->get('response_type', 'array'));
return $this->resolveResponse($response, $this->app['config']->get('response_type'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/OpenPlatform/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function getAuthorizerConfig(string $appId, string $refreshToken): arr
{
return [
'debug' => $this['config']->get('debug', false),
'response_type' => $this['config']->get('response_type', 'array'),
'response_type' => $this['config']->get('response_type'),
'log' => $this['config']->get('log', []),
'app_id' => $appId,
'refresh_token' => $refreshToken,
Expand Down
2 changes: 1 addition & 1 deletion src/Payment/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function request($api, array $params, $method = 'post', array $options

$response = $this->performRequest($api, $method, $options);

return $returnResponse ? $response : $this->resolveResponse($response, $this->app->config->get('response_type', 'array'));
return $returnResponse ? $response : $this->resolveResponse($response, $this->app->config->get('response_type'));
}

/**
Expand Down
22 changes: 0 additions & 22 deletions src/Payment/LICENSE

This file was deleted.

2 changes: 0 additions & 2 deletions src/Payment/README.md

This file was deleted.

25 changes: 0 additions & 25 deletions src/Payment/composer.json

This file was deleted.

Loading

0 comments on commit 664c4a1

Please sign in to comment.