{"id":20016895,"url":"https://github.com/inspirum/arrayable-php","last_synced_at":"2025-05-04T22:31:48.000Z","repository":{"id":48305189,"uuid":"508441725","full_name":"inspirum/arrayable-php","owner":"inspirum","description":"PHP Arrayable interface with to-array method.","archived":false,"fork":false,"pushed_at":"2024-12-12T13:51:00.000Z","size":78,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-25T00:29:25.049Z","etag":null,"topics":["array","arrayable","php","to-array","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/inspirum.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"docs/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"docs/CODE_OF_CONDUCT.md","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}},"created_at":"2022-06-28T20:15:25.000Z","updated_at":"2024-12-12T13:51:06.000Z","dependencies_parsed_at":"2024-04-19T14:43:13.811Z","dependency_job_id":"ea9e655a-36cb-461c-a23b-f0fe67e6fb2b","html_url":"https://github.com/inspirum/arrayable-php","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"abcb5da1569cd45d7d481e22bc2150a1a7102e3c"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspirum%2Farrayable-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspirum%2Farrayable-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspirum%2Farrayable-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inspirum%2Farrayable-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inspirum","download_url":"https://codeload.github.com/inspirum/arrayable-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252408357,"owners_count":21743102,"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":["array","arrayable","php","to-array","toarray","toarray-interface"],"created_at":"2024-11-13T08:13:36.161Z","updated_at":"2025-05-04T22:31:47.991Z","avatar_url":"https://github.com/inspirum.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Arrayable\n\n[![Latest Stable Version][ico-packagist-stable]][link-packagist-stable]\n[![Build Status][ico-workflow]][link-workflow]\n[![Coverage Status][ico-scrutinizer]][link-scrutinizer]\n[![Quality Score][ico-code-quality]][link-code-quality]\n[![PHPStan][ico-phpstan]][link-phpstan]\n[![Total Downloads][ico-packagist-download]][link-packagist-download]\n[![Software License][ico-license]][link-licence]\n\n\n## Motivation\n\nUnfortunately PHP does not have a nice way how to typecast objects to `array`.\n\nThere is the `__toString()` magic method for [`\\Stringable`](https://www.php.net/manual/en/class.stringable.php) interface (since PHP 8.0) and the `jsonSerialize()` method for [`\\JsonSerializable`](https://www.php.net/manual/en/class.jsonserializable.php) interface (since PHP 5.4), but `__toArray()` method is not (and will not) be supported – there are just several rejected draft RFC ([object_cast_to_types](https://wiki.php.net/rfc/object_cast_to_types), [to array](https://wiki.php.net/rfc/to-array), ...) that suggests some kind of object to scalar type casting.\n\nBut so far (at least) there is no way to implement some (not even magic) method to be called when cast to `array`.\n\nIdeally, something like this would work:\n\n```php\nclass Person\n{\n    public function __construct(\n        public string $name,\n        protected string $username,\n        private string $password,\n    ) {}\n \n    public function __toArray(): array\n    {\n        return [\n            'name' =\u003e $this-\u003ename,\n            'email' =\u003e $this-\u003eusername,\n        ];\n    }\n}\n\n$person = new Person('John Doe', 'j.doe@example.com', 'secret_pwd');\n\n$personArray = (array) $person; // casting triggers __toArray()\n\n/**\nvar_dump($personArray);\n[\n  'name' =\u003e 'John Doe'\n  'email' =\u003e 'j.doe@example.com'\n]\n*/\n```\n\nbut actually it cast to array like this:\n\n```php\n/**\nvar_dump($personArray);\n[\n  'name' =\u003e 'John Doe'\n  '*username' =\u003e 'j.doe@example.com'\n  'Person@password' =\u003e 'secret_pwd'\n]\n*/\n```\n\n\n## Usage example\n\n*All the code snippets shown here are modified for clarity, so they may not be executable.*\n\nThis package implements simple `\\Arrayable` (or `\\Inspirum\\Arrayable\\Arrayable`) interface.\n\n```php\n/** @implements \\Arrayable\u003cstring, string\u003e */\nclass Person implements \\Arrayable\n{\n    public function __construct(\n        public string $name,\n        protected string $username,\n        protected string $password,\n    ) {}\n \n    /** @return array\u003cstring, string\u003e */\n    public function __toArray(): array\n    {\n        return [\n            'name' =\u003e $this-\u003ename,\n            'email' =\u003e $this-\u003eusername,\n        ];\n    }\n}\n\n$person = new Person('John Doe', 'j.doe@example.com', 'secret_pwd');\n```\n\nThere is `\\is_arrayable()` function (or `\\Inspirum\\Arrayable\\Convertor::isArrayable()` method) to check if given data are able to type cast itself to `array`.\n\n```php\nvar_dump(\\is_arrayable([1, 2, 3])); // bool(true)\nvar_dump(\\is_arrayable(new \\ArrayIterator([1, 2, 3]))); // bool(true)\nvar_dump(\\is_arrayable(new \\ArrayObject([4, 5, 6]))); // bool(true)\nvar_dump(\\is_arrayable((function () { yield 1; })())); // bool(true)\nvar_dump(\\is_arrayable(1)); // bool(false)\nvar_dump(\\is_arrayable(new \\stdClass())); // bool(false)\nvar_dump(\\is_arrayable(new class {})); // bool(false)\nvar_dump(\\is_arrayable(new class implements \\Arrayable {})); // bool(true)\nvar_dump(\\is_arrayable($person); // bool(true)\n```\n\nThen there is `\\to_array()` function (or `\\Inspirum\\Arrayable\\Convertor::toArray()` method) to recursively cast data to `array`.\n\n```php\n$data = \\to_array(new \\ArrayIterator([1, $person, (object) ['a' =\u003e true]]));\n\n/**\nvar_dump($data);\n[\n  0 =\u003e 1\n  1 =\u003e [ \n    'name' =\u003e 'John Doe'\n    'email' =\u003e 'j.doe@example.com'\n  ]\n  2 =\u003e [\n   'a' =\u003e true\n  ]\n*/\n```\n\nThere is also helper abstract classes for common use for DAO ([`BaseModel`](./src/BaseModel.php)) and collection ([`BaseCollection`](./src/BaseCollection.php)) objects.\n\n\n## System requirements\n\n* [PHP 8.1+](http://php.net/releases/8_1_0.php)\n* [ext-json](http://php.net/json)\n\n\n## Installation\n\nRun composer require command\n```bash\n$ composer require inspirum/arrayable\n```\n\n\n## Testing\n\nTo run unit tests, run:\n\n```bash\n$ composer test:test\n```\n\nTo show coverage, run:\n\n```bash\n$ composer test:coverage\n```\n\n\n## Contributing\n\nPlease see [CONTRIBUTING][link-contributing] and [CODE_OF_CONDUCT][link-code-of-conduct] for details.\n\n\n## Security\n\nIf you discover any security related issues, please email tomas.novotny@inspirum.cz instead of using the issue tracker.\n\n\n## Credits\n\n- [Tomáš Novotný](https://github.com/tomas-novotny)\n- [All Contributors][link-contributors]\n\n\n## License\n\nThe MIT License (MIT). Please see [License File][link-licence] for more information.\n\n\n[ico-license]:              https://img.shields.io/github/license/inspirum/arrayable-php.svg?style=flat-square\u0026colorB=blue\n[ico-workflow]:             https://img.shields.io/github/actions/workflow/status/inspirum/arrayable-php/master.yml?branch=master\u0026style=flat-square\n[ico-scrutinizer]:          https://img.shields.io/scrutinizer/coverage/g/inspirum/arrayable-php/master.svg?style=flat-square\n[ico-code-quality]:         https://img.shields.io/scrutinizer/g/inspirum/arrayable-php.svg?style=flat-square\n[ico-packagist-stable]:     https://img.shields.io/packagist/v/inspirum/arrayable.svg?style=flat-square\u0026colorB=blue\n[ico-packagist-download]:   https://img.shields.io/packagist/dt/inspirum/arrayable.svg?style=flat-square\u0026colorB=blue\n[ico-phpstan]:              https://img.shields.io/badge/style-level%2010-brightgreen.svg?style=flat-square\u0026label=phpstan\n\n[link-author]:              https://github.com/inspirum\n[link-contributors]:        https://github.com/inspirum/arrayable-php/contributors\n[link-licence]:             ./LICENSE.md\n[link-changelog]:           ./CHANGELOG.md\n[link-contributing]:        ./docs/CONTRIBUTING.md\n[link-code-of-conduct]:     ./docs/CODE_OF_CONDUCT.md\n[link-workflow]:            https://github.com/inspirum/arrayable-php/actions\n[link-scrutinizer]:         https://scrutinizer-ci.com/g/inspirum/arrayable-php/code-structure\n[link-code-quality]:        https://scrutinizer-ci.com/g/inspirum/arrayable-php\n[link-packagist-stable]:    https://packagist.org/packages/inspirum/arrayable\n[link-packagist-download]:  https://packagist.org/packages/inspirum/arrayable/stats\n[link-phpstan]:             https://github.com/phpstan/phpstan\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finspirum%2Farrayable-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finspirum%2Farrayable-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finspirum%2Farrayable-php/lists"}