PEAR2_Services_NYTimes-0.1.0PEAR2_Services_NYTimes-0.1.0/php/PEAR2/Services/NYTimes/Base.php

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
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
 * PEAR2\Services\NYTimes\Base
 *
 * PHP version 5
 *
 * @category  Services
 * @package   PEAR2_Services_NYTimes
 * @author    Till Klampaeckel <till@php.net>
 * @copyright 2011 Till Klampaeckel
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      https://github.com/pear2/Services_NYTimes
 */

/**
 * Base class for PEAR2_Services_NYTimes
 *
 * This is implemented by each API.
 *
 * @category  Services
 * @package   PEAR2_Services_NYTimes
 * @author    Till Klampaeckel <till@php.net>
 * @copyright 2011 Till Klampaeckel
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link      https://github.com/pear2/Services_NYTimes
 * @link      http://developer.nytimes.com/attribution
 */
namespace PEAR2\Services\NYTimes;
abstract class Base
{
    protected $format = 'json';

    /**
     * @var string $key The API key.
     * @see http://developer.nytimes.com/apps/register
     */
    protected $key;

    /**
     * @var \HTTP_Request2 $req
     */
    protected $req;

    /**
     * __construct()
     *
     * @param string $key The API key.
     *
     * @return $this
     */
    public function __construct($key)
    {
        $this->key = $key;
    }

    /**
     * Acceptor pattern.
     *
     * @param mixed $mixed
     *
     * @return $this
     */
    public function accept($mixed)
    {
        if ($mixed instanceof \HTTP_Request2) {
            $this->req = $mixed;
            return $this;
        }
        throw new \DomainException("Problem?");
    }

    /**
     * This set of classes will support a distinct version of each API. This method
     * allows the developer to return the API's version programmatically.
     *
     * @return string
     * @see    parent::$apiVersion
     */
    public function getApiVersion()
    {
        return $this->apiVersion;
    }

    /**
     * Set another response format.
     *
     * @param string $format
     *
     * @return $this
     * @throws \InvalidArgumentException
     * @uses   self::$format
     * @see    parent::makeRequest()
     */
    public function setResponseFormat($format)
    {
        static $supported = array('json', 'xml', 'sphp');
        if (!in_array($format, $supported)) {
            throw new \InvalidArgumentException("Format {$format} is not supported.");
        }
        $this->format = $format;
        return $this;
    }

    /**
     * Determine if the response is valid.
     *
     * @param \HTTP_Request2_Response $response
     *
     * @return boolean
     */
    protected function isSuccessful(\HTTP_Request2_Response $response)
    {
        if ($response->getStatus() == 200) {
            return true;
        }
        return false;
    }

    /**
     * Determine the problem.
     *
     * @return void
     */
    protected function hazProblem(\HTTP_Request2_Response $response)
    {
        switch ($response->getStatus) {
        case 400:
            throw new \RuntimeException("Bad request.");
        case 403:
            throw new \RuntimeException("You seem to be rate-limited.");
        case 404:
            throw new \LogicException("Resource does not exist.");
        case 500:
            throw new \RuntimeException("Please try again later.");
        default:
            throw new \DomainException("An error occurred: {$response->getStatus()}");
        }
    }

    abstract protected function getUri();
    abstract protected function makeRequest($uri);
    abstract protected function parseResponse(\HTTP_Request2_Response $response);
}
EOF