describe("::encode()", function () { it("encodes in json", function () { $json = Media::encode('json', ['key' => 'value']); expect($json)->toBe('{"key":"value"}'); }); it("encodes objects in json", function () { $json = Media::encode('json', new Collection(['key' => 'value'])); expect($json)->toBe('{"key":"value"}'); }); it("encodes in form data", function () { $json = Media::encode('form', ['key1' => 'value1', 'key2' => 'value2']); expect($json)->toBe('key1=value1&key2=value2'); }); }); describe("::decode()", function () { it("decodes json", function () { $data = Media::decode('json', '{"key":"value"}'); expect($data)->toBe(['key' => 'value']); }); it("decodes form data", function () { $data = Media::decode('form', 'key1=value1&key2=value2'); expect($data)->toBe(['key1' => 'value1', 'key2' => 'value2']); }); }); describe("::to()", function () { it("delegates to `::encode()`", function () { expect(Media::class)->toReceive('::encode')->with('json', '', ['key' => 'value']); Media::to('json', '', ['key' => 'value']); }); }); });
/** * Parses a JSON-API payload string. * * @return object The payload object. */ public static function parse($payload, $key = 'id', $keys = []) { if (!$payload) { $data = []; } elseif (is_string($payload)) { $data = Media::decode('json', $payload); } else { $data = $payload; } return new static(['data' => $data, 'key' => $key, 'keys' => $keys]); }