{"id":15917458,"url":"https://github.com/takuya/php-generators-into-array-access","last_synced_at":"2025-10-11T09:04:18.505Z","repository":{"id":85877017,"uuid":"606689206","full_name":"takuya/php-generators-into-array-access","owner":"takuya","description":"php yield generator as ArrayAccess. ","archived":false,"fork":false,"pushed_at":"2023-02-28T16:34:53.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-11T23:14:52.329Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/takuya.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}},"created_at":"2023-02-26T09:01:51.000Z","updated_at":"2023-02-26T10:05:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"22c51545-3f8d-4aba-80df-50b262c1b006","html_url":"https://github.com/takuya/php-generators-into-array-access","commit_stats":{"total_commits":33,"total_committers":1,"mean_commits":33.0,"dds":0.0,"last_synced_commit":"96db94d51e4d7e2b76e1f5d3be196abc222d7ad3"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takuya%2Fphp-generators-into-array-access","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takuya%2Fphp-generators-into-array-access/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takuya%2Fphp-generators-into-array-access/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takuya%2Fphp-generators-into-array-access/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/takuya","download_url":"https://codeload.github.com/takuya/php-generators-into-array-access/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246998098,"owners_count":20866690,"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-10-06T18:10:45.424Z","updated_at":"2025-10-11T09:04:13.484Z","avatar_url":"https://github.com/takuya.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](https://github.com/takuya/php-generators-into-array-access/workflows/main/badge.svg)\n\n\n# php / GeneratorArrayAccess\n\nMake generator(yield) into ArrayAccess.\n\n## Example\n\n```php\n\u003c?php\n// generators by yield.\n$generator = (function(){foreach (['x'.'y','z'] as $e){ yield $e;}})();\n// Using with new.\nuse Takuya\\Php\\GeneratorArrayAccess;\n$iter = new GeneratorArrayAccess($generator);\n\n// Access as Array. \n$iter[0]; #=\u003e 'x'\n$iter[1]; #=\u003e 'y'\n$iter[2]; #=\u003e 'z'\n```\n\n## Why use.\n\nGenerator can foreach itself. but, cannot access as Array. This characteristic is against developers's intention. Like this.\n\n### Generator cannot be a array. \n```php\nclass MyClass{\n  public function elements(){\n    foreach ($this-\u003epaths[] as $path){\n      yield get_something($path);\n    }\n  }\n}\n\n$node = new MyClass();\n// Generator can Access foreach\nforeach ($node-\u003eelements() as $item) {\n  // something.    \n}\n// but Cannot access as Array\n$first = $node-\u003eelements()[0]; //=\u003e Error\n```\n\n### CachingIterator is not enough\n\nUsing `\\CachingIterator` is a common way, but make a problem\n#### \\CachingIterator cannot be a array.\n\nThis behaviour is very confusing.\n\n```php\n$node = new MyClass();\n$elements = new CachingIterator($node-\u003eelements())\n// CachingIterator cannot access Directory, before cached.\n$first = $elements[1]; //=\u003e BadMethodCallException\n// after caching, CachingIterator can access as Array.\nforeach ($elements as $e){;;}\n$first = $elements[1]; //=\u003e not null.\n```\n\nWith FullCache option, cached at initializing.\n\n```php\n$node = new MyClass();\n$elements = new \\CachingIterator(\n  $node-\u003eelements(),\n  \\CachingIterator::FULL_CACHE\n);// \u003c= All Cached on constructor.\n```\n\nIf generator is API call, It can spend a lot of time, caching time inevitable.\n\nCachingIterator is useless. Everyone uses `iterator_to_array()` instead.\n`iterator_to_array()`function has same problem ( load all, mess up Generator concept, get data when using ).\n\n### Dynamically get, make Unnecessary api call avoidable.\n\nTo solve that problem ,GeneratorArrayAccess cache dynamically.\n```php\n\n$node = new MyClass();\n$iter = new GeneratorArrayAccess($node-\u003eelements());\n$iter[1]; //=\u003e make cache $iter[0],$iter[1];\n$iter[9]; //=\u003e make cache $iter[0]...$iter[9]\n```\n\nCache is able to reuse.\n```php\n$node = new MyClass();\n$iter = new GeneratorArrayAccess($node-\u003eelements());\n// first access \nforeach($iter as $e){;;}\n// cache access with rewind.\nforeach($iter as $e){;;}\n```\n\n## When use this.\n\nReduce WebAPI Call. without re-arrange code.\n\nCurrent exists code.\n```php\nfunction my_list_items(){\n  foreach(  $api-\u003ecall('list_item') as $id){\n    $list[]=$api-\u003ecall('get_item', $id);\n  }\n  return $list;\n}\n$items = $my_list_items();\n$item = $items[0];\n```\nUse Generator.\n```php\nfunction my_list_items(){\n  foreach(  $api-\u003ecall('list_item') as $id){\n    $item  = $api-\u003ecall('get_item', $id);\n    yield $item;\n  }\n}\n$items = $my_list_items();\n$item = $items[0];//\u003c= No Code changed. Becomes ERORR!. \n```\nUse GeneratorArrayAccess\n```php\nfunction my_list_items(){\n  return new GeneratorArrayAccess((function(){\n  foreach(  $api-\u003ecall('list_item') as $id){\n    $item  = $api-\u003ecall('get_item', $id);\n    yield $item;\n  }  \n  })());\n}\n$items = $my_list_items();\n$item = $items[0];//\u003c= No Code changed. **No Error**.\n```\n\nThis class supports to make use of Generator(yield), Less code changed.\n\n### Limitations.\n\nInfinite generator will be no response.\n```php\n$generator = (function(){\n  while(true){ yield 'a';}\n})();\n$iter = new GeneratorArrayAccess($generator);\nsizeof($iter);//=\u003e infinite loop\n```\n\n\n\n\n## Installation\nfrom github\n\n```shell\nrepository='php-generators-into-array-access'\ncomposer config repositories.$repository \\\nvcs https://github.com/takuya/$repository  \ncomposer require takuya/$repository:master\ncomposer install\n```\nfrom composer \n```shell\ncomposer require takuya/php-genetator-array-access\n```\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakuya%2Fphp-generators-into-array-access","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftakuya%2Fphp-generators-into-array-access","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakuya%2Fphp-generators-into-array-access/lists"}