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

fix: Changes property to name for twitter cards #17

Closed
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
23 changes: 23 additions & 0 deletions src/TwitterBaseObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Astrotomic\OpenGraph;

use Astrotomic\ConditionalProxy\HasConditionalCalls;

abstract class TwitterBaseObject
{
use HasConditionalCalls;

/** @var TwitterBaseObject[] */
protected $tags = [];

public function setName(string $prefix, string $name, string $content)
{
$this->tags[$prefix.':'.$name] = TwitterName::make($prefix, $name, $content);
}

public function __toString(): string
{
return implode(PHP_EOL, array_map('strval', $this->tags));
}
}
34 changes: 34 additions & 0 deletions src/TwitterName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Astrotomic\OpenGraph;

class TwitterName
{
/** @var string */
protected $prefix = 'twitter';

/** @var string */
protected $name;

/** @var string */
protected $content;

public function __construct(string $prefix, string $name, string $content)
{
$this->prefix = $prefix;
$this->name = $name;
$this->content = $content;
}

public static function make(string $prefix, string $name, string $content)
{
return new static($prefix, $name, $content);
}

public function __toString(): string
{
$content = str_replace('"', '&quot;', $this->content);

return "<meta name=\"{$this->prefix}:{$this->name}\" content=\"{$content}\">";
}
}
14 changes: 7 additions & 7 deletions src/TwitterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Astrotomic\OpenGraph;

abstract class TwitterType extends BaseObject
abstract class TwitterType extends TwitterBaseObject
{
protected const PREFIX = 'twitter';

public function __construct(?string $title = null)
{
$this->setProperty(self::PREFIX, 'card', $this->type);
$this->setName(self::PREFIX, 'card', $this->type);
$this->when($title)->title($title);
}

Expand All @@ -19,29 +19,29 @@ public static function make(?string $title = null)

public function title(string $title)
{
$this->setProperty(self::PREFIX, 'title', $title);
$this->setName(self::PREFIX, 'title', $title);

return $this;
}

public function site(string $site)
{
$this->setProperty(self::PREFIX, 'site', $site);
$this->setName(self::PREFIX, 'site', $site);

return $this;
}

public function description(string $description)
{
$this->setProperty(self::PREFIX, 'description', $description);
$this->setName(self::PREFIX, 'description', $description);

return $this;
}

public function image(string $image, ?string $alt = null)
{
$this->setProperty(self::PREFIX, 'image', $image);
$this->when($alt)->setProperty(self::PREFIX, 'image:alt', $alt);
$this->setName(self::PREFIX, 'image', $image);
$this->when($alt)->setName(self::PREFIX, 'image:alt', $alt);

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Types/Twitter/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Player extends TwitterType

public function player(string $url, int $width, int $height)
{
$this->setProperty(self::PREFIX, 'player', $url);
$this->setProperty(self::PREFIX, 'player:width', $width);
$this->setProperty(self::PREFIX, 'player:height', $height);
$this->setName(self::PREFIX, 'player', $url);
$this->setName(self::PREFIX, 'player:width', $width);
$this->setName(self::PREFIX, 'player:height', $height);

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Types/Twitter/SummaryLargeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SummaryLargeImage extends TwitterType

public function creator(string $creator)
{
$this->setProperty(self::PREFIX, 'creator', $creator);
$this->setName(self::PREFIX, 'creator', $creator);

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +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="twitter:card" content="player">
<meta property="twitter:title" content="Title | Example">
<meta property="twitter:description" content="Description">
<meta property="twitter:site" content="@astrotomic_oss">
<meta property="twitter:image" content="http://www.example.com/image1.jpg">
<meta property="twitter:image:alt" content="Image alternate text">
<meta property="twitter:player" content="http://www.example.com/player.iframe">
<meta property="twitter:player:width" content="1920">
<meta property="twitter:player:height" content="1080">
<meta name="twitter:card" content="player">
<meta name="twitter:title" content="Title | Example">
<meta name="twitter:description" content="Description">
<meta name="twitter:site" content="@astrotomic_oss">
<meta name="twitter:image" content="http://www.example.com/image1.jpg">
<meta name="twitter:image:alt" content="Image alternate text">
<meta name="twitter:player" content="http://www.example.com/player.iframe">
<meta name="twitter:player:width" content="1920">
<meta name="twitter:player:height" content="1080">
</head></html>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head>
<meta property="twitter:card" content="summary">
<meta property="twitter:title" content="Title | Example">
<meta property="twitter:description" content="Description">
<meta property="twitter:site" content="@astrotomic_oss">
<meta property="twitter:image" content="http://www.example.com/image1.jpg">
<meta property="twitter:image:alt" content="Image alternate text">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Title | Example">
<meta name="twitter:description" content="Description">
<meta name="twitter:site" content="@astrotomic_oss">
<meta name="twitter:image" content="http://www.example.com/image1.jpg">
<meta name="twitter:image:alt" content="Image alternate text">
</head></html>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head>
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:title" content="Title | Example">
<meta property="twitter:description" content="Description">
<meta property="twitter:site" content="@astrotomic_oss">
<meta property="twitter:creator" content="@devgummibeer">
<meta property="twitter:image" content="http://www.example.com/image1.jpg">
<meta property="twitter:image:alt" content="Image alternate text">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Title | Example">
<meta name="twitter:description" content="Description">
<meta name="twitter:site" content="@astrotomic_oss">
<meta name="twitter:creator" content="@devgummibeer">
<meta name="twitter:image" content="http://www.example.com/image1.jpg">
<meta name="twitter:image:alt" content="Image alternate text">
</head></html>