{"id":15029380,"url":"https://github.com/edmondscommerce/php-generic","last_synced_at":"2025-10-04T00:30:36.853Z","repository":{"id":56975110,"uuid":"170994074","full_name":"edmondscommerce/php-generic","owner":"edmondscommerce","description":"Generator for generic array/vector.","archived":false,"fork":true,"pushed_at":"2019-06-14T13:07:02.000Z","size":145,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-29T20:22:59.083Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"d0niek/php-generic","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edmondscommerce.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":"2019-02-16T11:04:06.000Z","updated_at":"2019-06-14T13:06:00.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/edmondscommerce/php-generic","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fphp-generic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fphp-generic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fphp-generic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fphp-generic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edmondscommerce","download_url":"https://codeload.github.com/edmondscommerce/php-generic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235204448,"owners_count":18952326,"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":[],"created_at":"2024-09-24T20:10:28.402Z","updated_at":"2025-10-04T00:30:31.581Z","avatar_url":"https://github.com/edmondscommerce.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# This is a Fork\n\nPlease see original: https://github.com/d0niek/php-generic\n\nThis is now becoming more of a pure wrapper for php-ds to allow generating of typed implementations of the php-ds data structures.\n\nCurrently there is only Vector, though more might be added as we need them\n\n## PHPStan\n\nCurrently seeing some issues with PHPStan, suggest the following ignore errors:\n\n```\nparameters:\n    ignoreErrors:\n        - '#unknown class.+?HumbugBox.+?Vector#'\n        - '#Vector.+?offset.+?\\(\\) should be contravariant#'\n        - '#Cannot .+? offset int .+? Ds\\\\Vector#'\n```\n\n-------\n\nOriginal README\n\n# Prepare for future with php-generic\n\nAccording to this [article](https://www.sitepoint.com/creating-strictly-typed-arrays-collections-php/)\nwhich shows how to create strictly typed arrays and collections in Php7, php-generic generator was born.\n\n---\n\nThere is some [discusion](https://wiki.php.net/rfc/generic-arrays) about generic in Php\nbut who knows when it comes to us.\n\nIt is not exacly what you know from Java or C++ where generic looks like\n`Vector\u003cint\u003e()`, `Array\u003cbool\u003e()` or `Vector\u003c\\Namespace\\Entity\\User\u003e()`.\n\nHere generics looks like `VectorInt`, `ArrayBool` and `VectorUser`\nso I hope when they come to nativ Php all what you need to do will be:\n1. Replace all `VectorType`, `ArrayType` to `Vector\u003cType\u003e`, `array\u003cType\u003e`,\n2. Delete directory where you store all generated array/vector,\n3. Enjoy a nice day.\n\n## What generics are (not)\n\nThey are not collections like Doctrine or Laravel Collections. They are like normal php array\nwhich can store values one type. `array\u003cint\u003e` can store only numeric values which will converted to `int`\nso you can not push `'some string value'` to it.\n\n## Install\n\n```bash\n$ composer require d0niek/php-generic\n```\n\n## Generate generic `array\u003cType\u003e`\n\nThere is a bin command that you should find in **vendor/bin**\nor somewhere else according to your **composer.json** settings.\n\nTo generate a generic array run:\n\n```bash\n$ bin/generic generate:array [-s|--save [SAVE]] [--] \u003ctype\u003e \u003cnamespace\u003e\n```\nwhere:\n* **-s**|**--saveCollection** - do you want to save generated array for future regenerate (default **true**),\n* **type** - is a type of generic array. It can be simple type (bool, int, float, string, array)\nor complex type (\\\\YourApp\\\\Module\\\\Repository\\\\User),\n* **namespace** - is a namespace where new generic array will be save.\nRemember that namespace's directory have to exists.\nTo separate namespace parts use **\\\\\\\\** or **/** to speed up typing\nif your namespace is 1:1 with your directory structure\n\nFor example you have project in **/path/to/project** and your **composer.json** contains this kind of entry:\n```json\n\"autoload\": {\n    \"psr-4\": {\n        \"VendorName\\\\AppName\\\\\": \"src/\"\n    }\n}\n```\nNow, when you call command like this:\n```bash\n$ bin/generic generate:array int VendorName\\\\AppName\\\\Collections\n```\nnew generic array `ArrayInt` will be save to **/path/to/project/src/Collections/** directory.\nIf this directory does not exists, exceptions will be throw.\n\u003e Tip! Store all php-generics in one diretory and add it to **.gitignore**.\nWhen php will start support generics, replace `ArrayInt` to `array\u003cint\u003e` and remove php-generic directory.\n\n## Generate generic `Vector\u003cType\u003e`\n\nYou can alse generate generic [\\\\Ds\\\\Vector](http://php.net/manual/en/class.ds-vector.php)\n(it is new data structure since Php7,\n    [here](https://medium.com/@rtheunissen/efficient-data-structures-for-php-7-9dda7af674cd)\n    you can and you should read about it!). To do this just run:\n```bash\n$ bin/generic generate:vector [-s|--save [SAVE]] [--] \u003ctype\u003e \u003cnamespace\u003e\n```\nparameters means exacly the same whats means when you run `generate:array`.\n\n## Regenerate\n\nBy defaule generated array/vector are save in **generated-colletions.json** file in your root app path.\nKeep this file in repository and ignore all generated php-generics. When you clone repository,\nafter `composer install` run:\n```bash\n$ bin/generic collections:regenerate\n```\nand all your collections will be regenerate.\n\n## Select data from DB\n\nNow you can create in easy way specific generic when you are selecting data from DB\n```php\nclass UserRepository implements UserRepositoryInterface\n{\n    ...\n\n    /**\n     * @inheritDoc\n     */\n    public function findAll(): VectorUser\n    {\n        $users = new VectorUser();\n        $mysqli = new \\mysqli('localhost:3306', 'user', 'password', 'db');\n\n        $mysqliResult = $mysqli-\u003equery('SELECT id, name FROM users LIMIT 10');\n        if ($mysqliResult !== false) {\n            while (($user = $mysqliResult-\u003efetch_object(User::class)) !== null) {\n                $users-\u003epush($user);\n            }\n        }\n\n        $mysqli-\u003eclose();\n\n        return $users;\n    }\n\n    ...\n}\n```\n\n## Test\n\nBefore you run tests remember to regenerate collections. Run:\n```bash\n$ bin/generic collections:regenerate\n```\nand now you can run\n```bash\n$ phpunit\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedmondscommerce%2Fphp-generic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedmondscommerce%2Fphp-generic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedmondscommerce%2Fphp-generic/lists"}