i have a rest api request response in this form
{
"first_field": null,
"elements": [
{
"type": "type_1",
"quantity": 234
},
{
"type": "type_2",
"quantity": 432
},
{
"type": "type_3",
"quantity": 112
},
{
"type": "type_4",
"quantity": 212
}
],
"third_field": {
"option1": value1,
"option2": "value2",
"option3": "value3",
"option4": "value4"
}
}
I would like to pass it to a constructor to create an object. I thought to use the Symfony4 OptionsResolver to configure it.
Dummy and incomplete example
class Status{
public function __construct(array $options){
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$this->options = $resolver->resolve($options);
$this->init();
}
protected function init(){
foreach($this->options['elements'] as $element){
switch($element['type']){
case 'type_1'
$this->type1 = $element['quantity'];
break;
// And so on
}
}
}
}
How can i define in OptionsResolver the elements array? For third field the solution is the nested options, but it is an array of options, not an array of array of options...
Thanks in advance
EDIT To instantiate a new object from a Json Object a fine solution can be the karriere/json-decoder