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

Open graph protocol #257

Merged
merged 5 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add image array for multiple tags allowed. Some cosmetics
  • Loading branch information
DeryabinSergey committed Apr 15, 2021
commit 9e8c0946c0946c1f24a8abcbb06d3f8371a4497c
78 changes: 54 additions & 24 deletions src/Main/Markup/OGP/OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
*/
class OpenGraph
{
const OGP_NAMESPACE = ['og', 'http://ogp.me/ns#'] ;
const FB_NAMESPACE = ['fb', 'http://ogp.me/ns/fb#'];
const OGP_NAMESPACE = ['og', 'https://ogp.me/ns#'] ;
const FB_NAMESPACE = ['fb', 'https://ogp.me/ns/fb#'];

/**
* The title of your object as it should appear within the graph, e.g., "The Rock".
Expand Down Expand Up @@ -76,9 +76,9 @@ class OpenGraph
protected ?OpenGraphObject $type = null;
/**
* An image which should represent your object within the graph.
* @var ?OpenGraphImage
* @var OpenGraphImage[]
*/
protected ?OpenGraphImage $image = null;
protected array $image = [];
/**
* @var ?OpenGraphVideo
*/
Expand All @@ -94,6 +94,10 @@ class OpenGraph
* @var ?string
*/
protected ?string $url = null;
/**
* @var ?string
*/
protected ?string $vkImage = null;
/**
* @var ?string
*/
Expand Down Expand Up @@ -156,10 +160,10 @@ public function setDaterminer(string $daterminer): static
*/
public function setLocale(string $locale): static
{
Assert::isTrue(
preg_match('/^[a-z]{2}_[A-Z]{2}$/iu', $locale),
'wrong locale format'
);
Assert::isTrue(
preg_match('/^[a-z]{2}_[A-Z]{2}$/iu', $locale) == 1,
'wrong locale format'
);
$this->locale = $locale;

return $this;
Expand All @@ -172,10 +176,10 @@ public function setLocale(string $locale): static
*/
public function setLocaleAlternates(string $locale): static
{
Assert::isTrue(
preg_match('/^[a-z]{2}_[A-Z]{2}$/iu', $locale),
'wrong locale format'
);
Assert::isTrue(
preg_match('/^[a-z]{2}_[A-Z]{2}$/iu', $locale) == 1,
'wrong locale format'
);
$this->localeAlternates[] = $locale;

return $this;
Expand Down Expand Up @@ -209,7 +213,7 @@ public function setType(OpenGraphObject $type): static
*/
public function setImage(OpenGraphImage $image): static
{
$this->image = $image;
$this->image[] = $image;

return $this;
}
Expand Down Expand Up @@ -269,13 +273,32 @@ public function setUrl(string $url): static
return $this;
}

/**
* @param bool $full
* @return string
*/
/**
* Minimal image size - 160 x 160 px. Recommend greater than 510 x 228 px.
* @see https://vk.com/dev/publications
* @param string $vkImage
* @return static
*/
public function setVkImage(string $vkImage): static
{
$this->vkImage = $vkImage;

return $this;
}

/**
* @param bool $full
* @return string
* @throws WrongArgumentException
*/
public function getPrefix(bool $full = true): string
{
$prefix = [self::OGP_NAMESPACE[0] . ': ' . self::OGP_NAMESPACE[1]];
Assert::isNotEmpty($this->type, 'type is required');

$prefix = [
self::OGP_NAMESPACE[0] . ': ' . self::OGP_NAMESPACE[1],
$this->type->getNamespace() . ': ' . $this->type->getType()->getNamespace(),
];
if (!empty($this->appId)) {
$prefix[] = self::FB_NAMESPACE[0] . ': ' . self::FB_NAMESPACE[1];
}
Expand Down Expand Up @@ -306,11 +329,6 @@ public function dump(): string
(new SgmlOpenTag())->setId('meta')->setEmpty(true)
->setAttribute('property', 'og:url')
->setAttribute('content', $this->url),
(new SgmlOpenTag())->setId('meta')->setEmpty(true)
->setAttribute(
'prefix',
$this->type->getNamespace() . ': ' . $this->type->getType()->getNamespace()
),
(new SgmlOpenTag())->setId('meta')->setEmpty(true)
->setAttribute('property', 'og:type')
->setAttribute('content', $this->type->getType()->getName()),
Expand All @@ -332,7 +350,12 @@ function ($item) {
->setAttribute('property', $item[0])
->setAttribute('content', $item[1]);
},
$this->image->getList()
array_reduce(
$this->image,
function ($result, OpenGraphImage $image) {
return array_merge($result, $image->getList());
}, []
)
),
array_map(
function ($item) {
Expand Down Expand Up @@ -378,6 +401,13 @@ function ($item) {
->setAttribute('property', 'fb:app_id')
->setAttribute('content', $this->appId),
],
empty($this->vkImage)
? []
: [
(new SgmlOpenTag())->setId('meta')->setEmpty(true)
->setAttribute('property', 'vk:image')
->setAttribute('content', $this->vkImage),
],
array_map(
function ($item) {
return (new SgmlOpenTag())->setId('meta')->setEmpty(true)
Expand Down
2 changes: 1 addition & 1 deletion src/Main/Markup/OGP/OpenGraphArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct()
$this->items = [
'published_time' => null,
'modified_time' => null,
'expiration_time ' => null,
'expiration_time' => null,
'author' => [],
'section' => null,
'tag' => [],
Expand Down
36 changes: 29 additions & 7 deletions src/Main/Markup/OGP/OpenGraphMovie.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,43 @@ public function __construct()
$this->type = new OpenGraphType(OpenGraphType::MOVIE_ID);
}

/**
* @param string $name
* @param mixed $value
* @return static
* @throws WrongArgumentException
*/
public function set(string $name, mixed $value): static
{
if ($name == 'actor') {
$count = count($this->items['actor']);
for($i = $count - count($this->items['actor:role']); $i > 0; $i--) {
$this->items['actor:role'][] = null;
}
}

if ($name == 'actor:role') {
Assert::isLesser(
count($this->items['actor:role']),
count($this->items['actor']),
'add actor before adding actor:role'
);
}

return parent::set($name, $value);
}

/**
* @return array
* @throws WrongArgumentException
*/
public function getList(): array
{
Assert::isTrue(
count($this->items['actor']) == count($this->items['actor:role']),
'actor items not equal actor:role items'
);

$list = [];
foreach($this->items['actor'] as $key => $actor) {
$list[] = [$this->namespace . ':' . 'actor', $actor];
$list[] = [$this->namespace . ':' . 'actor:role', $this->items['actor:role'][$key]];
if (!empty($this->items['actor:role'][$key])) {
$list[] = [$this->namespace . ':' . 'actor:role', $this->items['actor:role'][$key]];
}
}

foreach($this->items as $key => $value) {
Expand Down
1 change: 0 additions & 1 deletion src/Main/Markup/OGP/OpenGraphObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public static function create(): static

/**
* @return array
* @throws WrongArgumentException
*/
public function getList(): array
{
Expand Down
18 changes: 17 additions & 1 deletion src/Main/Markup/OGP/OpenGraphSong.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OnPHP\Main\Markup\OGP;

use OnPHP\Core\Base\Assert;
use OnPHP\Core\Exception\WrongArgumentException;

/**
Expand Down Expand Up @@ -68,12 +69,27 @@ public function set(string $name, mixed $value): static
}
}

if ($name == 'album:disc') {
Assert::isLesser(
count($this->items['album:disc']),
count($this->items['album']),
'add album before adding album:disc'
);
}

if ($name == 'album:track') {
Assert::isLesser(
count($this->items['album:track']),
count($this->items['album']),
'add album before adding album:track'
);
}

return parent::set($name, $value);
}

/**
* @return array
* @throws WrongArgumentException
*/
public function getList(): array
{
Expand Down
19 changes: 18 additions & 1 deletion src/Main/Markup/OGP/OpenGraphSongObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OnPHP\Main\Markup\OGP;

use OnPHP\Core\Base\Assert;
use OnPHP\Core\Exception\WrongArgumentException;

/**
Expand Down Expand Up @@ -53,12 +54,28 @@ public function set(string $name, mixed $value): static
$this->items['song:track'][] = null;
}
}

if ($name == 'song:disc') {
Assert::isLesser(
count($this->items['song:disc']),
count($this->items['song']),
'add song before adding song:disc'
);
}

if ($name == 'song:track') {
Assert::isLesser(
count($this->items['song:track']),
count($this->items['song']),
'add song before adding song:track'
);
}

return parent::set($name, $value);
}

/**
* @return array
* @throws WrongArgumentException
*/
public function getList(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Main/Markup/OGP/OpenGraphVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OpenGraphVideo extends OpenGraphStructure
/**
* @var string
*/
protected string $video = 'video';
protected string $name = 'video';
/**
* @var int
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Main/Markup/OGP/OpenGraphWebsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
*/
class OpenGraphWebsite extends OpenGraphObject
{
/**
* @var string
*/
protected string $namespace = 'website';
/**
* OpenGraphWebsite constructor.
*/
Expand Down