{"id":16954354,"url":"https://github.com/nanosector/collections","last_synced_at":"2026-03-12T00:33:30.556Z","repository":{"id":37013363,"uuid":"95682700","full_name":"NanoSector/collections","owner":"NanoSector","description":"Simple Collection class allowing storage of specific data.","archived":false,"fork":false,"pushed_at":"2023-10-02T22:28:18.000Z","size":266,"stargazers_count":4,"open_issues_count":5,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-14T22:09:34.669Z","etag":null,"topics":["array","arrayobject","collections","data-storage","php","validation"],"latest_commit_sha":null,"homepage":null,"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/NanoSector.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":"2017-06-28T15:12:31.000Z","updated_at":"2023-02-13T09:31:53.000Z","dependencies_parsed_at":"2024-06-19T02:42:24.628Z","dependency_job_id":"9bd88ac0-9949-477b-9538-4c926e5b150d","html_url":"https://github.com/NanoSector/collections","commit_stats":{"total_commits":110,"total_committers":6,"mean_commits":"18.333333333333332","dds":0.5545454545454546,"last_synced_commit":"c4f3f068cd1cc795a2a4d4683ed830b7ac022dbd"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Fcollections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Fcollections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Fcollections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NanoSector%2Fcollections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NanoSector","download_url":"https://codeload.github.com/NanoSector/collections/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221830341,"owners_count":16887796,"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","arrayobject","collections","data-storage","php","validation"],"created_at":"2024-10-13T22:09:29.731Z","updated_at":"2026-03-12T00:33:30.510Z","avatar_url":"https://github.com/NanoSector.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Collections\n[![Build Status](https://scrutinizer-ci.com/g/Yoshi2889/collections/badges/build.png)](https://scrutinizer-ci.com/g/Yoshi2889/collections/build-status/master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Yoshi2889/collections/badges/quality-score.png)](https://scrutinizer-ci.com/g/Yoshi2889/collections/?branch=master)\n[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/Yoshi2889/collections/badges/coverage.png)](https://scrutinizer-ci.com/g/Yoshi2889/collections/code-structure/master/code-coverage)\n[![Latest Stable Version](https://poser.pugx.org/yoshi2889/collections/v/stable)](https://packagist.org/packages/yoshi2889/collections)\n[![Latest Unstable Version](https://poser.pugx.org/yoshi2889/collections/v/unstable)](https://packagist.org/packages/yoshi2889/collections)\n[![Total Downloads](https://poser.pugx.org/yoshi2889/collections/downloads)](https://packagist.org/packages/yoshi2889/collections)\n\nSimple Collection class allowing storage of specific data, based on PHP's ArrayObject.\n\n## Installation\nYou can install this class via `composer`:\n\n```composer require yoshi2889/collections```\n\n## Usage\nTo use a Collection, create a new instance:\n\n```php\n$validationClosure = function ($value)\n{\n    return is_string($value);\n};\n\n$initialItems = ['This is a test value!', 'This is another test value!'];\n\n$collection = new \\Yoshi2889\\Collections\\Collection($validationClosure, $initialItems);\n```\n\nNote that the closure passed as the first parameter to the `Collection` constructur **MUST** return a boolean value.\nThis function is used to validate any added data types. A returned value of true means the given value may be added to the collection.\n\nThe second parameter can contain any initial values which should be in the collection. These items will also be validated.\n\n### Validating\nData is validated using a `Closure` instance, or a callback. The given closure must support exactly 1 untyped(!) parameter and\nreturn a boolean value. Values for which the closure returns `false` will be denied with an `\\InvalidArgumentException`.\n\nPlease note that only the **values** are validated, while keys are not.\n\nValidation can be manually performed on values using the `-\u003evalidateType($value)` and `-\u003evalidateArray(array $array)` methods, which\ncheck a single value and an array of values respectively.\n\n`-\u003evalidateType($value)` will return boolean `true` if the value passes validation, and `false` if it does not.\n\n`-\u003evalidateArray(array $array)` will return boolean `true` if **all** the values pass validation, and `false` if one or more do not.\n \n### Manipulation\nA `Collection` instance may be treated like a generic array. This means you can manipulate it like the following example:\n\n```php\n$validationClosure = function ($value)\n{\n    return is_string($value);\n};\n\n$collection = new \\Yoshi2889\\Collections\\Collection($validationClosure);\n\n$collection['foo'] = 'bar';\n```\n\nPlease note that any data entered this way will still be validated and may still throw an `\\InvalidArgumentException`.\n\nFurthermore, to remove data from the Collection you may use the `-\u003eoffsetUnset($index)` and `-\u003eremoveAll($value)` methods.\n\n`-\u003eoffsetUnset($index)` will remove the value located at key `$index`, which works similarly to using `unset($collection['key'])`.\n\n`-\u003eremoveAll($value)` will remove every value which is equal to `$value` from the collection. Because values may exist multiple times \nin the same Collection, it is not possible to reliably remove only a single value from the collection using a value; consider removing by key instead.\nIf you wish to preserve the original values while creating a new instance without certain values, please refer to Filtering below.\n\n### Filtering\nA Collection can be filtered using the `-\u003efilter(\\Closure $condition)` method. The given closure **MUST** return a boolean value.\n\nThe condition closure is called for every element in the collection. Any elements for which the closure returns `true` will be kept\nand put in a new `Collection` instance, which will be returned. For example, to filter out all elements which are NOT `foo`:\n\n```php\n$validationClosure = function ($value)\n{\n    return is_string($value);\n};\n\n$initialItems = ['foo', 'bar', 'baz', 'foo', 'bar', 'baz'];\n\n$collection = new \\Yoshi2889\\Collections\\Collection($validationClosure, $initialItems);\n\n$filterClosure = function ($value)\n{\n    return $value != 'foo';\n};\n\n// Will contain: ['bar', 'baz', 'bar', 'baz']\n$newCollection = $collection-\u003efilter($filterClosure);\n```\n\n### Events\nA Collection will emit a `changed` event if data gets added to or removed from it. No arguments are passed. \nThis functionality is borrowed from [Événement](https://github.com/igorw/evenement) by _igorw_.\n\nYou can subscribe to the event using the `-\u003eon($event, callable $listener)` method, like so:\n\n```php\n$collection-\u003eon('changed', function ()\n{\n    echo 'The collection changed!';\n});\n```\n\n## Validation Closures\nYou can find a lot of closures and utilities which can be used by a Collection in the separate [validation-closures project](https://github.com/Yoshi2889/validation-closures).\n\n## License\nThis code is released under the MIT License. Please see `LICENSE` to read it.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanosector%2Fcollections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanosector%2Fcollections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanosector%2Fcollections/lists"}