10/02/2018 - SYMFONY
In this example we are going to map multidimensional complex JSON request into model classes with JMSSerializerBundle. I am not going to show how to use serialiser to map data to model because there are a lot of examples in this blog that does it so you might just look for it.
Make sure to run composer require jms/serializer-bundle
command.
{
"schedules": {
"Monday": [
{
"minute": "05",
"hour": "13"
},
{
"minute": "05",
"hour": "14"
},
{
"minute": "05",
"hour": "15"
}
],
"Sunday": [
{
"minute": "05",
"hour": "13"
}
]
}
}
declare(strict_types=1);
namespace AppBundle\Model;
use JMS\Serializer\Annotation as Serializer;
class Create
{
/**
* @Serializer\Type("array<string, array<AppBundle\Model\Schedule>>")
*/
public $schedules;
}
declare(strict_types=1);
namespace AppBundle\Model;
use JMS\Serializer\Annotation as Serializer;
class Schedule
{
/**
* @Serializer\Type("string")
*/
public $minute;
/**
* @Serializer\Type("string")
*/
public $hour;
}
AppBundle\Model\Create Object
(
[schedules] => Array
(
[Monday] => Array
(
[0] => AppBundle\Model\Schedule Object
(
[minute] => 05
[hour] => 13
)
[1] => AppBundle\Model\Schedule Object
(
[minute] => 05
[hour] => 14
)
[2] => AppBundle\Model\Schedule Object
(
[minute] => 05
[hour] => 15
)
)
[Sunday] => Array
(
[0] => AppBundle\Model\Schedule Object
(
[minute] => 05
[hour] => 13
)
)
)
)