使用 PHP,如何访问从 Stripe API 返回的受保护的 _values 属性? [英] Using PHP, how can I access the protected _values property returned from the Stripe API?

查看:25
本文介绍了使用 PHP,如何访问从 Stripe API 返回的受保护的 _values 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Stripe API 与 CMS 集成.我需要将查询中的 _values 属性作为数组返回,以便数据可用作 CMS 中的模板变量,但它始终受到保护.

I'm integrating the Stripe API with a CMS. I need to return the _values property from queries as an array so that the data is available as template variables in the CMS, but it's always protected.

我一直在使用 反射类来获取数据,但现在我正在使用 Stripe 的 \Stripe\Plan::all();,我不得不多次调用我写的快捷方法来处理反射类.不过,它并不完全是递归的,因为根据我从 Stripe API 调用的方法,我必须以不同的方式处理它.

I've been using a Reflection class to get at the data, but now that I'm using Stripe's \Stripe\Plan::all();, I have to call the shortcut method I wrote to handle the Reflection class multiple times. It's not exactly recursive though, because I'd have to handle it differently depending on which method I call from the Stripe API.

有没有办法真正递归地使用反射类?有没有比我不知道的 Reflection 类更合适的东西?

Is there a way to use a Reflection class truly recursively? Is there something more fitting than a Reflection class that I'm just not aware of?

这是 \Stripe\Plan::all();var_dump()ed 响应示例:

Here's a sample var_dump()ed response of \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 类实现了 ArrayAccess,可以直接遍历:

You don't have to use the Reflection API, the Stripe\Collection class implements ArrayAccess, you can iterate through it directly:

$collection = \Stripe\Plan::all();
foreach ($collection as $plan) {
    // Do something with the plan
}

这里Collection 类扩展.对于 Stripe 的 PHP 库中的几乎所有类都是如此,包括 Stripe\Plan.所以你可以使用任何类型的递归来处理普通数组.

Here is the base class the Collection class extends. This is true for almost all the classes in Stripe's PHP library, including Stripe\Plan. So you can use any kind of recursion you would use with normal arrays.

如果要将 _values 属性作为数组返回,可以使用 __toArray() 方法:

If you want to return the _values property as an array, you can use the __toArray() method:

$array = $collection->__toArray(true);

true 参数是一个递归选项.

The true argument is a recursion option.

这篇关于使用 PHP,如何访问从 Stripe API 返回的受保护的 _values 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆