使用PHP,我如何访问Stripe API返回的protected _values属性?

Cur*_*ell 9 php stripe-payments

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

我一直在使用Reflection类来获取数据,但是现在我正在使用Stripe \Stripe\Plan::all();,我必须调用我编写的快捷方法来多次处理Reflection类.但它并不完全是递归的,因为我必须根据我从Stripe API调用的方法来区别对待它.

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


这是一个示例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) {
  }
}
Run Code Online (Sandbox Code Playgroud)

Vic*_*ciu 15

您不必使用Reflection API,Stripe\Collection该类实现ArrayAccess,您可以直接迭代它:

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

Collection类扩展的基类.对于Stripe的PHP库中的几乎所有类都是如此,包括Stripe\Plan.因此,您可以使用任何类型的递归,您将使用普通数组.

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

$array = $collection->__toArray(true);
Run Code Online (Sandbox Code Playgroud)

true参数是一个递归选项.