1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 
<?php declare(strict_types = 1);
/**
 * This file is part of the Tmdb package.
 *
 * (c) Vincent Faliès <vincent@vfac.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author Vincent Faliès <vincent@vfac.fr>
 * @copyright Copyright (c) 2017
 */


namespace VfacTmdb\Items;

use VfacTmdb\Abstracts\Item;
use VfacTmdb\Interfaces\Items\CollectionInterface;

use VfacTmdb\Exceptions\NotFoundException;
use VfacTmdb\Traits\ElementTrait;
use VfacTmdb\Results;
use VfacTmdb\Interfaces\TmdbInterface;

/**
 * Class to manipulate a collection
 * @package Tmdb
 * @author Vincent Faliès <vincent@vfac.fr>
 * @copyright Copyright (c) 2017
 */
class Collection extends Item implements CollectionInterface
{
    use ElementTrait;

    /**
     * Configuration
     * @var \stdClass
     */
    protected $conf = null;
    /**
     * Id
     * @var int
     */
    protected $id = null;
    /**
     * Tmdb object
     * @var TmdbInterface
     */
    protected $tmdb = null;

    /**
     * Constructor
     * @param TmdbInterface $tmdb
     * @param int $collection_id
     * @param array $options
     */
    public function __construct(TmdbInterface $tmdb, int $collection_id, array $options = array())
    {
        parent::__construct($tmdb, $collection_id, $options, 'collection');

        $this->setElementTrait($this->data);
    }

    /**
     * Get collection ID
     * @return int
     */
    public function getId() : int
    {
        return $this->id;
    }

    /**
     * Get collection name
     * @return string
     * @throws NotFoundException
     */
    public function getName() : string
    {
        if (isset($this->data->name)) {
            return $this->data->name;
        }
        $this->logger->error('Collection name not found', array('collection_id' => $this->id));
        throw new NotFoundException;
    }

    /**
     * Get collection parts
     * @return \Generator
     */
    public function getParts() : \Generator
    {
        if (!empty($this->data->parts)) {
            foreach ($this->data->parts as $part) {
                $movie = new Results\Movie($this->tmdb, $part);
                yield $movie;
            }
        }
    }

    /**
     * Get collection backdrops
     * @return \Generator|Results\Image
     */
    public function getBackdrops() : \Generator
    {
        $params = [];
        $this->tmdb->checkOptionLanguage($this->params, $params);
        $data = $this->tmdb->getRequest('collection/' . (int) $this->id . '/images', $params);

        foreach ($data->backdrops as $b) {
            $image = new Results\Image($this->tmdb, $this->id, $b);
            yield $image;
        }
    }

    /**
     * Get collection posters
     * @return \Generator|Results\Image
     */
    public function getPosters() : \Generator
    {
        $params = [];
        $this->tmdb->checkOptionLanguage($this->params, $params);
        $data = $this->tmdb->getRequest('collection/' . (int) $this->id . '/images', $params);

        foreach ($data->posters as $b) {
            $image = new Results\Image($this->tmdb, $this->id, $b);
            yield $image;
        }
    }
}