JMS Serializer ดีซีเรียลไลซ์อ็อบเจ็กต์ที่ซ้อนกัน

ฉันใช้: https://jmsyst.com/libs/serializer

สำหรับการดีซีเรียลไลซ์อ็อบเจ็กต์ json ขณะนี้ฉันมีสิ่งนี้:

/**
 * @param int $id
 * @return Customer
 * @throws \Http\Client\Exception
 */
public function get(int $id): Customer
{
    $response = $this->client->get('/customers/' . $id);

    $data = json_encode(json_decode(
        $response->getBody()->getContents()
    )->data->attributes);

    return $this
        ->serializer
        ->deserialize($data, Customer::class, 'json');
}

json ที่ฉันได้รับที่นี่มีลักษณะดังนี้:

   {
    "data": {
            "type": "customer",
            "id": "4356456",
            "links": {
                    "self":"https:\/\/api.ecurring.com\/customers\/345656"
            },
            "attributes": {
                    "gender": "m",
            "first_name": "Laurens"
        }
    }

เป็นไปได้ไหมที่จะบอก JMS ว่าควรเริ่มจาก data->attributes โดยอัตโนมัติแทนที่จะทำอะไรสกปรกแบบนี้:

 $data = json_encode(json_decode(
            $response->getBody()->getContents()
        )->data->attributes);

person Jamie    schedule 04.03.2019    source แหล่งที่มา


คำตอบ (1)


คุณสามารถตรวจสอบนโยบายการยกเว้นได้

อ่านเอกสาร มันมีประโยชน์มาก

นโยบายการยกเว้น และ คำอธิบายประกอบ

person thafirstone    schedule 05.03.2019