Skip to content

Instantly share code, notes, and snippets.

@ftdebugger
Created June 19, 2013 10:18

Revisions

  1. ftdebugger created this gist Jun 19, 2013.
    81 changes: 81 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    <?php
    /**
    * @author Evgeny Shpilevsky <[email protected]>
    */

    class Post
    {

    /**
    * @var string
    */
    protected $title;

    /**
    * @param string $title
    */
    public function setTitle($title)
    {
    $this->title = $title;
    }

    /**
    * @return string
    */
    public function getTitle()
    {
    return $this->title;
    }

    }

    class PostContainer implements IteratorAggregate
    {

    /**
    * @var Post[]
    */
    protected $posts;

    /**
    * @var int
    */
    protected $archiveCount;

    /**
    * @param Post[] $posts
    * @param int $archiveCount
    */
    public function __construct(array $posts, $archiveCount)
    {
    $this->posts = $posts;
    $this->archiveCount = $archiveCount;
    }

    /**
    * @return int
    */
    public function getArchiveCount()
    {
    return $this->archiveCount;
    }

    /**
    * @return ArrayIterator|Traversable
    */
    public function getIterator()
    {
    return new ArrayIterator($this->posts);
    }

    }

    $news = new PostContainer($this->getService()->getLatestNews(), $this->getService()->getArchiveCount());

    // где-то
    foreach ($news as $post) {
    echo $post->getTitle();
    // something else
    }

    echo $news->getArchiveCount();