Skip to content

Commit

Permalink
🐛 修复某些参数无法设置的问题,fixed #23,#24
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbotton committed Sep 17, 2021
1 parent f28bf6f commit 3125d24
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 103 deletions.
12 changes: 0 additions & 12 deletions aop/AlipayAccessorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,21 @@

namespace Alipay;

use Alipay\Exception\AlipayInvalidPropertyException;

trait AlipayAccessorTrait
{
public function __get($name)
{
$getter = 'get'.$name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set'.$name)) {
throw new AlipayInvalidPropertyException('Getting write-only property', $name);
}

throw new AlipayInvalidPropertyException('Getting unknown property', $name);
}

public function __set($name, $value)
{
$setter = 'set'.$name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get'.$name)) {
throw new AlipayInvalidPropertyException('Setting read-only property', $name);
} else {
throw new AlipayInvalidPropertyException('Setting unknown property', $name);
}
}

Expand All @@ -45,8 +35,6 @@ public function __unset($name)
$setter = 'set'.$name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get'.$name)) {
throw new AlipayInvalidPropertyException('Unsetting read-only property', $name);
}
}
}
17 changes: 4 additions & 13 deletions aop/AlipayRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Alipay;

use Alipay\Exception\AlipayInvalidPropertyException;
use Alipay\Exception\AlipayInvalidRequestException;
use Alipay\Request\AlipayRequest;

class AlipayRequestFactory
Expand All @@ -25,24 +23,17 @@ public static function create($apiName, $config = [])
* 通过 `API 名称` 创建请求类实例.
*
* @param $apiName
* @param array $config
*
* @throws AlipayInvalidRequestException
*
* @param array $config
* @return AlipayRequest
*/
private function createByApi($apiName, $config = [])
{
$config = array_merge($config, ['api_method_name' => $apiName]);
$request = new AlipayRequest();

try {
foreach ($config as $key => $value) {
$property = AlipayHelper::studlyCase($key, '_');
$request->$property = $value;
}
} catch (AlipayInvalidPropertyException $ex) {
throw new AlipayInvalidRequestException($ex->getMessage().': '.$key);
foreach ($config as $key => $value) {
$property = AlipayHelper::studlyCase($key, '_');
$request->$property = $value;
}

return $request;
Expand Down
6 changes: 1 addition & 5 deletions aop/AlipayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ public function getError($assoc = true)
if ($this->isSuccess()) {
return null;
}
if (isset($this->parsed[static::ERROR_NODE])) {
$result = $this->parsed[static::ERROR_NODE];
} else {
$result = $this->getFirstElement();
}
$result = $this->parsed[static::ERROR_NODE] ?? $this->getFirstElement();
if ($assoc == false) {
$result = (object) ($result);
}
Expand Down
23 changes: 0 additions & 23 deletions aop/Exception/AlipayInvalidPropertyException.php

This file was deleted.

10 changes: 0 additions & 10 deletions aop/Exception/AlipayInvalidRequestException.php

This file was deleted.

18 changes: 0 additions & 18 deletions tests/default/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,4 @@ public function testCreateByApi()
$ins = AlipayRequestFactory::create($this->apiName);
$this->assertInstanceOf(AlipayRequest::className(), $ins);
}

public function testInvalidConfig()
{
$this->setExpectedException('Alipay\Exception\AlipayInvalidRequestException');

AlipayRequestFactory::create('foo.bar', [
'foo' => 'this config does not exist',
]);
}

public function testUnwritableConfig()
{
$this->setExpectedException('Alipay\Exception\AlipayInvalidRequestException');

AlipayRequestFactory::create($this->apiName, [
'foo' => 'bar',
]);
}
}
22 changes: 0 additions & 22 deletions tests/default/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ public function testGetterSetter()
$this->assertEquals($url, $ins->getNotifyUrl());
}

public function testSetUnknownProperty()
{
$this->setExpectedException('Alipay\Exception\AlipayInvalidPropertyException');

$ins = AlipayRequestFactory::create($this->apiName);
$ins->foo;
}

public function testGetUnknownProperty()
{
$this->setExpectedException('Alipay\Exception\AlipayInvalidPropertyException');
$ins = AlipayRequestFactory::create($this->apiName);

try {
$ins->foo;
} catch (Alipay\Exception\AlipayInvalidPropertyException $ex) {
$this->assertEquals('foo', $ex->getProperty());

throw $ex;
}
}

public function testTimestamp()
{
$ins = AlipayRequestFactory::create($this->apiName);
Expand Down

0 comments on commit 3125d24

Please sign in to comment.