Skip to content

Commit 83805bb

Browse files
committed
allow to suppress URL suffix for structured properties
1 parent dd3d4c2 commit 83805bb

7 files changed

+81
-41
lines changed

src/StructuredProperties/Audio.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,10 @@
22

33
namespace Astrotomic\OpenGraph\StructuredProperties;
44

5-
use Astrotomic\OpenGraph\BaseObject;
6-
7-
class Audio extends BaseObject
5+
class Audio extends StructuredProperty
86
{
97
protected const PREFIX = 'og:audio';
108

11-
public function __construct(string $url)
12-
{
13-
$this->setProperty(self::PREFIX, 'url', $url);
14-
}
15-
16-
public static function make(string $url)
17-
{
18-
return new static($url);
19-
}
20-
219
public function secureUrl(string $url)
2210
{
2311
$this->setProperty(self::PREFIX, 'secure_url', $url);

src/StructuredProperties/Image.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,10 @@
22

33
namespace Astrotomic\OpenGraph\StructuredProperties;
44

5-
use Astrotomic\OpenGraph\BaseObject;
6-
7-
class Image extends BaseObject
5+
class Image extends StructuredProperty
86
{
97
protected const PREFIX = 'og:image';
108

11-
public function __construct(string $url)
12-
{
13-
$this->setProperty(self::PREFIX, 'url', $url);
14-
}
15-
16-
public static function make(string $url)
17-
{
18-
return new static($url);
19-
}
20-
219
public function secureUrl(string $url)
2210
{
2311
$this->setProperty(self::PREFIX, 'secure_url', $url);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Astrotomic\OpenGraph\StructuredProperties;
4+
5+
use Astrotomic\OpenGraph\BaseObject;
6+
7+
abstract class StructuredProperty extends BaseObject
8+
{
9+
public function __construct(string $url, bool $withUrlSuffix = true)
10+
{
11+
if ($withUrlSuffix) {
12+
$this->setProperty(static::PREFIX, 'url', $url);
13+
} else {
14+
$prefix = explode(':', static::PREFIX, 2);
15+
$this->setProperty($prefix[0], $prefix[1], $url);
16+
}
17+
}
18+
19+
public static function make(string $url, bool $withUrlSuffix = true)
20+
{
21+
return new static($url, $withUrlSuffix);
22+
}
23+
}

src/StructuredProperties/Video.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,10 @@
22

33
namespace Astrotomic\OpenGraph\StructuredProperties;
44

5-
use Astrotomic\OpenGraph\BaseObject;
6-
7-
class Video extends BaseObject
5+
class Video extends StructuredProperty
86
{
97
protected const PREFIX = 'og:video';
108

11-
public function __construct(string $url)
12-
{
13-
$this->setProperty(self::PREFIX, 'url', $url);
14-
}
15-
16-
public static function make(string $url)
17-
{
18-
return new static($url);
19-
}
20-
219
public function secureUrl(string $url)
2210
{
2311
$this->setProperty(self::PREFIX, 'secure_url', $url);

tests/GlobalTypesTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Astrotomic\OpenGraph\StructuredProperties\Audio;
4+
use Astrotomic\OpenGraph\StructuredProperties\Image;
45
use Astrotomic\OpenGraph\Types\Article;
56
use Astrotomic\OpenGraph\Types\Book;
67
use Astrotomic\OpenGraph\Types\Profile;
@@ -35,13 +36,41 @@ public function __toString()
3536
assertMatchesHtmlSnapshot((string) $og);
3637
})->group('global', 'website');
3738

39+
it('can generate website tags with structured image', function () {
40+
$og = Website::make('Title | Example')
41+
->url('http://www.example.com')
42+
->description('Description')
43+
->locale('en_US')
44+
->alternateLocale('en_GB')
45+
->siteName('Example')
46+
->image(Image::make('http://www.example.com/image1.jpg')->mimeType('image/jpeg'));
47+
48+
assertMatchesHtmlSnapshot((string) $og);
49+
})->group('global', 'website');
50+
51+
it('can generate website tags with structured image without url suffix', function () {
52+
$og = Website::make('Title | Example')
53+
->url('http://www.example.com')
54+
->description('Description')
55+
->locale('en_US')
56+
->alternateLocale('en_GB')
57+
->siteName('Example')
58+
->image(Image::make('http://www.example.com/image1.jpg', false)->mimeType('image/jpeg'));
59+
60+
assertMatchesHtmlSnapshot((string) $og);
61+
})->group('global', 'website');
62+
3863
it('can generate website tags with conditional callbacks', function () {
3964
$og = Website::make('Title | Example')
4065
->url('http://www.example.com')
4166
->description('Description')
4267
->locale('en_US')
43-
->when(false, fn (Website $og) => $og->alternateLocale('de_DE'))
44-
->when(true, fn (Website $og) => $og->alternateLocale('en_GB'))
68+
->when(false, function (Website $og) {
69+
$og->alternateLocale('de_DE');
70+
})
71+
->when(true, function (Website $og) {
72+
$og->alternateLocale('en_GB');
73+
})
4574
->siteName('Example')
4675
->image('http://www.example.com/image1.jpg');
4776

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html><head>
3+
<meta property="og:type" content="website">
4+
<meta property="og:title" content="Title | Example">
5+
<meta property="og:url" content="http://www.example.com">
6+
<meta property="og:description" content="Description">
7+
<meta property="og:locale" content="en_US">
8+
<meta property="og:locale:alternate" content="en_GB">
9+
<meta property="og:site_name" content="Example">
10+
<meta property="og:image:url" content="http://www.example.com/image1.jpg">
11+
<meta property="og:image:type" content="image/jpeg">
12+
</head></html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html><head>
3+
<meta property="og:type" content="website">
4+
<meta property="og:title" content="Title | Example">
5+
<meta property="og:url" content="http://www.example.com">
6+
<meta property="og:description" content="Description">
7+
<meta property="og:locale" content="en_US">
8+
<meta property="og:locale:alternate" content="en_GB">
9+
<meta property="og:site_name" content="Example">
10+
<meta property="og:image" content="http://www.example.com/image1.jpg">
11+
<meta property="og:image:type" content="image/jpeg">
12+
</head></html>

0 commit comments

Comments
 (0)