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();