使用 PHP,如何访问从 Stripe API 返回的受保护的_values 属性?
Using PHP, how can I access the protected _values property returned from the Stripe API?
我正在将 Stripe API 与 CMS 集成。我需要 return 来自查询的 _values
属性 作为数组,以便数据在 CMS 中作为模板变量可用,但它始终受到保护。
我一直在使用Reflection class to get at the data, but now that I'm using Stripe's \Stripe\Plan::all();
,我必须多次调用我编写的快捷方法来处理反射class。但它并不完全是递归的,因为我必须根据我从 Stripe API.
调用的方法来不同地处理它
有没有办法真正递归地使用反射 class?有什么比我只是不知道的反射 class 更合适的东西吗?
这是 var_dump()
ed 对 \Stripe\Plan::all();
的响应示例:
object(Stripe\Collection)#604 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(4) {
["object"]=>
string(4) "list"
["has_more"]=>
bool(false)
["url"]=>
string(9) "/v1/plans"
["data"]=>
array(2) {
[0]=>
object(Stripe\Plan)#605 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(8) "my_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(9) "My Plan"
["created"]=>
int(1427441577)
["amount"]=>
int(20000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#608 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#612 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#613 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#609 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#610 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
[1]=>
object(Stripe\Plan)#611 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(9) "some_other_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(14) "Some Other Plan"
["created"]=>
int(1427431129)
["amount"]=>
int(40000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#614 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#618 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#619 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#615 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#616 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
}
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#606 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#607 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
你不必使用反射API,Stripe\Collection
class实现ArrayAccess,你可以直接遍历它:
$collection = \Stripe\Plan::all();
foreach ($collection as $plan) {
// Do something with the plan
}
Here 是基础 class Collection
class 的扩展。 Stripe 的 PHP 库中的几乎所有 classes 都是如此,包括 Stripe\Plan
。因此,您可以使用与普通数组一起使用的任何类型的递归。
如果想return把_values
属性当成数组,可以使用__toArray()
方法:
$array = $collection->__toArray(true);
true
参数是一个递归选项。
添加到 Victor Stanciu 的回答中,如果您只想访问 _values
数组的某些元素,您可以使用动态属性。
比如你想访问数据,你可以这样做:
$collection = \Stripe\Plan::all();
$plans = $collection->data;
然后,类似地,您可以从每个计划中检索任何数组元素:
foreach ($plans as $plan) {
print $plan->name;
}
我将此代码用于 Stripe API 版本 3。
我正在将 Stripe API 与 CMS 集成。我需要 return 来自查询的 _values
属性 作为数组,以便数据在 CMS 中作为模板变量可用,但它始终受到保护。
我一直在使用Reflection class to get at the data, but now that I'm using Stripe's \Stripe\Plan::all();
,我必须多次调用我编写的快捷方法来处理反射class。但它并不完全是递归的,因为我必须根据我从 Stripe API.
有没有办法真正递归地使用反射 class?有什么比我只是不知道的反射 class 更合适的东西吗?
这是 var_dump()
ed 对 \Stripe\Plan::all();
的响应示例:
object(Stripe\Collection)#604 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(4) {
["object"]=>
string(4) "list"
["has_more"]=>
bool(false)
["url"]=>
string(9) "/v1/plans"
["data"]=>
array(2) {
[0]=>
object(Stripe\Plan)#605 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(8) "my_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(9) "My Plan"
["created"]=>
int(1427441577)
["amount"]=>
int(20000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#608 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#612 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#613 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#609 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#610 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
[1]=>
object(Stripe\Plan)#611 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(9) "some_other_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(14) "Some Other Plan"
["created"]=>
int(1427431129)
["amount"]=>
int(40000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#614 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#618 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#619 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#615 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#616 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
}
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#606 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#607 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
你不必使用反射API,Stripe\Collection
class实现ArrayAccess,你可以直接遍历它:
$collection = \Stripe\Plan::all();
foreach ($collection as $plan) {
// Do something with the plan
}
Here 是基础 class Collection
class 的扩展。 Stripe 的 PHP 库中的几乎所有 classes 都是如此,包括 Stripe\Plan
。因此,您可以使用与普通数组一起使用的任何类型的递归。
如果想return把_values
属性当成数组,可以使用__toArray()
方法:
$array = $collection->__toArray(true);
true
参数是一个递归选项。
添加到 Victor Stanciu 的回答中,如果您只想访问 _values
数组的某些元素,您可以使用动态属性。
比如你想访问数据,你可以这样做:
$collection = \Stripe\Plan::all();
$plans = $collection->data;
然后,类似地,您可以从每个计划中检索任何数组元素:
foreach ($plans as $plan) {
print $plan->name;
}
我将此代码用于 Stripe API 版本 3。