wpsy/Entity/PostCollection.php line 54

Open in your IDE?
  1. <?php
  2. namespace Metabolism\WordpressBundle\Entity;
  3. use ArrayIterator;
  4. use Metabolism\WordpressBundle\Factory\PostFactory;
  5. use Metabolism\WordpressBundle\Service\PaginationService;
  6. /**
  7.  * Class Metabolism\WordpressBundle Framework
  8.  */
  9. class PostCollection implements \IteratorAggregate\Countable {
  10.     public $query;
  11.     protected $items;
  12.     protected $pagination;
  13.     /**
  14.      * @param $query
  15.      */
  16.     public function __construct($query)
  17.     {
  18.         $this->query = new \WP_Query$query );
  19.         $items = [];
  20.         foreach ($this->query->posts as $post)
  21.             $items[] = PostFactory::create$post );
  22.         $this->items array_filter($items);
  23.     }
  24.     /**
  25.      * @param $args
  26.      * @return array
  27.      */
  28.     public function getPagination($args=[]){
  29.         if( is_null($this->pagination) ){
  30.             $paginationService = new PaginationService();
  31.             $this->pagination $paginationService->build($args);
  32.         }
  33.         return $this->pagination;
  34.     }
  35.     /**
  36.      * @return ArrayIterator
  37.      */
  38.     public function getIterator() {
  39.         return new ArrayIterator($this->items);
  40.     }
  41.     /**
  42.      * Get total post count
  43.      *
  44.      * @return int
  45.      */
  46.     public function count()
  47.     {
  48.         return $this->query->found_posts;
  49.     }
  50. }