{"id":18637960,"url":"https://github.com/imponeer/toarray-interface","last_synced_at":"2025-07-02T03:36:24.365Z","repository":{"id":30900591,"uuid":"126316971","full_name":"imponeer/toarray-interface","owner":"imponeer","description":"PHP library that provides toArray interface, that could be used everywhere where you need to convert object into array and make sure that to that toArray function is because of same interface","archived":false,"fork":false,"pushed_at":"2025-06-01T23:48:11.000Z","size":63,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-02T09:23:17.501Z","etag":null,"topics":["composer-library","hacktoberfest","interface","php","php-library","toarray","toarray-interface"],"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/imponeer.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-03-22T10:15:12.000Z","updated_at":"2025-06-01T23:48:13.000Z","dependencies_parsed_at":"2023-02-17T12:01:12.883Z","dependency_job_id":"bf319078-be24-442a-9d62-3bb116651ddf","html_url":"https://github.com/imponeer/toarray-interface","commit_stats":{"total_commits":35,"total_committers":6,"mean_commits":5.833333333333333,"dds":0.3142857142857143,"last_synced_commit":"a341ac5035a3c4363d1f3298bf6f23e55b1f8d28"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/imponeer/toarray-interface","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imponeer%2Ftoarray-interface","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imponeer%2Ftoarray-interface/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imponeer%2Ftoarray-interface/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imponeer%2Ftoarray-interface/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imponeer","download_url":"https://codeload.github.com/imponeer/toarray-interface/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imponeer%2Ftoarray-interface/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263071206,"owners_count":23409255,"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":["composer-library","hacktoberfest","interface","php","php-library","toarray","toarray-interface"],"created_at":"2024-11-07T05:38:27.472Z","updated_at":"2025-07-02T03:36:24.354Z","avatar_url":"https://github.com/imponeer.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License](https://img.shields.io/github/license/imponeer/toarray-interface.svg?maxAge=2592000)](LICENSE)\n[![GitHub release](https://img.shields.io/github/release/imponeer/toarray-interface.svg?maxAge=2592000)](https://github.com/imponeer/toarray-interface/releases) [![PHP](https://img.shields.io/packagist/php-v/imponeer/toarray-interface.svg)](http://php.net)\n[![Packagist](https://img.shields.io/packagist/dm/imponeer/toarray-interface.svg)](https://packagist.org/packages/imponeer/toarray-interface)\n\n# ToArray Interface\n\nThere have been several attempts to introduce a standard `toArray` method or interface in PHP:\n\n- The first attempt was in 2012, when an [RFC proposed adding several magic methods to PHP, including `__toArray`, `__toInt()`, `__toFloat()`, `__toScalar()`, and `__toBool()`](https://wiki.php.net/rfc/object_cast_to_types). This proposal failed before reaching the voting stage due to concerns about implementation details and lack of consensus.\n- In 2019, a separate [RFC specifically focused on introducing only a `__toArray` method for objects](https://wiki.php.net/rfc/to-array). Unlike the broader 2012 proposal, this RFC aimed to standardize array conversion alone, but it also failed before reaching the voting stage due to lack of support and unresolved concerns—particularly that the `__toArray` method did not specify what kind of array should be returned (e.g., associative or indexed), nor whether nested arrays or objects were allowed as values. One of the main unresolved questions was how to specify or standardize the format of the returned array in a way that would suit all use cases.\n- Over the years, various frameworks and libraries (such as [Laravel's illuminate/contracts](https://packagist.org/packages/illuminate/contracts)) have introduced their own `toArray` conventions, but these are not universal and often come with additional dependencies.\n- As of now, neither PHP itself nor the [PHP-FIG group](https://www.php-fig.org/psr/) has adopted an official standard for object-to-array conversion.\n\nThis package was created to address this gap: it provides a minimal Composer package containing only the `ToArrayInterface`. Use it when you need to ensure an object can be converted to an array via a `toArray` method, without introducing unnecessary dependencies.\n\n## Installation\n\nTo install and use this package, use [Composer](https://getcomposer.org):\n\n```bash\ncomposer require imponeer/toarray-interface\n```\n\nAlternatively, manually include the files from the `src/` directory.\n\n**Note:** For PHP 5 projects, use [version 1.0 of this library](https://packagist.org/packages/imponeer/toarray-interface#1.0.0).\n\n## Example\n\n```php\nuse Imponeer\\ToArrayInterface;\n\nclass DummyObject implements ToArrayInterface {\n\n    /**\n     * Converts object to array\n     *\n     * @return array\u003cstring, mixed\u003e\n     */\n    public function toArray(): array {\n        return [\n            'hash' =\u003e sha1(time()),\n        ];\n    }\n}\n\n$instance = new DummyObject();\nif ($instance instanceof ToArrayInterface) {\n    var_dump($instance-\u003etoArray());\n}\n```\n\n## Documentation\n\nThis repository features an automatically updated [GitHub Wiki](https://github.com/imponeer/toarray-interface/wiki) containing up-to-date code documentation generated from the source. You can use the Wiki to explore interfaces, methods, and other technical details about the project.\n\n## Alternatives\n\nBecause PHP does not yet have a standard way to convert objects to arrays, many alternative libraries exist, each trying to solve the same problem in its own way. Depending on your specific needs - such as recursion support, fluent APIs, strict interface contracts, or framework integration - you may find one of these packages better suited for your project.\n\nBelow is a curated list of popular alternatives, showcasing different approaches to object-to-array conversion in PHP:\n\n| Package                                                                                           | PHP Version | Features                                             | \n| ------------------------------------------------------------------------------------------------- | ----------- | ---------------------------------------------------- | \n| [inspirum/arrayable](https://github.com/inspirum/arrayable-php)                                 | [![PHP](https://img.shields.io/packagist/dependency-v/inspirum/arrayable/php)](http://php.net)        | Recursive conversion, helper functions, base classes | \n| [dmytrof/array-convertible](https://github.com/dmytrof/ArrayConvertible)           | [![PHP](https://img.shields.io/packagist/dependency-v/dmytrof/array-convertible/php)](http://php.net)        | Lightweight, nested conversion, simple interface     | \n| [rexlabsio/array-object-php](https://github.com/rexlabsio/array-object-php)                     | [![PHP](https://img.shields.io/packagist/dependency-v/rexlabs/array-object/php)](http://php.net)        | Fluent interface, `toArray()` and `toJson()` methods | \n| [php-extended/php-arrayable-interface](https://gitlab.com/php-extended/php-arrayable-interface) | [![PHP](https://img.shields.io/packagist/dependency-v/php-extended/php-arrayable-interface/php)](http://php.net)        | Interface-only, strict contract definition           |\n| [illuminate/contracts](https://github.com/illuminate/contracts) | [![PHP](https://img.shields.io/packagist/dependency-v/illuminate/contracts/php)](http://php.net) | Defines a generic `Arrayable` interface usable with or without Laravel |\n\nMissing an alternative? Submit a [pull request](https://github.com/imponeer/toarray-interface/compare) to get it added.\n\n## Development\n\nTo maintain code quality, this project uses:\n\n- [PHP_CodeSniffer (phpcs)](https://github.com/squizlabs/PHP_CodeSniffer) for coding standards.\n- [PHPStan](https://phpstan.org/) for static analysis.\n\n### Running PHPCS\n\nRun the following command to check code style:\n\n```bash\nvendor/bin/phpcs\n```\n\n### Running PHPStan\n\nRun the following command to perform static analysis:\n\n```bash\nvendor/bin/phpstan analyse\n```\n\nRefer to `phpcs.xml` and `phpstan.neon` for configuration details.\n\n## How to contribute?\n\nContributions are welcome! To contribute:\n\n1. Fork this repository on GitHub.\n2. Create a new branch for your feature or bugfix.\n3. Make your changes and commit them with clear messages.\n4. Push your branch to your fork and open a Pull Request.\n\nIf you find a bug or have a question, please use the [issues tab](https://github.com/imponeer/toarray-interface/issues).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimponeer%2Ftoarray-interface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimponeer%2Ftoarray-interface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimponeer%2Ftoarray-interface/lists"}