Skip to content

Commit c8a03fd

Browse files
committed
fixed tests, added suppport for google maps
1 parent 72a788a commit c8a03fd

File tree

6 files changed

+110
-10
lines changed

6 files changed

+110
-10
lines changed

src/Adapters/Google.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,46 @@
77

88
use Embed\Request;
99
use Embed\Utils;
10+
use Embed\Url;
11+
use Embed\Providers\Api;
1012

1113
class Google extends Webpage implements AdapterInterface
1214
{
15+
protected $mode;
16+
1317
/**
1418
* {@inheritdoc}
1519
*/
1620
public static function check(Request $request)
1721
{
1822
return $request->isValid() && $request->match([
19-
//'https://maps.google.*',
20-
//'https://www.google.*/maps*',
23+
'https://maps.google.*',
24+
'https://www.google.*/maps*',
2125
'https://drive.google.com/file/*/view',
2226
]);
2327
}
2428

29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function run()
33+
{
34+
if ($this->request->match('*/maps/*')) {
35+
$this->addProvider('google', new Api\GoogleMaps());
36+
}
37+
38+
parent::run();
39+
}
40+
2541
/**
2642
* {@inheritdoc}
2743
*/
2844
public function getCode()
2945
{
46+
if (($google = $this->getProvider('google'))) {
47+
return $google->getCode();
48+
}
49+
3050
$url = $this->request->createUrl()
3151
->withDirectoryPosition(3, 'preview')
3252
->withQueryParameters([]);

src/Providers/Api/GoogleMaps.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Embed\Providers\Api;
4+
5+
use Embed\Utils;
6+
use Embed\Providers\Provider;
7+
use Embed\Providers\ProviderInterface;
8+
9+
/**
10+
* Provider to use the API of google
11+
*/
12+
class GoogleMaps extends Provider implements ProviderInterface
13+
{
14+
protected $mode;
15+
protected $config = [
16+
'key' => null,
17+
];
18+
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function run()
23+
{
24+
$mode = $this->request->getDirectoryPosition(1);
25+
26+
switch ($mode) {
27+
case 'place':
28+
case 'dir':
29+
case 'search':
30+
$this->mode = $mode;
31+
break;
32+
}
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function getTitle()
39+
{
40+
if ($this->mode === 'place') {
41+
return $this->request->getDirectoryPosition(2);
42+
}
43+
44+
if ($this->mode === 'dir') {
45+
return $this->request->getDirectoryPosition(2).' / '.$this->request->getDirectoryPosition(3);
46+
}
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getCode()
53+
{
54+
if (empty($this->config['key'])) {
55+
return;
56+
}
57+
58+
switch ($this->mode) {
59+
case 'place':
60+
case 'search':
61+
return Utils::iframe($this->request->createUrl()
62+
->withPath('maps/embed/v1/'.$this->mode)
63+
->withQueryParameters([
64+
'q' => $this->request->getDirectoryPosition(2),
65+
'key' => $this->config['key']
66+
])
67+
);
68+
69+
case 'dir':
70+
return Utils::iframe($this->request->createUrl()
71+
->withPath('maps/embed/v1/directions')
72+
->withQueryParameters([
73+
'origin' => $this->request->getDirectoryPosition(2),
74+
'destination' => $this->request->getDirectoryPosition(3),
75+
'key' => $this->config['key']
76+
])
77+
);
78+
}
79+
}
80+
}

src/Url.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public function getSlicePath($offset, $length = null)
262262
*/
263263
public function getPath()
264264
{
265-
$path = !empty($this->info['path']) ? '/'.implode('/', $this->info['path']).'/' : '/';
265+
$path = !empty($this->info['path']) ? '/'.implode('/', array_map('urlencode', $this->info['path'])).'/' : '/';
266266

267267
if (isset($this->info['file'])) {
268268
$path .= $this->info['file'];
@@ -509,6 +509,8 @@ private function setPath($path)
509509

510510
if (isset($parts['dirname'])) {
511511
foreach (explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $parts['dirname'])) as $dir) {
512+
$dir = trim(urldecode($dir));
513+
512514
if ($dir !== '') {
513515
$this->info['path'][] = $dir;
514516
}

tests/BandcampTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function testOne()
1010
'title' => "Coquin L'éléphant, by Jean Jean",
1111
'description' => 'from the album Symmetry',
1212
'type' => 'video',
13-
'code' => '<iframe src="https://bandcamp.com/EmbeddedPlayer/v=2/track=2592086951/size=large/linkcol=0084B4/notracklist=true/twittercard=true/" frameborder="0" allowTransparency="true" style="border:none;overflow:hidden;width:350;height:467;"></iframe>',
13+
'code' => '<iframe src="https://bandcamp.com/EmbeddedPlayer/v=2/track=2592086951/size=large/tracklist=false/artwork=small/" frameborder="0" allowTransparency="true" style="border:none;overflow:hidden;width:400;height:120;"></iframe>',
1414
'width' => 400,
1515
'height' => 120,
1616
'providerName' => 'Jean Jean',

tests/FacebookTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testOne()
1414
fjs.parentNode.insertBefore(js, fjs);
1515
}(document, 'script', 'facebook-jssdk'));</script>
1616
17-
<div class="fb-post" data-href="https://www.facebook.com/permalink.php?story_fbid=827163017327807&id=149460691764713" data-width="500"></div>
17+
<div class="fb-post" data-href="https://www.facebook.com/asociaciondag/posts/827163017327807" data-width="500"></div>
1818
EOT;
1919

2020
$this->assertEmbed(

tests/GoogleTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
class GoogleTest extends TestCaseBase
44
{
5-
//the embed code for gmaps does not work. Need to investigate.
6-
public function noTestMap()
5+
public function testMap()
76
{
87
$this->assertEmbed(
98
'https://www.google.es/maps/place/Tordoia,+A+Coru%C3%B1a/@43.0871207,-8.5710004,12z/data=!3m1!4b1!4m2!3m1!1s0xd2ef4006f1ef489:0x404f58273ca55a0',
109
[
11-
'title' => 'Google Maps',
12-
'type' => 'rich',
13-
'code' => '<iframe src="https://www.google.es/maps/place/Tordoia,+A+Coru%C3%B1a/@43.0871207,-8.5710004,12z/data=!3m1!4b1!4m2!3m1!1s0xd2ef4006f1ef489:0x404f58273ca55a0?output=embed&amp;s=" frameborder="0" allowTransparency="true" style="border:none;overflow:hidden;width:600px;height:400px;"></iframe>',
10+
'title' => 'Tordoia, A Coruña',
11+
'type' => 'link',
1412
'providerName' => 'Google Maps',
1513
]
1614
);

0 commit comments

Comments
 (0)