{"id":25553936,"url":"https://github.com/prezly/draft-php","last_synced_at":"2025-04-11T22:52:09.382Z","repository":{"id":11307320,"uuid":"69226620","full_name":"prezly/draft-php","owner":"prezly","description":"Simple Draft.js model implemented in PHP","archived":false,"fork":false,"pushed_at":"2022-09-14T12:08:28.000Z","size":88,"stargazers_count":12,"open_issues_count":0,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-11T22:52:03.846Z","etag":null,"topics":["contentstate","draft-js","draft-php","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/prezly.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2016-09-26T07:56:47.000Z","updated_at":"2023-03-23T11:54:36.000Z","dependencies_parsed_at":"2022-09-18T03:50:50.065Z","dependency_job_id":null,"html_url":"https://github.com/prezly/draft-php","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prezly%2Fdraft-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prezly%2Fdraft-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prezly%2Fdraft-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prezly%2Fdraft-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prezly","download_url":"https://codeload.github.com/prezly/draft-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248492965,"owners_count":21113161,"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":["contentstate","draft-js","draft-php","php"],"created_at":"2025-02-20T12:03:31.896Z","updated_at":"2025-04-11T22:52:09.362Z","avatar_url":"https://github.com/prezly.png","language":"PHP","readme":"Prezly's Draft-PHP\n==================\n\nPHP implementation of Draft.js ContentState model to allow server-side handling of Draft.js produced content.\n\n![Tests Status](https://github.com/prezly/draft-php/actions/workflows/test.yml/badge.svg)\n\n### Table of contents\n\n* [Usage](#usage)\n  - [Reading ContentState JSON](#reading-contentstate-json)\n  - [Serializing ContentState back to JSON](#serializing-contentstate-back-to-json)\n* [Notes on implementation](#notes-on-implementation)\n* [Other implementations](#other-implementations)\n* [License](#license)\n* [Credits](#credits)\n\nUsage\n-----\n\n### Reading ContentState JSON\n\n```php\n// raw JSON content state coming from Draft.js frontend\n$json = '{\n    \"blocks\":[\n        {\n           \"key\": \"7si2a\",\n           \"text\": \"Say hello to world!\",\n           \"type\": \"unstyled\",\n           \"depth\": 0,\n           \"inlineStyleRanges\": [\n               {\n                    \"style\": \"BOLD\",\n                    \"offset\": 0,\n                    \"length\": 9\n               }\n           ],\n           \"entityRanges\": [\n               {\n                    \"key\": \"0\",\n                    \"offset\": 0,\n                    \"length\": 9\n               }\n           ],\n           \"data\": {}\n         }\n    ],\n    \"entityMap\":{\n        \"0\":{\n            \"type\":\"link\",\n            \"mutability\":\"MUTABLE\",\n            \"data\":{\n               \"href\":\"https://www.prezly.com/\"\n            }\n        }\n    }\n}';\n\n// convert raw JSON state to ContentState model object \n$contentState = \\Prezly\\DraftPhp\\Converter::convertFromJson($json);\n// or\n$rawState = json_decode($json); // Note: raw state should be an stdClass object, not an associative array\n$contentState = \\Prezly\\DraftPhp\\Converter::convertFromRaw($rawState);\n\nvar_dump($contentState);\n/*\n  Prezly\\DraftPhp\\Model\\ContentState {\n    -_blocks: array:1 [\n      0 =\u003e Prezly\\DraftPhp\\Model\\ContentBlock {#1507\n        -_key: \"7si2a\"\n        -_type: \"unstyled\"\n        -_text: \"Say hello to world!\"\n        -_characterList: array:19 [\n          0 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506\n            -_style: [\"BOLD\"]\n            -_entity: \"0\"\n          }\n          1 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          2 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          3 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          4 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          5 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          6 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          7 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          8 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506}\n          9 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490\n            -_style: []\n            -_entity: null\n          }\n          10 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          11 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          12 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          13 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          14 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          15 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          16 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          17 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n          18 =\u003e Prezly\\DraftPhp\\Model\\CharacterMetadata {#1490}\n        ]\n        -_depth: 0\n        -_data: []\n      }\n    ]\n    -_entityMap: array:1 [\n      0 =\u003e Prezly\\DraftPhp\\Model\\EntityInstance {#1505\n        -_type: \"link\"\n        -_mutability: \"MUTABLE\"\n        -_data: array:1 [\n          \"href\" =\u003e \"https://www.prezly.com/\"\n        ]\n      }\n    ]\n  }\n */\n \nvar_dump($contentState-\u003eblocks[0]-\u003echaracterList[0]);\n/*\n  Prezly\\DraftPhp\\Model\\CharacterMetadata {#1506\n    -_style: [\"BOLD\"]\n    -_entity: \"0\"\n  }\n*/\n\nvar_dump($contentState-\u003egetEntity($contentState-\u003eblocks[0]-\u003echaracterList[0]-\u003eentity));\n/*\n  Prezly\\DraftPhp\\Model\\EntityInstance {#1505\n    -_type: \"link\"\n    -_mutability: \"MUTABLE\"\n    -_data: array:1 [\n      \"href\" =\u003e \"https://www.prezly.com/\"\n    ]\n  }\n*/\n```\n\n### Serializing ContentState back to JSON\n\n```php\n// convert raw JSON state to ContentState model object \n$contentState = \\Prezly\\DraftPhp\\Converter::convertFromJson($json);\n\n$serializedJson = \\Prezly\\DraftPhp\\Serializer::serialize($contentState);\n// or\n$serializedJson = json_serialize($contentState); \n\n// now $json is equivalent to $serializedJson (formatting and order may differ though)\n// see SerializerTest for examples\n```\n\nNotes on implementation \n-----------------------\n\n1. ContentState now holds an `$entityMap` property and has `-\u003egetEntity(string $entityKey)` method.\n   This approach allows to incapsulate all the data coming from JSON into a single object and then use it for rendering.\n   \n   Having global static pool of entities (as in native Draft.js implementation, and another PHP port of Draft.js model) \n   is not that useful. Global state gets into your way when you need to render multiple content states in  a single PHP process.\n   Also it complicates testing.\n   \n2. All the model classes are immutable. That's achived by storing all the data in private properies \n   providing getters only as public API (`getXxxx` methods + magic `__get()` method to emulate read-only public props).\n   \nOther implementations\n---------------------\n\n- [webstronauts/draft-php](https://github.com/webstronauts/draft-php) — a one-to-one port of Draft.js model specs, well tested\n\nLicense\n-------\n[MIT](./LICENSE)\n\nCredits\n-------\nBuilt with :metal: by [Prezly](https://www.prezly.com/) — CRM software crafted for PR communication \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprezly%2Fdraft-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprezly%2Fdraft-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprezly%2Fdraft-php/lists"}