PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 1.4.2
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v1.4.2
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / library / ostraining / embera / Lib / Embera / Adapters / Service.php
embedpress / library / ostraining / embera / Lib / Embera / Adapters Last commit date
Service.php 9 years ago
Service.php
193 lines
1 <?php
2 /**
3 * Service.php
4 *
5 * @package Adapters
6 * @author Michael Pratt <pratt@hablarmierda.net>
7 * @link http://www.michael-pratt.com/
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace Embera\Adapters;
14
15 /**
16 * This is an abstract class and all Service/Providers should extend it.
17 */
18 abstract class Service
19 {
20 /**
21 * @var mixed Instance of \Embera\Url
22 * (Marking this as mixed instead of object because scrutinizer complains about type check on preg_match)
23 */
24 protected $url;
25
26 /** @var object Instance of \Embera\Oembed */
27 protected $oembed = null;
28
29 /** @var array Associative array with config/parameters to be sent to the oembed provider */
30 protected $config = array();
31
32 /** @var array Array with all the errors */
33 protected $errors = array();
34
35 /** @var string The api url for the current service */
36 protected $apiUrl = null;
37
38 /**
39 * Validates that the url belongs to this service.
40 * Should be implemented on all children and should
41 * return a boolean (preg_match returns 0 or 1 that
42 * is why I'm also allowing 'int' as a return type).
43 *
44 * The current url is made available via $this->url
45 *
46 * @return bool|int
47 */
48 abstract protected function validateUrl();
49
50 /**
51 * Construct
52 *
53 * @param string $url
54 * @param array $config
55 * @param object $oembed
56 * @return void
57 *
58 * @throws InvalidArgumentException when the given url doesnt match the current service
59 */
60 public function __construct($url, array $config = array(), \Embera\Oembed $oembed)
61 {
62 $this->url = new \Embera\Url($url);
63 $this->normalizeUrl();
64
65 if (!$this->validateUrl()) {
66 throw new \InvalidArgumentException('Url ' . $this->url . ' seems to be invalid for the ' . get_class($this) . ' service');
67 }
68
69 $this->config = array_replace_recursive(array(
70 'params' => array(
71 'maxwidth' => 0,
72 'maxheight' => 0,
73 )
74 ), $config);
75
76 $this->oembed = $oembed;
77 }
78
79 /**
80 * Gets the information from an Oembed provider
81 * when this fails, it tries to provide a fakeResponse.
82 * Returns an associative array with a (common) Oembed response.
83 *
84 * @return array
85 */
86 public function getInfo()
87 {
88 try {
89
90 if ($res = $this->oembed->getResourceInfo($this->config['oembed'], $this->apiUrl, (string) $this->url, $this->config['params'])) {
91 return $this->modifyResponse($res);
92 }
93
94 } catch (\Exception $e) {
95 $this->errors[] = $e->getMessage();
96 }
97
98 /**
99 * Use fakeResponses when the oembed setting is null or false
100 * If the oembed config is true, the user strictly wants real responses
101 */
102 if (!$this->config['oembed'] && $response = $this->fakeResponse()) {
103 $fakeResponse = new \Embera\FakeResponse($this->config, $response);
104 return $this->modifyResponse($fakeResponse->buildResponse());
105 }
106
107 return array();
108 }
109
110 /**
111 * Appends custom parameters for the oembed request
112 *
113 * @param array $params
114 * @return void
115 */
116 public function appendParams(array $params = array())
117 {
118 $this->config['params'] = array_merge($this->config['params'], $params);
119 }
120
121 /**
122 * Returns the url
123 *
124 * @return string
125 */
126 public function getUrl()
127 {
128 return (string) $this->url;
129 }
130
131 /**
132 * Returns an array with all the parameters for the oembed request
133 *
134 * @return array
135 */
136 public function getParams()
137 {
138 return $this->config['params'];
139 }
140
141 /**
142 * Returns an array with found errors
143 *
144 * @return array
145 */
146 public function getErrors()
147 {
148 return $this->errors;
149 }
150
151 /**
152 * This method fakes a Oembed response.
153 *
154 * It should be overwritten by the service
155 * itself if the service is capable to determine
156 * an html embed code based on the url or by other methods.
157 *
158 * @return array with data that the oembed response should have
159 * @codeCoverageIgnore
160 */
161 public function fakeResponse()
162 {
163 return array();
164 }
165
166 /**
167 * Normalizes a url.
168 * This method should be overwritten by the
169 * service itself, if needed.
170 *
171 * Use the $this->url property to do the job
172 *
173 * @return void
174 */
175 protected function normalizeUrl() {}
176
177 /**
178 * Gives the ability to modify the response/fake-response array
179 * from an oembed provider.
180 *
181 * It should be overwritten by the service when needed
182 *
183 * @param array $response
184 * @return array
185 */
186 protected function modifyResponse(array $response = array())
187 {
188 return $response;
189 }
190 }
191
192 ?>
193