{"id":22611500,"url":"https://github.com/fbraem/kwai-jsonapi","last_synced_at":"2025-03-28T23:21:53.787Z","repository":{"id":45867932,"uuid":"432717722","full_name":"fbraem/kwai-jsonapi","owner":"fbraem","description":"JSON:API serializer for PHP resources","archived":false,"fork":false,"pushed_at":"2021-12-26T13:49:09.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-02-03T09:47:56.294Z","etag":null,"topics":["json-api","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fbraem.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-28T13:18:37.000Z","updated_at":"2023-06-05T10:36:34.000Z","dependencies_parsed_at":"2022-09-14T12:01:40.467Z","dependency_job_id":null,"html_url":"https://github.com/fbraem/kwai-jsonapi","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbraem%2Fkwai-jsonapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbraem%2Fkwai-jsonapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbraem%2Fkwai-jsonapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fbraem%2Fkwai-jsonapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fbraem","download_url":"https://codeload.github.com/fbraem/kwai-jsonapi/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246112809,"owners_count":20725329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-api","php"],"created_at":"2024-12-08T16:11:24.318Z","updated_at":"2025-03-28T23:21:53.761Z","avatar_url":"https://github.com/fbraem.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kwai-jsonapi\n![Latest Stable Version](http://poser.pugx.org/kwai/jsonapi/v)\n![PHP Version Require](http://poser.pugx.org/kwai/jsonapi/require/php)\n\nA JSON:API serializer for PHP classes using PHP attributes.\n\n\u003e Currently, this library has no support for [links](https://jsonapi.org/format/#document-links).\n\n## Installation\n\n```` \ncomposer require kwai/jsonapi\n````\n\n## Requirements\nPHP attributes are used to serialize a PHP class to a JSONAPI resource. So, the \nPHP version must be at least 8.0. There are no other external dependencies.\n\n## Documentation\n\n### #[JSONAPI/Resource]\nThe \"JSONAPI/Resource\" attribute is used to set the type of the resource. \n\n+ The type argument is required.\n+ This attribute can only be applied to a class.\n+ By default, the id is retrieved from the id property. Use the id argument to \n use a method or a property with another name.\n\n````php\nuse Kwai\\JSONAPI;\n\n#[JSONAPI/Resource(type: 'people')]\nclass Person \n{\n    public function __construct(\n        // We need at least an id property.\n        private string $id,\n    )\n}\n````\n\n### #[JSONAPI/Attribute]\nThe \"JSONAPI/Attribute\" attribute is used to set an attribute of a resource.\n\n+ This attribute can be applied to a property or a method of a class.\n+ The name argument can be used to give a name to the property.\n+ When applied to a method, the name argument is required.\n\n````php\nuse Kwai\\JSONAPI;\n\n#[JSONAPI/Resource(type: 'people')]\nclass Person \n{\n    public function __construct(\n        private string $id,\n        #[JSONAPI/Attribute]\n        private string $name,\n        private int $age,\n    ) {\n    }\n    \n    #[JSONAPI/Attribute(name: 'age')]\n    public function getAge(): int\n    {\n        return $this-\u003eage;\n    }\n}\n````\nProperties may be private. A method must be public.\n\nWith a Person instance like this:\n\n````php\n$person = new Person(\n    id: '1',\n    name: 'Jigoro Kano',\n    age: 77,\n);\n````\nThe result will be:\n\n````json\n{\n  \"data\": {\n    \"type\": \"people\",\n    \"id\": \"1\",\n    \"attributes\": {\n      \"name\": \"Jigoro Kano\",\n      \"age\": 77\n    }\n  }\n}\n````\n\n### #[JSONAPI/Relationship]\nThe \"JSONAPI/Relationship\" is used to map a relationship.\n\n+ This attribute can be applied to a property or a method.\n+ The name argument can be used to give a name to the relationship.\n+ When no name argument is set for a property, the name of the property will be used.\n+ When applied to a method, the name argument is required.\n\n````php\n#[JSONAPI\\Resource(type:'athletes')]\nclass Athlete\n{\n    public function __construct(\n        private string $id,\n        #[JSONAPI\\Attribute]\n        private string $name,\n        #[JSONAPI\\Relationship]\n        private Country $country,\n    ) {\n    }\n}\n````\nProperties may be private. A method must be public.\n\nThe linked resource must contain a JSONAPI\\Resource attribute. If not, a \nJSONAPI\\Exception will be thrown. A relationship can also be an array.\n\nWith the given PHP code:\n\n````php\n$country = new Country(\n    id: '1',\n    code: 'BEL',\n);\n$athlete = new Athlete(\n    id: '1',\n    name: 'Ingrid Berghmans',\n    country: $country\n)\n````\n\nThe result of the serializing will be:\n\n````json\n{\n  \"data\": {\n    \"type\": \"athletes\",\n    \"id\": \"1\",\n    \"attributes\": {\n      \"name\": \"Ingrid Berghmans\"\n    },\n    \"relationships\": {\n      \"country\": {\n        \"data\": {\n          \"type\": \"countries\",\n          \"id\": \"1\"\n        }\n      }\n    }\n  },\n  \"included\": [\n    {\n      \"type\": \"countries\",\n      \"id\": \"1\",\n      \"attributes\": {\n        \"code\": \"BEL\"\n      }\n    }\n  ]\n}\n````\n\n### Serializing\nThe JSONAPI\\Document class is used to serialize an object or an array to a\nJSON:API structure.\n\n````php\nuse Kwai\\JSONAPI;\n\n$person = new Person(\n    id: '1',\n    name: 'Jigoro Kano',\n    age: 77,\n);\n\ntry {\n    $jsonapi = JSONAPI\\Document::createFromObject($person)-\u003eserialize();\n    // Send $jsonapi to the client...\n} catch (JSONAPI\\Exception $e) {\n    // An exception occurred while serializing the PHP object.\n}\n````\n\n### Meta\nMeta information can be set with the setMeta method.\n\n````php\n    try {\n        $jsonapi =\n            JSONAPI\\Document::createFromObject($person)\n                -\u003esetMeta('count', 1)\n                -\u003eserialize();\n    } catch (JSONAPI\\Exception $e) {\n        // Handle exception...\n    }\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffbraem%2Fkwai-jsonapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffbraem%2Fkwai-jsonapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffbraem%2Fkwai-jsonapi/lists"}