Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP, how can I access the protected _values property returned from the Stripe API?

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.

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.

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?


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) {
  }
}
like image 498
Curtis Blackwell Avatar asked Apr 07 '15 14:04

Curtis Blackwell


1 Answers

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
}

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.

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

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

The true argument is a recursion option.

like image 57
Victor Stanciu Avatar answered Oct 14 '22 00:10

Victor Stanciu