Class Api 包装器的组织
Class Organisation of Api Wrappers
我正在尝试学习为第 3 方 api 创建 php 包装器。但是,我对彼此实现多个 classes extended/implemented 感到困惑。
class Api {
protected $username;
protected $password;
protected $wsdl = "http://example.com"
protected $client;
protected $account;
public function __construct($username, $password)
{
$this->client = new SoapClient($this->wsdl);
$authData = [
"Credentials" => [
"Username" => $username,
"Password" => $password
]
];
$this->makeCall('AuthenticateUser', $authData);
$this->account = $this->makeCall('GetAccountInfo', ["Authenticator" => $this->authenticator]);
}
protected function makeCall($method, $data) {
$result = $this->client->$method($data);
$this->authenticator = $result->Authenticator;
return $result;
}
}
说到这里,才有意义。但是,此时我不想在这个 class 中添加所有方法。因此,我决定为每个方法创建一个单独的 class。那里,问题开始了。
class AddressValidator extends Api
{
public function validateAddress($data) {
$response = $this->makeCall('validateAddress', $data);
dd($response);
}
}
从逻辑上讲,我需要如何调用包装器(在我的控制器中)如下所示,对吗?
$api = new Api($username, $password);
$api->validateAddress($params); // but I can't call this line with this setup
相反,这有效:
$api = new ValidateAddress($username, $password);
$api->validateAddress($params);
有道理,但这是组织它的好方法吗?
设置api包装器的美妙方法是什么?顺便说一句,也许我对这种方法完全错了。我很乐意听听你的想法
您可以使用特征来组织您的方法,而不是扩展您的 API class。
class Api {
use ValidateAddressTrait;
...
}
特质:
trait ValidateAddressTrait {
public function validateAddress($data) {
$response = $this->makeCall('validateAddress', $data);
dd($response);
}
}
使用:
$api = new Api($username, $password);
$api->validateAddress($params);
这不完全是特征的意图,但我认为它可以为您提供您正在寻找的结果。
也许像
class Api {
private $class;
.
.
public function __construct($username, $password, $class_name) {
.
.
$this->class = new $class_name();
}
public function ApiCall($func, ...$arguments) {
$this->class->$func($arguments);
}
}
我不确定这是否让你的事情变得更容易,但它确实有效。
我正在尝试学习为第 3 方 api 创建 php 包装器。但是,我对彼此实现多个 classes extended/implemented 感到困惑。
class Api {
protected $username;
protected $password;
protected $wsdl = "http://example.com"
protected $client;
protected $account;
public function __construct($username, $password)
{
$this->client = new SoapClient($this->wsdl);
$authData = [
"Credentials" => [
"Username" => $username,
"Password" => $password
]
];
$this->makeCall('AuthenticateUser', $authData);
$this->account = $this->makeCall('GetAccountInfo', ["Authenticator" => $this->authenticator]);
}
protected function makeCall($method, $data) {
$result = $this->client->$method($data);
$this->authenticator = $result->Authenticator;
return $result;
}
}
说到这里,才有意义。但是,此时我不想在这个 class 中添加所有方法。因此,我决定为每个方法创建一个单独的 class。那里,问题开始了。
class AddressValidator extends Api
{
public function validateAddress($data) {
$response = $this->makeCall('validateAddress', $data);
dd($response);
}
}
从逻辑上讲,我需要如何调用包装器(在我的控制器中)如下所示,对吗?
$api = new Api($username, $password);
$api->validateAddress($params); // but I can't call this line with this setup
相反,这有效:
$api = new ValidateAddress($username, $password);
$api->validateAddress($params);
有道理,但这是组织它的好方法吗?
设置api包装器的美妙方法是什么?顺便说一句,也许我对这种方法完全错了。我很乐意听听你的想法
您可以使用特征来组织您的方法,而不是扩展您的 API class。
class Api {
use ValidateAddressTrait;
...
}
特质:
trait ValidateAddressTrait {
public function validateAddress($data) {
$response = $this->makeCall('validateAddress', $data);
dd($response);
}
}
使用:
$api = new Api($username, $password);
$api->validateAddress($params);
这不完全是特征的意图,但我认为它可以为您提供您正在寻找的结果。
也许像
class Api {
private $class;
.
.
public function __construct($username, $password, $class_name) {
.
.
$this->class = new $class_name();
}
public function ApiCall($func, ...$arguments) {
$this->class->$func($arguments);
}
}
我不确定这是否让你的事情变得更容易,但它确实有效。