{"action":"created","issue":{"url":"https://api.github.com/repos/danielgsims/php-collections/issues/61","repository_url":"https://api.github.com/repos/danielgsims/php-collections","labels_url":"https://api.github.com/repos/danielgsims/php-collections/issues/61/labels{/name}","comments_url":"https://api.github.com/repos/danielgsims/php-collections/issues/61/comments","events_url":"https://api.github.com/repos/danielgsims/php-collections/issues/61/events","html_url":"https://github.com/danielgsims/php-collections/issues/61","id":355564019,"node_id":"MDU6SXNzdWUzNTU1NjQwMTk=","number":61,"title":"Setter for collection type","user":{"login":"gnumoksha","id":696797,"node_id":"MDQ6VXNlcjY5Njc5Nw==","avatar_url":"https://avatars2.githubusercontent.com/u/696797?v=4","gravatar_id":"","url":"https://api.github.com/users/gnumoksha","html_url":"https://github.com/gnumoksha","followers_url":"https://api.github.com/users/gnumoksha/followers","following_url":"https://api.github.com/users/gnumoksha/following{/other_user}","gists_url":"https://api.github.com/users/gnumoksha/gists{/gist_id}","starred_url":"https://api.github.com/users/gnumoksha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gnumoksha/subscriptions","organizations_url":"https://api.github.com/users/gnumoksha/orgs","repos_url":"https://api.github.com/users/gnumoksha/repos","events_url":"https://api.github.com/users/gnumoksha/events{/privacy}","received_events_url":"https://api.github.com/users/gnumoksha/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-08-30T12:31:56Z","updated_at":"2018-08-30T13:18:21Z","closed_at":null,"author_association":"NONE","body":"At work we use this collections to improve type hinting. Example: \r\n\r\n```php\r\nuse \\Foo\\Bar\\UrlCollection;\r\n\r\nclass SomeClass\r\n{\r\n    /** @var \\Foo\\Bar\\UrlCollection */\r\n    private $urls;\r\n\r\n    public function __construct(UrlCollection $urls)\r\n    {\r\n        $this->urls = $urls;\r\n    }\r\n\r\n    public function getUrls() : UrlCollection\r\n    {\r\n        return $this->urls;\r\n    }\r\n}\r\n```\r\n```php\r\n$config = new SomeClass(new UrlCollection(Url::class, $items));\r\n```\r\nBut we always need to declare the collection type, which is strange because we have a class for the collection and it should know his type.\r\n\r\nAs result we ended up with this abstract class:\r\n```php\r\ndeclare(strict_types=1);\r\n\r\nnamespace Foo\\Bar;\r\n\r\nuse Collections\\Collection;\r\n\r\nabstract class SmartTypeCollection extends Collection\r\n{\r\n    /**\r\n     * Creates a new collection with only $typeOrItems parameter.\r\n     * @param string|mixed[] $typeOrItems you will pass only this parameter that contains you data in an array.\r\n     * @param mixed[]        $items this parameter will be passed only by Collection class.\r\n     * @throws \\Collections\\Exceptions\\InvalidArgumentException\r\n     */\r\n    public function __construct($typeOrItems, ?array $items = [])\r\n    {\r\n        if (\\is_string($typeOrItems)) {\r\n            $type = $typeOrItems;\r\n        } else {\r\n            $type = $this->getSmartType();\r\n        }\r\n\r\n        if (\\is_array($typeOrItems)) {\r\n            $items = $typeOrItems;\r\n        }\r\n\r\n        parent::__construct($type, $items);\r\n    }\r\n\r\n    /**\r\n     * This method will return a string specifying the type for the collection's items.\r\n     */\r\n    abstract protected function getSmartType() : string;\r\n}\r\n```\r\nWich allow us to do this:\r\n\r\n```php\r\nnamespace \\Foo\\Bar;\r\n\r\nclass UrlCollection extends SmartTypeCollection\r\n{\r\n    /** {@inheritdoc} */\r\n    protected function getSmartType() : string\r\n    {\r\n        return \\Foo\\Bar\\Url::class;\r\n    }\r\n}\r\n```\r\n```php\r\n$config = new SomeClass(new UrlCollection($items));\r\n```\r\n\r\nI'm wondering if it makes sense to you, and if so, there is a way to add this feature into this project."},"comment":{"url":"https://api.github.com/repos/danielgsims/php-collections/issues/comments/417315958","html_url":"https://github.com/danielgsims/php-collections/issues/61#issuecomment-417315958","issue_url":"https://api.github.com/repos/danielgsims/php-collections/issues/61","id":417315958,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNzMxNTk1OA==","user":{"login":"gnumoksha","id":696797,"node_id":"MDQ6VXNlcjY5Njc5Nw==","avatar_url":"https://avatars2.githubusercontent.com/u/696797?v=4","gravatar_id":"","url":"https://api.github.com/users/gnumoksha","html_url":"https://github.com/gnumoksha","followers_url":"https://api.github.com/users/gnumoksha/followers","following_url":"https://api.github.com/users/gnumoksha/following{/other_user}","gists_url":"https://api.github.com/users/gnumoksha/gists{/gist_id}","starred_url":"https://api.github.com/users/gnumoksha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gnumoksha/subscriptions","organizations_url":"https://api.github.com/users/gnumoksha/orgs","repos_url":"https://api.github.com/users/gnumoksha/repos","events_url":"https://api.github.com/users/gnumoksha/events{/privacy}","received_events_url":"https://api.github.com/users/gnumoksha/received_events","type":"User","site_admin":false},"created_at":"2018-08-30T13:18:21Z","updated_at":"2018-08-30T13:18:21Z","author_association":"NONE","body":"@danielgsims the first option :)"}}
{"actor":{"display_login":"gnumoksha"}}