1<?php
2/**
3 * This file is part of the Tmdb PHP API created by Michael Roterman.
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @package Tmdb
9 * @author Michael Roterman <michael@wtfz.net>
10 * @copyright (c) 2013, Michael Roterman
11 * @version 0.0.1
12 */
13namespace Tmdb\Repository;
14
15use Symfony\Component\EventDispatcher\EventDispatcher;
16use Tmdb\Api\ApiInterface;
17use Tmdb\Client;
18use Tmdb\Factory\AbstractFactory;
19use Tmdb\Model\Common\QueryParameter\QueryParameterInterface;
20
21/**
22 * Class AbstractRepository
23 * @package Tmdb\Repository
24 */
25abstract class AbstractRepository
26{
27    protected $client = null;
28    protected $api    = null;
29
30    /**
31     * Constructor
32     *
33     * @param Client $client
34     */
35    public function __construct(Client $client)
36    {
37        $this->client = $client;
38    }
39
40    /**
41     * Return the client
42     *
43     * @return Client
44     */
45    public function getClient()
46    {
47        return $this->client;
48    }
49
50    /**
51     * @return EventDispatcher
52     */
53    public function getEventDispatcher()
54    {
55        return $this->client->getEventDispatcher();
56    }
57
58    /**
59     * Process query parameters
60     *
61     * @param  array $parameters
62     * @return array
63     */
64    protected function parseQueryParameters(array $parameters = [])
65    {
66        foreach ($parameters as $key => $candidate) {
67            if (is_a($candidate, 'Tmdb\Model\Common\QueryParameter\QueryParameterInterface')) {
68                $interfaces = class_implements($candidate);
69
70                if (array_key_exists('Tmdb\Model\Common\QueryParameter\QueryParameterInterface', $interfaces)) {
71                    unset($parameters[$key]);
72
73                    $parameters[$candidate->getKey()] = $candidate->getValue();
74                }
75            }
76        }
77
78        return $parameters;
79    }
80
81    /**
82     * Return the API Class
83     *
84     * @return ApiInterface
85     */
86    abstract public function getApi();
87
88    /**
89     * Return the Factory Class
90     *
91     * @return AbstractFactory
92     */
93    abstract public function getFactory();
94}
95