🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-12-04 12:34:16.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-12-04 12:34:16.0 +00:00:00
commit
70b77c1f2b9acf18210e4db72ad162bbf8f418c4 [patch]
tree
d6315d31e182b31638e2d148ffc0eebf34178b73
parent
b69a23a6a743a456eaa78c7283f452e26c5734f7
download
70b77c1f2b9acf18210e4db72ad162bbf8f418c4.tar.gz

Comments rework to allow easier iteration and nested comments



Diff

 src/Models/Post.php                      |  40 +++++++-----
 src/Support/RecursiveCommentIterator.php | 111 ++++++++++++++++++++++++++++++++-
 2 files changed, 137 insertions(+), 14 deletions(-)

diff --git a/src/Models/Post.php b/src/Models/Post.php
index 9fa880f..773738f 100644
--- a/src/Models/Post.php
+++ b/src/Models/Post.php
@@ -9,7 +9,9 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Koselig\Exceptions\UnsatisfiedDependencyException;
use Koselig\Support\Action;
use Koselig\Support\RecursiveCommentIterator;
use Koselig\Support\Wordpress;
use RecursiveIteratorIterator;
use Watson\Rememberable\Rememberable;
use WP_Post;

@@ -29,6 +31,20 @@ class Post extends Model
    protected $prefix = DB_PREFIX;

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('published', function (Builder $builder) {
            $builder->where('post_status', 'publish');
        });
    }

    /**
     * Length of time to cache this model for.
     *
     * @var int
@@ -321,28 +337,24 @@ class Post extends Model
    }

    /**
     * Get the {@link WP_Post} instance for this Post.
     *
     * @deprecated Use the methods already provided by this model.
     * Get an iterator for all the comments in this post.
     *
     * @return WP_Post
     * @param int $flags flags to pass to the {@link RecursiveIteratorIterator}
     * @return RecursiveIteratorIterator
     */
    public function toWordpressPost()
    public function getCommentIterator($flags = RecursiveIteratorIterator::SELF_FIRST)
    {
        return new WP_Post((object) $this->toArray());
        return new RecursiveIteratorIterator(new RecursiveCommentIterator($this->comments), $flags);
    }

    /**
     * The "booting" method of the model.
     * Get the {@link WP_Post} instance for this Post.
     *
     * @return void
     * @deprecated Use the methods already provided by this model.
     * @return WP_Post
     */
    protected static function boot()
    public function toWordpressPost()
    {
        parent::boot();

        static::addGlobalScope('published', function (Builder $builder) {
            $builder->where('post_status', 'publish');
        });
        return new WP_Post((object) $this->toArray());
    }
}
diff --git a/src/Support/RecursiveCommentIterator.php b/src/Support/RecursiveCommentIterator.php
new file mode 100644
index 0000000..9545122
--- /dev/null
+++ b/src/Support/RecursiveCommentIterator.php
@@ -0,0 +1,111 @@
<?php
namespace Koselig\Support;

use Illuminate\Support\Collection;
use Koselig\Models\Comment;
use RecursiveIterator;

/**
 * Interface to allow easier looping over comments.
 *
 * @author Jordan Doyle <jordan@doyle.wf>
 */
class RecursiveCommentIterator implements RecursiveIterator
{
    private $current = 0;
    private $comments;

    /**
     * Create a new RecursiveCommentIterator instance.
     *
     * @param Collection|Comment[] $comments
     */
    public function __construct($comments)
    {
        $this->comments = $comments;
    }

    /**
     * Return the current element
     *
     * @link http://php.net/manual/en/iterator.current.php
     * @return Comment Can return any type.
     * @since 5.0.0
     */
    public function current()
    {
        return $this->comments[$this->current];
    }

    /**
     * Move forward to next element
     *
     * @link http://php.net/manual/en/iterator.next.php
     * @return void Any returned value is ignored.
     * @since 5.0.0
     */
    public function next()
    {
        $this->current++;
    }

    /**
     * Return the key of the current element
     *
     * @link http://php.net/manual/en/iterator.key.php
     * @return mixed scalar on success, or null on failure.
     * @since 5.0.0
     */
    public function key()
    {
        return $this->current;
    }

    /**
     * Checks if current position is valid
     *
     * @link http://php.net/manual/en/iterator.valid.php
     * @return boolean The return value will be casted to boolean and then evaluated.
     * Returns true on success or false on failure.
     * @since 5.0.0
     */
    public function valid()
    {
        return isset($this->comments[$this->current]);
    }

    /**
     * Rewind the Iterator to the first element
     * @link http://php.net/manual/en/iterator.rewind.php
     * @return void Any returned value is ignored.
     * @since 5.0.0
     */
    public function rewind()
    {
        $this->current = 0;
    }

    /**
     * Returns if an iterator can be created for the current entry.
     *
     * @link http://php.net/manual/en/recursiveiterator.haschildren.php
     * @return bool true if the current entry can be iterated over, otherwise returns false.
     * @since 5.1.0
     */
    public function hasChildren()
    {
        return !$this->current()->children->isEmpty();
    }

    /**
     * Returns an iterator for the current entry.
     *
     * @link http://php.net/manual/en/recursiveiterator.getchildren.php
     * @return RecursiveIterator An iterator for the current entry.
     * @since 5.1.0
     */
    public function getChildren()
    {
        return new static($this->current()->children);
    }
}