{"id":28098011,"url":"https://github.com/daison12006013/php-struct","last_synced_at":"2025-05-13T17:47:29.223Z","repository":{"id":49064332,"uuid":"517147718","full_name":"daison12006013/php-struct","owner":"daison12006013","description":"PHP Struct inspired by Go (golang)","archived":false,"fork":false,"pushed_at":"2022-07-26T01:57:04.000Z","size":42,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-02-27T23:35:50.122Z","etag":null,"topics":["library","php","strict-types","struct"],"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/daison12006013.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-23T19:32:02.000Z","updated_at":"2022-09-08T00:52:16.000Z","dependencies_parsed_at":"2022-09-24T01:40:41.440Z","dependency_job_id":null,"html_url":"https://github.com/daison12006013/php-struct","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daison12006013%2Fphp-struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daison12006013%2Fphp-struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daison12006013%2Fphp-struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daison12006013%2Fphp-struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daison12006013","download_url":"https://codeload.github.com/daison12006013/php-struct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253996792,"owners_count":21996754,"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":["library","php","strict-types","struct"],"created_at":"2025-05-13T17:47:24.369Z","updated_at":"2025-05-13T17:47:29.217Z","avatar_url":"https://github.com/daison12006013.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"PHP Version | Status\n------------|--------\n7.4 | [![PHP 7.4 Composer](https://github.com/daison12006013/php-struct/actions/workflows/php7.4.yml/badge.svg)](https://github.com/daison12006013/php-struct/actions/workflows/php7.4.yml)\n8.x | in progress...\n\n- [PHP Struct](#php-struct)\n  - [Quick Example](#quick-example)\n  - [Struct over Struct](#struct-over-struct)\n  - [Optional Keys](#optional-keys)\n  - [Checklists](#checklists)\n  - [License](#license)\n\n# PHP Struct\n\nA new way to structure your data, apply a real strict typing to your team, avoid any\n\n- Unwanted types, such as you're expecting it to be `int` while your FE / Client returns with `string` quoted value.\n- This helps you to avoid using too much `isset($data['key'])` or `!empty(...)`\n  - The array key is always present when calling `-\u003etoArray()`\n\nInspired by Go(golang)'s struct\n\n## Quick Example\n\nWe do it this way in golang, where we transform a json request into a struct type automatically.\n\n```go\ntype Photo struct {\n    name string\n    url  string\n}\ntype User struct {\n    firstName string\n    age       int\n    photos    []Photo\n}\n\nvar user User\njsonStr := `{...}`\njson.Unmarshal([]byte(jsonStr), \u0026user)\n\nfmt.Print(user.firstName)\nfmt.Print(user.age)\nfmt.Printf(\"%+v\", user.photos)\n```\n\nNow if we're going to convert that into this library.\n\n```php\nuse Daison\\Struct\\Collection;\nuse Daison\\Struct\\Common;\nuse Daison\\Struct\\TypeException;\nuse Daison\\Struct\\Struct;\n\n$photo = new Struct([\n    'name' =\u003e Common::STRING(),\n    'url'  =\u003e Common::STRING(),\n]);\n\n$user = new Struct([\n    'firstName' =\u003e Common::STRING(),\n    'age'       =\u003e Common::INTEGER(),\n    'photos'    =\u003e fn (array $p) =\u003e new Collection($photo, $p ?? []),\n]);\n\n$user-\u003eload([\n    'firstName' =\u003e 'John',\n    'age' =\u003e '31',\n    'photos' =\u003e [\n        ['url' =\u003e 'https://images.dummy.com/x.jpg'],\n        ['url' =\u003e 'https://images.dummy.com/y.jpg'],\n        ['url' =\u003e 'https://images.dummy.com/z.jpg'],\n    ],\n]);\n\ntry {\n    $user-\u003efirstName; // John\n    $user-\u003ephotos-\u003eempty(); // false\n    $user-\u003ephotos[0]-\u003eurl; // https://images.dummy.com/x.jpg\n\n    // TypeException -\u003e \"Struct: Data type of [age] expects [int] but value is '31' typed [string]\"\n    $user-\u003eage;\n\n    // TypeException -\u003e \"Struct: Undefined struct data [email]\"\n    $user-\u003eemail;\n} catch (TypeException $e) {\n    // log it somewhere thru your kibana / sentry\n    var_dump($e);\n\n    throw $e;\n}\n```\n\nA full examples can be seen inside [example.php](example.php) or by running `./run-tests.sh`\n\n## Struct over Struct\n\n```php\n// imagine this is your contract structure from your frontend / clients\n$load = [\n    'location' =\u003e ['x' =\u003e 0.111, 'y' =\u003e 0.555],\n];\n\n$location = new Struct([\n    'x' =\u003e Common::FLOAT(),\n    'y' =\u003e Common::FLOAT(),\n]);\n\n$sample = new Struct([\n    'location' =\u003e fn (array $d) =\u003e $location-\u003eload($d),\n]);\n\n$sample-\u003eload($load);\n$sample-\u003elocation-\u003ex; // 0.111\n$sample-\u003elocation-\u003ey; // 0.555\n```\n\n## Optional Keys\n\nOptional keys will still resolve the value as null, this acts the same in Golang interface{} for nil'ishable approach\n\n```php\n// imagine this is your contract structure from your frontend / clients\n$load = [\n    // basically this data is not required\n    // 'nullableKey' =\u003e 'whatever',\n\n    'requiredKey' =\u003e 1,\n];\n\n$sample = new Struct([\n    // if there is no return type, this library interprets it as optional or any type.\n    'nullableKey' =\u003e fn ($x) =\u003e $x,\n\n    // since we require it to be \"string\", the library can detect and throw\n    // TypeException if the provided data is missing or the type is wrong\n    'requiredKey' =\u003e Common::STRING(),\n]);\n$sample-\u003eload($load);\n$sample-\u003etoArray(); // ['nullableKey' =\u003e null, 'requiredKey' =\u003e 1]\n```\n\n## Checklists\n\n- [x] Struct-uring\n- [x] Thrown strict types\n  - [x] Data type is different from the value returned\n  - [x] Returned type is different from the value returned\n- [ ] Error Bags\n- [ ] Validation (in progress...)\n\n## License\n\nThe php-struct library is open-sourced software licensed under the [MIT license.](/license.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaison12006013%2Fphp-struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaison12006013%2Fphp-struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaison12006013%2Fphp-struct/lists"}