• 5
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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) {
  }
}
      • 1
    • I know nothing about that library but, unless the class is final, you should be able to extend it and write your own getter.
      • 1
    • @ÁlvaroG.Vicario The API will return that class from Plan::all() there is no way override that behaviour easily...

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.

  • 15
Reply Report

To add to Victor Stanciu's answer, if you want to access only certain elements of _values array, you can use dynamic properties.

For example, if you want to access data, you can do it this way:

$collection = \Stripe\Plan::all();
$plans = $collection->data;

And then, analogously, you can retrieve any array element from each plan:

foreach ($plans as $plan) {
    print $plan->name;
}

I use this code for Stripe API version 3.

  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions