Skip to content

Commit 192574d

Browse files
committed
new methods getImagesUrls() and getProviderIconsUrls() to fix #50
1 parent e8ac0e9 commit 192574d

18 files changed

+188
-80
lines changed

demo/index.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,24 @@ function printCode($code, $asHtml = true)
6060
echo $code;
6161
}
6262

63-
echo '<pre>'.htmlspecialchars($code, ENT_IGNORE).'</pre>';
63+
if ($code) {
64+
echo '<pre>'.htmlspecialchars($code, ENT_IGNORE).'</pre>';
65+
}
6466
}
6567

6668
$providerData = [
6769
'title' => 'printText',
6870
'description' => 'printText',
6971
'url' => 'printUrl',
7072
'type' => 'printText',
71-
'images' => 'printArray',
73+
'imagesUrls' => 'printArray',
7274
'code' => 'printCode',
7375
'source' => 'printUrl',
7476
'width' => 'printText',
7577
'height' => 'printText',
7678
'authorName' => 'printText',
7779
'authorUrl' => 'printUrl',
78-
'providerIcons' => 'printArray',
80+
'providerIconsUrls' => 'printArray',
7981
'providerName' => 'printText',
8082
'providerUrl' => 'printUrl',
8183
'publishedTime' => 'printText'
@@ -88,10 +90,12 @@ function array_insert (&$array, $position, $insert_array) {
8890
$array = array_merge($first_array, $insert_array, $array);
8991
}
9092

93+
array_insert($adapterData, 4, ['images' => 'printArray']);
9194
array_insert($adapterData, 4, ['image' => 'printImage']);
9295
array_insert($adapterData, 5, ['imageWidth' => 'printText']);
9396
array_insert($adapterData, 6, ['imageHeight' => 'printText']);
9497
array_insert($adapterData, 12, ['aspectRatio' => 'printText']);
98+
array_insert($adapterData, 15, ['providerIcons' => 'printArray']);
9599
array_insert($adapterData, 15, ['providerIcon' => 'printImage']);
96100
?>
97101

@@ -150,6 +154,17 @@ function array_insert (&$array, $position, $insert_array) {
150154
</div>
151155

152156
<div id="advanced-data">
157+
<?php if (isset($info->api)): ?>
158+
<h2>Data provider by the custom API</h2>
159+
160+
<table>
161+
<tr>
162+
<th>Data provider by the API</th>
163+
<td><?php printArray($info->api->getAll(), false); ?></td>
164+
</tr>
165+
</table>
166+
<?php endif ?>
167+
153168
<?php foreach ($info->getAllProviders() as $providerName => $provider): ?>
154169
<h2><?php echo $providerName; ?> provider</h2>
155170

demo/styles.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ img {
1515
display: block;
1616
margin-bottom: 0.5em;
1717
}
18+
pre {
19+
overflow: auto;
20+
background: #EEE;
21+
padding: 1em;
22+
}
1823

1924
/* form */
2025
form {
@@ -81,7 +86,7 @@ section h2 {
8186

8287
table {
8388
text-align: left;
84-
width: 100vw;
89+
width: 100%;
8590
table-layout: fixed;
8691
}
8792
th, td {

src/Adapters/Adapter.php

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ public function __get($name)
151151
*/
152152
public function getTitle()
153153
{
154-
return Utils::getData($this->providers, 'title') ?: $this->request->url->getUrl();
154+
return Utils::getFirstData($this->providers, 'title') ?: $this->request->url->getUrl();
155155
}
156156

157157
/**
158158
* {@inheritdoc}
159159
*/
160160
public function getDescription()
161161
{
162-
return Utils::getData($this->providers, 'description');
162+
return Utils::getFirstData($this->providers, 'description');
163163
}
164164

165165
/**
@@ -173,7 +173,7 @@ public function getType()
173173
return 'video';
174174
}
175175

176-
if (($type = Utils::getData($this->providers, 'type', false)) && ($type !== 'link')) {
176+
if (($type = Utils::getMostPopular(Utils::getAllData($this->providers, 'type'))) && ($type !== 'link')) {
177177
return $type;
178178
}
179179

@@ -193,17 +193,15 @@ public function getType()
193193
*/
194194
public function getSource()
195195
{
196-
if (($source = Utils::getData($this->providers, 'source', true))) {
197-
return $this->request->url->getAbsolute($source);
198-
}
196+
return Utils::getFirstUrlData($this->request->url, $this->providers, 'source');
199197
}
200198

201199
/**
202200
* {@inheritdoc}
203201
*/
204202
public function getCode()
205203
{
206-
if ($code = Utils::getData($this->providers, 'code')) {
204+
if ($code = Utils::getFirstData($this->providers, 'code')) {
207205
if (strpos($code, '</iframe>') !== false) {
208206
return preg_replace('|^.*(<iframe.*</iframe>).*$|Us', '$1', $code);
209207
}
@@ -225,48 +223,44 @@ public function getCode()
225223
*/
226224
public function getUrl()
227225
{
228-
if (($url = Utils::getData($this->providers, 'url', true))) {
229-
return $this->request->url->getAbsolute($url);
230-
}
231-
232-
return $this->request->url->getUrl();
226+
return Utils::getFirstUrlData($this->request->url, $this->providers, 'url') ?: $this->request->url->getUrl();
233227
}
234228

235229
/**
236230
* {@inheritdoc}
237231
*/
238232
public function getAuthorName()
239233
{
240-
return Utils::getData($this->providers, 'authorName');
234+
return Utils::getFirstData($this->providers, 'authorName');
241235
}
242236

243237
/**
244238
* {@inheritdoc}
245239
*/
246240
public function getAuthorUrl()
247241
{
248-
if (($url = Utils::getData($this->providers, 'authorUrl', true))) {
249-
return $this->request->url->getAbsolute($url);
250-
}
242+
return Utils::getFirstUrlData($this->request->url, $this->providers, 'authorUrl');
251243
}
252244

253245
/**
254246
* {@inheritdoc}
255247
*/
256-
public function getProviderIcons()
248+
public function getProviderIconsUrls()
257249
{
258-
$icons = [];
259-
260-
foreach ($this->getAllProviders() as $provider) {
261-
foreach ($provider->getProviderIcons() as $icon) {
262-
$icons[] = $this->request->url->getAbsolute($icon);
263-
}
264-
}
250+
$icons = Utils::getAllUrlData($this->request->url, $this->providers, 'providerIconsUrls');
265251

266252
$icons[] = $this->request->url->getAbsolute('/favicon.ico');
267253
$icons[] = $this->request->url->getAbsolute('/favicon.png');
268254

269-
return call_user_func("{$this->imageClass}::getImagesInfo", array_unique($icons), $this->imageConfig);
255+
return array_unique($icons);
256+
}
257+
258+
/**
259+
* {@inheritdoc}
260+
*/
261+
public function getProviderIcons()
262+
{
263+
return call_user_func("{$this->imageClass}::getImagesInfo", $this->getProviderIconsUrls());
270264
}
271265

272266
/**
@@ -288,35 +282,31 @@ public function getProviderIcon()
288282
*/
289283
public function getProviderName()
290284
{
291-
return Utils::getData($this->providers, 'providerName') ?: $this->request->url->getDomain();
285+
return Utils::getFirstData($this->providers, 'providerName') ?: $this->request->url->getDomain();
292286
}
293287

294288
/**
295289
* {@inheritdoc}
296290
*/
297291
public function getProviderUrl()
298292
{
299-
if (($url = Utils::getData($this->providers, 'providerUrl', true))) {
300-
return $this->request->url->getAbsolute($url);
301-
}
293+
return Utils::getFirstUrlData($this->request->url, $this->providers, 'providerUrl') ?: ($this->request->url->getScheme().'://'.$this->request->url->getDomain(true));
294+
}
302295

303-
return ($this->request->url->getScheme().'://'.$this->request->url->getDomain(true));
296+
/**
297+
* {@inheritdoc}
298+
*/
299+
public function getImagesUrls()
300+
{
301+
return Utils::getAllUrlData($this->request->url, $this->providers, 'imagesUrls');
304302
}
305303

306304
/**
307305
* {@inheritdoc}
308306
*/
309307
public function getImages()
310308
{
311-
$images = [];
312-
313-
foreach ($this->getAllProviders() as $k => $provider) {
314-
foreach ($provider->getImages() as $image) {
315-
$images[] = $this->request->url->getAbsolute($image);
316-
}
317-
}
318-
319-
return call_user_func("{$this->imageClass}::getImagesInfo", array_unique($images), $this->imageConfig);
309+
return call_user_func("{$this->imageClass}::getImagesInfo", $this->getImagesUrls(), $this->imageConfig);
320310
}
321311

322312
/**
@@ -368,15 +358,15 @@ public function getImageHeight()
368358
*/
369359
public function getWidth()
370360
{
371-
return Utils::getData($this->providers, 'width');
361+
return Utils::getFirstData($this->providers, 'width');
372362
}
373363

374364
/**
375365
* {@inheritdoc}
376366
*/
377367
public function getHeight()
378368
{
379-
return Utils::getData($this->providers, 'height');
369+
return Utils::getFirstData($this->providers, 'height');
380370
}
381371

382372
/**
@@ -397,6 +387,6 @@ public function getAspectRatio()
397387
*/
398388
public function getPublishedTime()
399389
{
400-
return Utils::getData($this->providers, 'publishedTime');
390+
return Utils::getFirstData($this->providers, 'publishedTime');
401391
}
402392
}

src/Adapters/AdapterInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function __construct(Request $request, array $config = null);
3131
*/
3232
public function run();
3333

34+
/**
35+
* Gets all icon provider urls found
36+
* It returns also the width, height and mime-type
37+
*
38+
* @return array
39+
*/
40+
public function getProviderIcons();
41+
3442
/**
3543
* Gets the best icon provider
3644
* if $config['getBiggerIcon'] is true, returns the bigger image found
@@ -40,6 +48,14 @@ public function run();
4048
*/
4149
public function getProviderIcon();
4250

51+
/**
52+
* Gets all images found in the webpage
53+
* It returns also the width, height and mime-type
54+
*
55+
* @return array
56+
*/
57+
public function getImages();
58+
4359
/**
4460
* Gets the best image
4561
* if $config['getBiggerImage'] is true, returns the biggest image

src/Adapters/Archive.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function getAuthorName()
120120
/**
121121
* {@inheritdoc}
122122
*/
123-
public function getImages()
123+
public function getImagesUrls()
124124
{
125125
$images = [];
126126

src/Adapters/Facebook.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ public function getSource()
173173
/**
174174
* {@inheritdoc}
175175
*/
176-
public function getImages()
176+
public function getImagesUrls()
177177
{
178-
$images = parent::getImages();
178+
$images = parent::getImagesUrls();
179179

180180
$cover = $this->api->get('cover');
181181

src/Adapters/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public function getCode()
7979
/**
8080
* {@inheritdoc}
8181
*/
82-
public function getImages()
82+
public function getImagesUrls()
8383
{
8484
if ($this->getType() === 'photo') {
85-
return call_user_func("{$this->imageClass}::getImagesInfo", [$this->getUrl()], $this->imageConfig);
85+
return [$this->getUrl()];
8686
}
8787

8888
return [];

src/Adapters/Google.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getCode()
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function getImages()
40+
public function getImagesUrls()
4141
{
4242
return [];
4343
}

src/Adapters/Soundcloud.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public function getUrl()
6969
/**
7070
* {@inheritdoc}
7171
*/
72-
public function getImages()
72+
public function getImagesUrls()
7373
{
74-
$images = parent::getImages();
74+
$images = parent::getImagesUrls();
7575

7676
if (!$this->api->get('artwork_url') && ($img = $this->api->get('user', 'avatar_url'))) {
7777
array_unshift($images, str_replace('-large.jpg', '-t500x500.jpg', $img));

src/DataInterface.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ public function getAuthorName();
6565
public function getAuthorUrl();
6666

6767
/**
68-
* Gets all icon provider urls found
68+
* Gets the urls of all icons of the provider
69+
* Note: it doesn't check whether the image exists or not
6970
*
7071
* @return array
7172
*/
72-
public function getProviderIcons();
73+
public function getProviderIconsUrls();
7374

7475
/**
7576
* Gets the provider name
@@ -86,11 +87,12 @@ public function getProviderName();
8687
public function getProviderUrl();
8788

8889
/**
89-
* Gets all images found in the webpage
90+
* Gets the urls of all images found in the webpage
91+
* Note: it doesn't check whether the image exists or not
9092
*
9193
* @return array
9294
*/
93-
public function getImages();
95+
public function getImagesUrls();
9496

9597
/**
9698
* Gets the width of the embedded widget

src/Providers/Facebook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getType()
6060
/**
6161
* {@inheritdoc}
6262
*/
63-
public function getImages()
63+
public function getImagesUrls()
6464
{
6565
$images = [];
6666

src/Providers/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function getProviderIcons()
107107
/**
108108
* {@inheritdoc}
109109
*/
110-
public function getImages()
110+
public function getImagesUrls()
111111
{
112112
$images = (array) $this->bag->get('images');
113113

0 commit comments

Comments
 (0)