Skip to content

Instantly share code, notes, and snippets.

@ornicar
Created September 22, 2010 21:32
Show Gist options
  • Select an option

  • Save ornicar/592626 to your computer and use it in GitHub Desktop.

Select an option

Save ornicar/592626 to your computer and use it in GitHub Desktop.
<?php
class Article extends Content
{
protected $comments; // a Collection implementation, ArrayCollection or PersistentCollection
// get the ArrayCollection or PersistentCollection
public function getComments()
{
return $this->comments;
}
// get the CommentCollection to access custom methods.
public function getCommentCollection()
{
return new CommentCollection($this->getComments());
}
}
class CommentCollection
{
protected $collection;
public function __construct(Collection $collection)
{
$this->collection = $collection;
}
public function getLastComments($limit = null)
{
$lastComments= array_reverse($this->collection->toArray());
if($limit) {
return array_slice($lastComments,0,$limit);
}
return $lastComments;
}
}
@avalanche123
Copy link
Copy Markdown

ok, why can't you do:

  public function getComments()
  {
    return new CommentCollection($this->comments);
  }

to get rid of getCommentCollection
that way your comments will always behave the same way.

@ornicar
Copy link
Copy Markdown
Author

ornicar commented Sep 24, 2010

Right. And ideally it should implement the Collection interface.
Maybe something like that:

class CommentCollection extends CollectionWrapper
{
    public function doFancyStuff()
    {
        // Do things with $this->collection
    }
}

class CollectionWrapper implements Collection
{
    protected $collection;

    public function __construct(Collection $collection)
    {
        $this->collection = $collection;
    }

    /* Implement here all Collection methods, forwarding logic to $this->collection */

    public function add($element)
    {
        return $this->collection->add($element);
    }

    /* And more... */
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment