- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 151
Add Nette\Utils\Collection #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| From the user's point of view, why would/should one use this, compared e.g. to doctrine/collections (which are even more generic and actually behave like array thanks to ArrayAccess)? | 
| @dg Rebased @Majkl578 I don't use doctrine collections, but when I take a look at source... My collections used to look very similarly. Problem arises when you want to be strict on types. You cannot change the  public function add($element)
{
    if (!$element instanceof Person) throw ...
}
 $people[$person->username] = $person;
# vs.
$people->add($person);
$people[$person->username]
# vs.
$people->get($person->username)
unset($person[$person->username])
# vs.
# not implemented, I don't remove items one by one, usually by filter onlyAnd sometimes, IDE has a problem with "napovídání" (mi vypadl anglický termín) with ArrayAccess. | 
| I don't think this belongs to nette/utils. | 
| @JanTvrdik Partially agree. The best would be native typed arrays in the PHP itself. Collection is a typical part of a model layer, Nette does not have such. This is helper only without big ambitions. Real world example how I use it: return People::fromIterator(
    $this->dibi->query('...')->setRowClass(Person::class)
);But not just database. In one project, I'm listing firewall rules from router: $rules = new Firewall\Rules;
foreach ($this->switch->command(.....) as $line) {
    $rules->add(Firewall\Rule::fromCliFormat($line));
}The point is, that working with typehint  Btw. there used to be Collections in Nette, but this is different. | 
d116328    to
    3054b70      
    Compare
  
    e2486c4    to
    d9b729c      
    Compare
  
    496a5dc    to
    622864e      
    Compare
  
    | Since its WIP, rebased to some old commit. | 
3897bc7    to
    b6341f0      
    Compare
  
    a846fab    to
    736c567      
    Compare
  
    cd9170e    to
    2b48b24      
    Compare
  
    6f1918e    to
    be53471      
    Compare
  
    ba103e3    to
    ce70865      
    Compare
  
    9aff0b8    to
    e67c406      
    Compare
  
    a702672    to
    5ab770f      
    Compare
  
    99fbd2d    to
    8ee89b1      
    Compare
  
    6bf57ef    to
    eaaf169      
    Compare
  
    
I'm using this abstract collection really often. Its purpose is to emulate typed array in most cases.
May seem to be strange that methods like
get()oradd()are missing. It is because of PHP invariance limitation.An example of basic usage:
Collection can declare frequent helpers on self:
And usage as typed array:
The
IteratorAggreageteis here for ordinary loops, like:The
ArrayAccessis not implemented. I tried that, but never found it useful.The
normalizeKey()method can convert complex types to scalar, for example for multi column primary keys in database. On the other hand, I overloaded it very rarely.If this would be accepted, I'll add tests and doc.