{"id":13816410,"url":"https://github.com/apioo/psx-schema","last_synced_at":"2025-08-11T09:39:00.157Z","repository":{"id":45480897,"uuid":"55151191","full_name":"apioo/psx-schema","owner":"apioo","description":"TypeSchema parser, object mapper and DTO generator","archived":false,"fork":false,"pushed_at":"2025-03-23T20:11:46.000Z","size":2041,"stargazers_count":56,"open_issues_count":0,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-03T11:03:08.982Z","etag":null,"topics":["code-generator","dto","json-schema","object-mapper","php","popo","typeschema"],"latest_commit_sha":null,"homepage":"https://typeschema.org/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apioo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"chriskapp","patreon":"fusio","custom":"https://www.paypal.me/fusioapi"}},"created_at":"2016-03-31T13:05:11.000Z","updated_at":"2025-03-23T20:10:10.000Z","dependencies_parsed_at":"2022-07-15T03:47:03.590Z","dependency_job_id":"42d00dad-8f08-43bf-bf4f-bba09252dbb0","html_url":"https://github.com/apioo/psx-schema","commit_stats":{"total_commits":555,"total_committers":5,"mean_commits":111.0,"dds":"0.20180180180180185","last_synced_commit":"16105e7e994b86a262d7c31ef9b027dda20c3820"},"previous_names":[],"tags_count":217,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apioo%2Fpsx-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apioo%2Fpsx-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apioo%2Fpsx-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apioo%2Fpsx-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apioo","download_url":"https://codeload.github.com/apioo/psx-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254110448,"owners_count":22016392,"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":["code-generator","dto","json-schema","object-mapper","php","popo","typeschema"],"created_at":"2024-08-04T05:00:40.550Z","updated_at":"2025-05-15T15:32:30.984Z","avatar_url":"https://github.com/apioo.png","language":"PHP","funding_links":["https://github.com/sponsors/chriskapp","https://patreon.com/fusio","https://www.paypal.me/fusioapi"],"categories":["PHP"],"sub_categories":[],"readme":"\n# Schema\n\nThis library helps you to work with fully typed objects, it provides the following features:\n\n* Transform raw JSON data into fully typed objects\n* Parse PHP classes into a [TypeSchema](https://typeschema.org/) specification\n* Generate DTOs in different languages like TypeScript, Java, C# etc.\n\nWe provide also a hosted version of this [code generator](https://typeschema.org/generator).\nFor more integration options you can also take a look at the [SDKgen](https://sdkgen.app/) project\nwhich provides a CLI binary or GitHub action to integrate the code generator.\n\n## Object mapper\n\nThis example reads raw JSON data and transform it into the provided `Person` class.\n\n```php\n$json = \u003c\u003c\u003c'JSON'\n{\n    \"firstName\": \"Ludwig\",\n    \"lastName\": \"Beethoven\",\n    \"age\": 254\n}\nJSON;\n\n$objectMapper = new ObjectMapper(new SchemaManager());\n\n$person = $objectMapper-\u003ereadJson($json, SchemaSource::fromClass(Person::class));\n\nassert('Ludwig' === $person-\u003egetFirstName());\nassert('Beethoven' === $person-\u003egetLastName());\nassert(254 === $person-\u003egetAge());\n\n$json = $objectMapper-\u003ewriteJson($person);\n```\n\nBesides a simple class there are multiple ways to specify a source, for example to parse\nan array of persons s.\n\n```php\n$json = \u003c\u003c\u003c'JSON'\n[\n    {\n        \"firstName\": \"Ludwig\",\n        \"lastName\": \"Beethoven\",\n        \"age\": 254\n    }\n]\nJSON;\n\n$objectMapper = new ObjectMapper(new SchemaManager());\n\n$personList = $objectMapper-\u003ereadJson($json, SchemaSource::fromType('array\u003cPerson\u003e'));\n\nassert(1 === count($personList));\nassert('Ludwig' === $personList[0]-\u003egetFirstName());\n\n$json = $objectMapper-\u003ewriteJson($person);\n```\n\n## Code generation\n\nIt is possible to transform any DTO class into a [TypeSchema](https://typeschema.org/) specification.\nThis schema can then be used to generate DTOs in different languages which helps to work with\ntype-safe objects across different environments.\n\n```php\n$schemaManager = new SchemaManager();\n$factory = new GeneratorFactory();\n\n$schema = $schemaManager-\u003egetSchema(Person::class);\n\n$generator = $factory-\u003egetGenerator(GeneratorFactory::TYPE_JAVA, Config::of('org.typeschema.model'));\n\n$result = $generator-\u003egenerate();\n\n$result-\u003ewriteTo('/my_model.zip');\n```\n\n## TypeSchema specification\n\nIt is possible to transform an existing [TypeSchema](https://typeschema.org/) specification into a PHP DTO class.\nFor example lets take a look at the following specification, which describes a person:\n\n```json\n{\n  \"definitions\": {\n    \"Person\": {\n      \"type\": \"struct\",\n      \"properties\": {\n        \"firstName\": {\n          \"type\": \"string\"\n        },\n        \"lastName\": {\n          \"type\": \"string\"\n        },\n        \"age\": {\n          \"description\": \"Age in years\",\n          \"type\": \"integer\"\n        }\n      }\n    }\n  },\n  \"root\": \"Person\"\n}\n```\n\nIt is then possible to turn this specification into a ready-to-use PHP class s.\n\n```php\n\n$schemaManager = new SchemaManager();\n$factory = new GeneratorFactory();\n\n$schema = $schemaManager-\u003egetSchema(__DIR__ . '/my_schema.json');\n\n$generator = $factory-\u003egetGenerator(GeneratorFactory::TYPE_PHP, Config::of('App\\\\Model'));\n\n$result = $generator-\u003egenerate();\n\nforeach ($result as $file =\u003e $code) {\n    file_put_contents(__DIR__ . '/' . $file, '\u003c?php' . \"\\n\" . $code);\n}\n```\n\nThis would result in the following PHP class:\n\n```php\n\u003c?php\n\ndeclare(strict_types = 1);\n\nclass Person implements \\JsonSerializable, \\PSX\\Record\\RecordableInterface\n{\n    protected ?string $firstName = null;\n    protected ?string $lastName = null;\n    #[Description(\"Age in years\")]\n    protected ?int $age = null;\n    public function setFirstName(?string $firstName) : void\n    {\n        $this-\u003efirstName = $firstName;\n    }\n    public function getFirstName() : ?string\n    {\n        return $this-\u003efirstName;\n    }\n    public function setLastName(?string $lastName) : void\n    {\n        $this-\u003elastName = $lastName;\n    }\n    public function getLastName() : ?string\n    {\n        return $this-\u003elastName;\n    }\n    public function setAge(?int $age) : void\n    {\n        $this-\u003eage = $age;\n    }\n    public function getAge() : ?int\n    {\n        return $this-\u003eage;\n    }\n    public function toRecord() : \\PSX\\Record\\RecordInterface\n    {\n        /** @var \\PSX\\Record\\Record\u003cmixed\u003e $record */\n        $record = new \\PSX\\Record\\Record();\n        $record-\u003eput('firstName', $this-\u003efirstName);\n        $record-\u003eput('lastName', $this-\u003elastName);\n        $record-\u003eput('age', $this-\u003eage);\n        return $record;\n    }\n    public function jsonSerialize() : object\n    {\n        return (object) $this-\u003etoRecord()-\u003egetAll();\n    }\n}\n```\n\nEvery generated PHP class implements also the `JsonSerializable` interface so you can simply encode an object to json.\n\n```php\n$person = new Person();\n$person-\u003esetFirstName('foo');\n$person-\u003esetLastName('bar');\n$person-\u003esetAge(32);\n\necho json_encode($person);\n\n// would result in\n// {\"firstName\": \"foo\", \"lastName\": \"bar\", \"age\": 32}\n\n```\n\n## Attributes\n\nThe following attributes are available:\n\n| Attribute     | Target         | Example                                 |\n|---------------|----------------|-----------------------------------------|\n| Deprecated    | Property       | #[Deprecated(true)]                     |\n| DerivedType   | Class          | #[DerivedType(Person::class, 'person')] |\n| Description   | Class/Property | #[Description(\"content\")]               |\n| Discriminator | Class          | #[Discriminator('type')]                |\n| Exclude       | Property       | #[Exclude]                              |\n| Format        | Property       | #[Format('date-time')]                  |\n| Key           | Property       | #[Key('my-complex-name')]               |\n| Nullable      | Property       | #[Nullable(true)]                       |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapioo%2Fpsx-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapioo%2Fpsx-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapioo%2Fpsx-schema/lists"}