Created
September 22, 2010 21:32
-
-
Save ornicar/592626 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } |
Author
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
ok, why can't you do:
to get rid of
getCommentCollectionthat way your comments will always behave the same way.