Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 4b9d056

Browse files
authored
Merge pull request #48 from 8fold/feed-rss
SocialMeta
2 parents d306a52 + e45672c commit 4b9d056

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

src/UIKit.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ static public function stripeElements($formId, $apiKey, $inputLabel, $buttonLabe
8989
return new $class($formId, $apiKey, $inputLabel, $buttonLabel);
9090
}
9191

92+
static public function socialMeta(
93+
string $type,
94+
string $title,
95+
string $url,
96+
string $description,
97+
string $image = "",
98+
string $appId = ""
99+
)
100+
{
101+
$class = self::class("socialMeta", self::CLASSES)->unfold();
102+
return new $class($type, $title, $url, $description, $image, $appId);
103+
}
104+
92105
static public function __callStatic(string $element, array $elements)
93106
{
94107
$class = Html::class($element, self::CLASSES);
@@ -177,7 +190,8 @@ static public function __callStatic(string $element, array $elements)
177190
// , 'alert' => UIKit\Elements\Compound\Alert::class
178191
'doubleWrap' => \Eightfold\Markup\UIKit\Elements\Compound\DoubleWrap::class,
179192
'markdown' => \Eightfold\Markup\UIKit\Elements\Compound\Markdown::class,
180-
'pagination' => \Eightfold\Markup\UIKit\Elements\Compound\Pagination::class
193+
'pagination' => \Eightfold\Markup\UIKit\Elements\Compound\Pagination::class,
194+
'socialMeta' => \Eightfold\Markup\UIKit\Elements\Compound\SocialMeta::class
181195
// , 'primary_nav' => UIKit\Elements\Compound\NavigationPrimary::class
182196
// , 'secondary_nav' => UIKit\Elements\Compound\NavigationSecondary::class
183197
// , 'side_nav' => UIKit\Elements\Compound\NavigationSide::class
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Eightfold\Markup\UIKit\Elements\Compound;
4+
5+
use Eightfold\Shoop\Shoop;
6+
7+
use Eightfold\Markup\Html\Elements\HtmlElement;
8+
use Eightfold\Markup\Html;
9+
10+
class SocialMeta extends HtmlElement
11+
{
12+
private $type = "";
13+
private $title = "";
14+
private $url = "";
15+
private $description = "";
16+
private $image = "";
17+
private $appId = "";
18+
19+
private $meta;
20+
21+
public function __construct(
22+
string $type,
23+
string $title,
24+
string $url,
25+
string $description,
26+
string $image = "",
27+
string $appId = ""
28+
)
29+
{
30+
$this->meta = Shoop::array([
31+
"og:type ". $type,
32+
"og:title ". $title,
33+
"og:url ". $url,
34+
"og:description ". $description
35+
]);
36+
37+
if (Shoop::string($image)->isNotEmpty) {
38+
$this->meta = $this->meta->plus("og:image ". $image);
39+
}
40+
41+
if (Shoop::string($appId)->isNotEmpty) {
42+
$this->meta = $this->meta->plus("og:app_id ". $appId);
43+
}
44+
}
45+
46+
public function twitter($card = "summary_large_image")
47+
{
48+
$this->meta = $this->meta->plus("twitter:card {$card}");
49+
return $this;
50+
}
51+
52+
public function unfold(): string
53+
{
54+
return $this->meta->each(function($meta) {
55+
list($name, $value) = Shoop::string($meta)->divide(" ", false, 2);
56+
return Shoop::string($name)
57+
->startsWith("og:", function($result, $name) use ($value) {
58+
return ($result->unfold())
59+
? Html::meta()->attr("property {$name}", "content {$value}")
60+
: Html::meta()->attr("name {$name}", "content {$value}");
61+
});
62+
})->join("");
63+
}
64+
}

tests/UIKit/SocialMetaTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Eightfold\Markup\Tests\UIKit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use Eightfold\Markup\UIKit;
8+
9+
class SocialMetaTest extends TestCase
10+
{
11+
public function testFacebook()
12+
{
13+
$expected = '<meta content="website" property="og:type"><meta content="Hello, World!" property="og:title"><meta content="https://8fold.pro" property="og:url"><meta content="A short description. LinkedIn would like to have 100+ characters." property="og:description">';
14+
$actual = UIKit::socialMeta(
15+
"website",
16+
"Hello, World!",
17+
"https://8fold.pro",
18+
"A short description. LinkedIn would like to have 100+ characters."
19+
);
20+
$this->assertEquals($expected, $actual->unfold());
21+
22+
$expected = '<meta content="website" property="og:type"><meta content="Hello, World!" property="og:title"><meta content="https://8fold.pro" property="og:url"><meta content="A short description. LinkedIn would like to have 100+ characters." property="og:description"><meta content="https://8fold.pro/assets/ui/logo.svg" property="og:image"><meta content="ABCDEFGHIJKLMNOP" property="og:app_id">';
23+
$actual = UIKit::socialMeta(
24+
"website",
25+
"Hello, World!",
26+
"https://8fold.pro",
27+
"A short description. LinkedIn would like to have 100+ characters.",
28+
"https://8fold.pro/assets/ui/logo.svg",
29+
"ABCDEFGHIJKLMNOP"
30+
);
31+
$this->assertEquals($expected, $actual->unfold());
32+
}
33+
34+
public function testTwitter()
35+
{
36+
$expected = '<meta content="website" property="og:type"><meta content="Hello, World!" property="og:title"><meta content="https://8fold.pro" property="og:url"><meta content="A short description. LinkedIn would like to have 100+ characters." property="og:description"><meta content="https://8fold.pro/assets/ui/logo.svg" property="og:image"><meta content="ABCDEFGHIJKLMNOP" property="og:app_id"><meta name="twitter:card" content="summary_large_image">';
37+
$actual = UIKit::socialMeta(
38+
"website",
39+
"Hello, World!",
40+
"https://8fold.pro",
41+
"A short description. LinkedIn would like to have 100+ characters.",
42+
"https://8fold.pro/assets/ui/logo.svg",
43+
"ABCDEFGHIJKLMNOP"
44+
)->twitter();
45+
$this->assertEquals($expected, $actual->unfold());
46+
}
47+
}

0 commit comments

Comments
 (0)