Skip to content

Commit 2d71baa

Browse files
committed
Merge pull request #106 from zyuhel/master
Base Tags base functionality.
2 parents 3679557 + 87e1caf commit 2d71baa

File tree

11 files changed

+114
-4
lines changed

11 files changed

+114
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $info->title; //The page title
3939
$info->description; //The page description
4040
$info->url; //The canonical url
4141
$info->type; //The page type (link, video, image, rich)
42+
$info->tags; //The page keywords (tags)
4243

4344
$info->images; //List of all images found in the page
4445
$info->image; //The image choosen as main image

src/Adapters/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ public function getType()
201201

202202
return 'link';
203203
}
204+
205+
/**
206+
* {@inheritdoc}
207+
*/
208+
public function getTags()
209+
{
210+
return Utils::getAllValues(Utils::getData($this->providers, 'tags'));
211+
}
204212

205213
/**
206214
* {@inheritdoc}

src/Adapters/N500px.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ public function run()
5959
//order is important
6060
$this->addProvider('oembed', new Providers\OEmbed());
6161
$this->addProvider('opengraph', new Providers\OpenGraph());
62+
$this->addProvider('html', new Providers\Html());
63+
6264
}
6365
}

src/Bag.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ public function getAll()
9090
*/
9191
public function has($name)
9292
{
93+
if (strpos($name, '[') !== false) {
94+
$names = explode('[', str_replace(']', '', $name));
95+
$key = array_shift($names);
96+
$item = isset($this->parameters[$key]) ? $this->parameters[$key] : [];
97+
98+
foreach ($names as $key) {
99+
if (!isset($item[$key])) {
100+
return false;
101+
}
102+
103+
$item = $item[$key];
104+
}
105+
106+
return isset($item);
107+
}
108+
93109
return isset($this->parameters[$name]);
94110
}
95111
}

src/DataInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public function getDescription();
3030
*/
3131
public function getType();
3232

33+
/**
34+
* Gets the tags of the url
35+
*
36+
* @return array
37+
*/
38+
public function getTags();
39+
3340
/**
3441
* Gets the source url (feed, api, etc).
3542
*

src/Providers/Html.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public function getType()
6565
return $this->bag->has('video_src') ? 'video' : null;
6666
}
6767

68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function getTags()
72+
{
73+
return $this->bag->has('keywords') ? array_map('trim', explode(',',$this->bag->get('keywords'))) : [];
74+
}
75+
76+
6877
/**
6978
* {@inheritdoc}
7079
*/

src/Providers/OEmbed.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ public function getType()
101101
}
102102
}
103103

104+
/**
105+
* {@inheritdoc}
106+
*/
107+
public function getTags()
108+
{
109+
if ($this->bag->has('meta[keywords]')) {
110+
//it means we are using iframe.ly api
111+
return array_map('trim', explode(',',$this->bag->get('meta[keywords]')));
112+
}
113+
114+
return [];
115+
116+
117+
}
118+
104119
/**
105120
* {@inheritdoc}
106121
*/

src/Providers/Provider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public function getType()
4949
{
5050
}
5151

52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function getTags()
56+
{
57+
return [];
58+
}
5259
/**
5360
* {@inheritdoc}
5461
*/

src/Utils.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ public static function getMetas(\DOMDocument $html)
2121
foreach ($html->getElementsByTagName('meta') as $meta) {
2222
$name = trim(strtolower($meta->getAttribute('property') ?: $meta->getAttribute('name')));
2323
$value = $meta->getAttribute('content') ?: $meta->getAttribute('value');
24-
2524
$metas[] = [$name, $value, $meta];
2625
}
27-
2826
return $metas;
2927
}
3028

@@ -68,7 +66,6 @@ public static function getData(array $providers, $name, Url $url = null)
6866

6967
foreach ($providers as $key => $provider) {
7068
$value = $provider->$method();
71-
7269
if (empty($value)) {
7370
continue;
7471
}
@@ -92,7 +89,6 @@ public static function getData(array $providers, $name, Url $url = null)
9289
}
9390
}
9491
}
95-
9692
return array_values($values);
9793
}
9894

@@ -176,6 +172,28 @@ public static function getFirstValue(array $values, $returnKey = false)
176172
}
177173
}
178174

175+
176+
/**
177+
* Returns values as array
178+
*
179+
* @param array $values The array provided by self::getData()
180+
* @param bool $returnKey Whether or not return the key instead the value
181+
*
182+
* @return array
183+
*/
184+
public static function getAllValues(array $values, $returnKey = false)
185+
{
186+
if ($returnKey){
187+
return array_keys($values);
188+
}
189+
$return_value=[];
190+
foreach ($values as $value)
191+
{
192+
$return_value[]=$value['value'];
193+
}
194+
return $return_value;
195+
}
196+
179197
/**
180198
* Returns the most popular value in an array.
181199
*

tests/TestCaseBase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ protected function assertEmbed($url, array $info, array $config = array())
5656
case 'height':
5757
case 'imageWidth':
5858
case 'imageHeight':
59+
case 'tags':
5960
$this->assertSame($value, $i->$name);
6061
break;
6162

0 commit comments

Comments
 (0)