Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to suppress URL suffix for structured properties #11

Merged
merged 5 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.1, 7.2, 7.3, 7.4]
php: [7.4]
dependency-version: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
"astrotomic/php-conditional-proxy": "^0.2.0"
},
"require-dev": {
"pestphp/pest": "^0.1.5",
"spatie/pest-plugin-snapshots": "^0.1.0"
"pestphp/pest": "^0.3.0",
"spatie/pest-plugin-snapshots": "^0.3.0"
},
"suggest": {
"php": "^7.4"
},
"config": {
"sort-packages": true
Expand Down
14 changes: 1 addition & 13 deletions src/StructuredProperties/Audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@

namespace Astrotomic\OpenGraph\StructuredProperties;

use Astrotomic\OpenGraph\BaseObject;

class Audio extends BaseObject
class Audio extends StructuredProperty
{
protected const PREFIX = 'og:audio';

public function __construct(string $url)
{
$this->setProperty(self::PREFIX, 'url', $url);
}

public static function make(string $url)
{
return new static($url);
}

public function secureUrl(string $url)
{
$this->setProperty(self::PREFIX, 'secure_url', $url);
Expand Down
14 changes: 1 addition & 13 deletions src/StructuredProperties/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@

namespace Astrotomic\OpenGraph\StructuredProperties;

use Astrotomic\OpenGraph\BaseObject;

class Image extends BaseObject
class Image extends StructuredProperty
{
protected const PREFIX = 'og:image';

public function __construct(string $url)
{
$this->setProperty(self::PREFIX, 'url', $url);
}

public static function make(string $url)
{
return new static($url);
}

public function secureUrl(string $url)
{
$this->setProperty(self::PREFIX, 'secure_url', $url);
Expand Down
23 changes: 23 additions & 0 deletions src/StructuredProperties/StructuredProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Astrotomic\OpenGraph\StructuredProperties;

use Astrotomic\OpenGraph\BaseObject;

abstract class StructuredProperty extends BaseObject
{
public function __construct(string $url, bool $withUrlSuffix = true)
{
if ($withUrlSuffix) {
$this->setProperty(static::PREFIX, 'url', $url);
} else {
$prefix = explode(':', static::PREFIX, 2);
$this->setProperty($prefix[0], $prefix[1], $url);
}
}

public static function make(string $url, bool $withUrlSuffix = true)
{
return new static($url, $withUrlSuffix);
}
}
14 changes: 1 addition & 13 deletions src/StructuredProperties/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@

namespace Astrotomic\OpenGraph\StructuredProperties;

use Astrotomic\OpenGraph\BaseObject;

class Video extends BaseObject
class Video extends StructuredProperty
{
protected const PREFIX = 'og:video';

public function __construct(string $url)
{
$this->setProperty(self::PREFIX, 'url', $url);
}

public static function make(string $url)
{
return new static($url);
}

public function secureUrl(string $url)
{
$this->setProperty(self::PREFIX, 'secure_url', $url);
Expand Down
26 changes: 26 additions & 0 deletions tests/GlobalTypesTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

use Astrotomic\OpenGraph\StructuredProperties\Audio;
use Astrotomic\OpenGraph\StructuredProperties\Image;
use Astrotomic\OpenGraph\Types\Article;
use Astrotomic\OpenGraph\Types\Book;
use Astrotomic\OpenGraph\Types\Profile;
use Astrotomic\OpenGraph\Types\Website;
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};

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

it('can generate website tags with structured image', function () {
$og = Website::make('Title | Example')
->url('http://www.example.com')
->description('Description')
->locale('en_US')
->alternateLocale('en_GB')
->siteName('Example')
->image(Image::make('http://www.example.com/image1.jpg')->mimeType('image/jpeg'));

assertMatchesHtmlSnapshot((string) $og);
})->group('global', 'website');

it('can generate website tags with structured image without url suffix', function () {
$og = Website::make('Title | Example')
->url('http://www.example.com')
->description('Description')
->locale('en_US')
->alternateLocale('en_GB')
->siteName('Example')
->image(Image::make('http://www.example.com/image1.jpg', false)->mimeType('image/jpeg'));

assertMatchesHtmlSnapshot((string) $og);
})->group('global', 'website');

it('can generate website tags with conditional callbacks', function () {
$og = Website::make('Title | Example')
->url('http://www.example.com')
Expand Down
1 change: 1 addition & 0 deletions tests/MusicTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Astrotomic\OpenGraph\Types\Music\Playlist;
use Astrotomic\OpenGraph\Types\Music\RadioStation;
use Astrotomic\OpenGraph\Types\Music\Song;
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};

it('can generate song tags', function () {
$og = Song::make('Title | Example')
Expand Down
1 change: 1 addition & 0 deletions tests/TwitterTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Astrotomic\OpenGraph\Types\Twitter\Player;
use Astrotomic\OpenGraph\Types\Twitter\Summary;
use Astrotomic\OpenGraph\Types\Twitter\SummaryLargeImage;
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};

it('can generate summary tags', function () {
$og = Summary::make('Title | Example')
Expand Down
1 change: 1 addition & 0 deletions tests/VideoTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Astrotomic\OpenGraph\Types\Video\Movie;
use Astrotomic\OpenGraph\Types\Video\Other;
use Astrotomic\OpenGraph\Types\Video\TvShow;
use function Spatie\Snapshots\{assertMatchesHtmlSnapshot};

it('can generate movie tags', function () {
$og = Movie::make('Title | Example')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head>
<meta property="og:type" content="website">
<meta property="og:title" content="Title | Example">
<meta property="og:url" content="http://www.example.com">
<meta property="og:description" content="Description">
<meta property="og:locale" content="en_US">
<meta property="og:locale:alternate" content="en_GB">
<meta property="og:site_name" content="Example">
<meta property="og:image:url" content="http://www.example.com/image1.jpg">
<meta property="og:image:type" content="image/jpeg">
</head></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head>
<meta property="og:type" content="website">
<meta property="og:title" content="Title | Example">
<meta property="og:url" content="http://www.example.com">
<meta property="og:description" content="Description">
<meta property="og:locale" content="en_US">
<meta property="og:locale:alternate" content="en_GB">
<meta property="og:site_name" content="Example">
<meta property="og:image" content="http://www.example.com/image1.jpg">
<meta property="og:image:type" content="image/jpeg">
</head></html>