1: <?php
2: /**
3: * This file is part of the Podcast Crawler package.
4: *
5: * Copyright (c) 2016 Dorian Neto
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10:
11: namespace PodcastCrawler\Provider;
12:
13: use PodcastCrawler\ProviderInterface;
14: use DateTime;
15: use SimpleXMLElement;
16:
17: /**
18: * Class Podcastcrawler\Provider\DigitalPodcast
19: *
20: * @version v1.1.0
21: * @link https://github.com/podcastcrawler/podcastcrawler
22: * @license https://github.com/podcastcrawler/podcastcrawler/blob/master/LICENSE.md MIT
23: * @copyright 2016 Podcast Crawler
24: * @author Dorian Neto <doriansampaioneto@gmail.com>
25: */
26: class DigitalPodcast extends AbstractProvider implements ProviderInterface
27: {
28: /**
29: * The Application ID
30: *
31: * @var string
32: */
33: const APP_ID = "podcastsearchdemo";
34:
35: /**
36: * Base url to search by keyword
37: *
38: * @var string
39: */
40: const SEARCH_URL = "http://api.digitalpodcast.com/v2r/search";
41:
42: /**
43: * Specifies the kind of format the podcast search service produces
44: *
45: * @var string
46: */
47: const FORMAT = "rss";
48:
49: /**
50: * The number of results to return
51: *
52: * @var int
53: */
54: private $limit = 15;
55:
56: /**
57: * Array with default query string values to implement in base url
58: *
59: * @var string
60: */
61: private $defaultQuery = null;
62:
63: /**
64: * The construct of the object
65: */
66: public function __construct()
67: {
68: $this->setDefaultQuery();
69: }
70:
71: /**
72: * Returns the limit for search
73: *
74: * @return int
75: */
76: public function getLimit()
77: {
78: return $this->limit;
79: }
80:
81: /**
82: * Set the limit for search
83: *
84: * @param int $limit The limit
85: */
86: public function setLimit($limit)
87: {
88: // `-1` being used here because the API returns 3 items when `results=2`.
89: $this->limit = ((int) $limit - 1);
90: }
91:
92: /**
93: * Set default URL query for search
94: */
95: public function setDefaultQuery()
96: {
97: $this->defaultQuery = http_build_query([
98: 'results' => $this->limit,
99: 'appid' => self::APP_ID,
100: 'format' => self::FORMAT
101: ]);
102: }
103:
104: /**
105: * Generate an URL to enable the searching
106: *
107: * @param string $value The keyword to be searching
108: * @return string
109: */
110: public function generateUrl($value)
111: {
112: $value = urlencode($value);
113: $url = self::SEARCH_URL . "?keywords={$value}";
114:
115: return $url . '&' . $this->defaultQuery;
116: }
117:
118: /**
119: * Structuring the response from API through the search
120: *
121: * @param array $response Response from API
122: * @return array
123: */
124: public function build(array $response)
125: {
126: $xml = new SimpleXMLElement($response['search']);
127: $xml = $xml->channel;
128:
129: $output['result_count'] = count($xml->item);
130:
131: foreach($xml->item as $value) {
132: $output['podcasts'][] = [
133: 'title' => $value->title,
134: 'rss' => (string) $value->source,
135: 'link' => (string) $value->link,
136: ];
137: }
138:
139: return $output;
140: }
141: }
142: