0

I'm using Stripe's (relatively) new Connect Account feature. Stripe returns a list of "fields_needed" for validation, and I update the Account object based on those fields. Here is a very basic solution :

public function setStripeValue($field, $value, $account){
switch ($field) {
  case 'legal_entity.additional_owners.address.city':
      $account->legal_entity->additional_owners->address->city = $value;
      return $account;
    break;

  case 'legal_entity.additional_owners.address.country':
      $account->legal_entity->additional_owners->address->country = $value;
      return $account;
    break;

  case 'legal_entity.additional_owners.address.line1':
      $account->legal_entity->additional_owners->address->line1 = $value;
      return $account;
    break;

  case 'legal_entity.additional_owners.address.line2':
      $account->legal_entity->additional_owners->address->line2 = $value;
      return $account;
    break;

  // etc etc ... 50 fields or more! 

As you can see, the request is always in the format legal_entity.additional_owners.address.city or similar (can be 1 level or many levels deep) and the value definition is always in the format $account->legal_entity->additional_owners->address->city or similar.

Is there a better way to set these values recursively than a switch statement? There can be one or many fields to be set (I call this function multiple times) and the fields can be nested to any length. Thanks!

tried so far:

    function get($path, $object, $value) {
        $temp = &$object;
        foreach($path as $var) {
            $temp =& $temp->$var;
        }
        $temp = $value;
    }

    $key = explode(".", $field);
    get($key, $account, $value);

This seems to update the values within the object but doesn't play well with Stripes API, which must update using PHP object system (->) and then $account->save();

Grokify
  • 15,092
  • 6
  • 60
  • 81
Leon
  • 1,851
  • 3
  • 21
  • 44
  • 1
    explode you `$field` and then do a array reduce to access the `$account` with each value of the splitter array. – marciojc Sep 06 '18 at 15:24
  • @marciojc could you give me an example of what you mean? – Leon Sep 06 '18 at 15:25
  • 1
    @marciojc, not sure you can use array_reduce here, he needs a recursive function that gets to the last item of each array to set a value... I'm trying to write one down but I'm quite stuck – Alberto Sep 06 '18 at 15:30
  • 1
    @Leon, have you tried writing some code? – Alberto Sep 06 '18 at 15:30
  • 1
    I think you should change the way you put data in $field from the origin... This seems not to be a good implementation of whatever you are trying to do. – Alberto Sep 06 '18 at 15:40
  • Thanks @Alberto . The dot syntax field names are set by stripe, and cannot be changed. My previous attempts (over the last 48 hours!) were along the lines of - explode the fields string - loop over each section of the exploded string - $temp =& $temp->$var; adds the string to an object. I'll update my question, but to be honest it didn't work! – Leon Sep 06 '18 at 15:45
  • 1
    Wait, I think I found one – Alberto Sep 06 '18 at 15:51
  • 1
    @Leon `$temp->{$var}` – marciojc Sep 06 '18 at 16:02
  • 1
    The get function works if you change ->$var with $temp[$var] and return $temp (without then setting $value as value) but you only get the value of the last element of each array... You need to set it. Are you using the Stripe PHP API (https://stripe.com/docs/api/php) ? – Alberto Sep 06 '18 at 16:05
  • 1
    Check the duplicate question, you'll find the answer there. – Alberto Sep 06 '18 at 16:08
  • Results in "Indirect modification of overloaded element of Stripe\StripeObject has no effect" but we are getting closer! Thanks I'll check the duplicate – Leon Sep 06 '18 at 16:09
  • I'd seen that duplicate. Unfortunately it doesn't seem to work in this context. – Leon Sep 06 '18 at 16:12

0 Answers0