Skip to content

Commit 2312ea9

Browse files
authored
Merge pull request #11 from Astrotomic/issue-10
allow to suppress URL suffix for structured properties
2 parents dd3d4c2 + 4dfd1a3 commit 2312ea9

12 files changed

+85
-42
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
fail-fast: true
1717
matrix:
18-
php: [7.1, 7.2, 7.3, 7.4]
18+
php: [7.4]
1919
dependency-version: [prefer-lowest, prefer-stable]
2020

2121
name: P${{ matrix.php }} - ${{ matrix.dependency-version }}

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
"astrotomic/php-conditional-proxy": "^0.2.0"
2020
},
2121
"require-dev": {
22-
"pestphp/pest": "^0.1.5",
23-
"spatie/pest-plugin-snapshots": "^0.1.0"
22+
"pestphp/pest": "^0.3.0",
23+
"spatie/pest-plugin-snapshots": "^0.3.0"
24+
},
25+
"suggest": {
26+
"php": "^7.4"
2427
},
2528
"config": {
2629
"sort-packages": true

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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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;
78
use Astrotomic\OpenGraph\Types\Website;
9+
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};
810

911
it('can generate website tags', function () {
1012
$og = Website::make('Title | Example')
@@ -35,6 +37,30 @@ public function __toString()
3537
assertMatchesHtmlSnapshot((string) $og);
3638
})->group('global', 'website');
3739

40+
it('can generate website tags with structured image', function () {
41+
$og = Website::make('Title | Example')
42+
->url('http://www.example.com')
43+
->description('Description')
44+
->locale('en_US')
45+
->alternateLocale('en_GB')
46+
->siteName('Example')
47+
->image(Image::make('http://www.example.com/image1.jpg')->mimeType('image/jpeg'));
48+
49+
assertMatchesHtmlSnapshot((string) $og);
50+
})->group('global', 'website');
51+
52+
it('can generate website tags with structured image without url suffix', function () {
53+
$og = Website::make('Title | Example')
54+
->url('http://www.example.com')
55+
->description('Description')
56+
->locale('en_US')
57+
->alternateLocale('en_GB')
58+
->siteName('Example')
59+
->image(Image::make('http://www.example.com/image1.jpg', false)->mimeType('image/jpeg'));
60+
61+
assertMatchesHtmlSnapshot((string) $og);
62+
})->group('global', 'website');
63+
3864
it('can generate website tags with conditional callbacks', function () {
3965
$og = Website::make('Title | Example')
4066
->url('http://www.example.com')

tests/MusicTypesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Astrotomic\OpenGraph\Types\Music\Playlist;
55
use Astrotomic\OpenGraph\Types\Music\RadioStation;
66
use Astrotomic\OpenGraph\Types\Music\Song;
7+
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};
78

89
it('can generate song tags', function () {
910
$og = Song::make('Title | Example')

tests/TwitterTypesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Astrotomic\OpenGraph\Types\Twitter\Player;
44
use Astrotomic\OpenGraph\Types\Twitter\Summary;
55
use Astrotomic\OpenGraph\Types\Twitter\SummaryLargeImage;
6+
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};
67

78
it('can generate summary tags', function () {
89
$og = Summary::make('Title | Example')

tests/VideoTypesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Astrotomic\OpenGraph\Types\Video\Movie;
77
use Astrotomic\OpenGraph\Types\Video\Other;
88
use Astrotomic\OpenGraph\Types\Video\TvShow;
9+
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};
910

1011
it('can generate movie tags', function () {
1112
$og = Movie::make('Title | Example')

0 commit comments

Comments
 (0)