Skip to content

Commit 073ac89

Browse files
committed
add conditional proxy
1 parent 430851f commit 073ac89

5 files changed

+84
-1
lines changed

src/BaseObject.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ abstract class BaseObject
99
/** @var BaseObject[] */
1010
protected array $tags = [];
1111

12-
public function when(bool $condition, Closure $callback)
12+
/**
13+
* @param $condition
14+
* @param Closure|null $callback
15+
*
16+
* @return $this|ConditionalProxy
17+
*/
18+
public function when($condition, ?Closure $callback = null)
1319
{
20+
if ($callback === null) {
21+
return new ConditionalProxy($this, boolval($condition));
22+
}
23+
1424
if ($condition) {
1525
$callback($this);
1626
}

src/ConditionalProxy.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Astrotomic\OpenGraph;
4+
5+
/** @mixin BaseObject */
6+
class ConditionalProxy
7+
{
8+
protected BaseObject $object;
9+
protected bool $condition;
10+
11+
public function __construct(BaseObject $object, bool $condition)
12+
{
13+
$this->object = $object;
14+
$this->condition = $condition;
15+
}
16+
17+
public function __call(string $name, array $arguments)
18+
{
19+
if($this->condition) {
20+
call_user_func_array([$this->object, $name], $arguments);
21+
}
22+
23+
return $this->object;
24+
}
25+
}

tests/GlobalTypesTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ public function __toString()
3434
assertMatchesHtmlSnapshot((string) $og);
3535
})->group('global', 'website');
3636

37+
it('can generate website tags with conditional callbacks', function () {
38+
$og = Website::make('Title | Example')
39+
->url('http://www.example.com')
40+
->description('Description')
41+
->locale('en_US')
42+
->when(false, fn(Website $og) => $og->alternateLocale('de_DE'))
43+
->when(true, fn(Website $og) => $og->alternateLocale('en_GB'))
44+
->siteName('Example')
45+
->image('http://www.example.com/image1.jpg');
46+
47+
assertMatchesHtmlSnapshot((string) $og);
48+
})->group('global', 'website');
49+
50+
it('can generate website tags with conditional proxies', function () {
51+
$og = Website::make('Title | Example')
52+
->url('http://www.example.com')
53+
->description('Description')
54+
->locale('en_US')
55+
->when(false)->alternateLocale('de_DE')
56+
->when(true)->alternateLocale('en_GB')
57+
->siteName('Example')
58+
->image('http://www.example.com/image1.jpg');
59+
60+
assertMatchesHtmlSnapshot((string) $og);
61+
})->group('global', 'website');
62+
3763
it('can generate article tags', function () {
3864
$og = Article::make('Article | Example')
3965
->url('http://www.example.com/article')
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html><head>
3+
<meta property="og:type" content="website">
4+
<meta property="og:title" content="Title | Example">
5+
<meta property="og:url" content="http://www.example.com">
6+
<meta property="og:description" content="Description">
7+
<meta property="og:locale" content="en_US">
8+
<meta property="og:locale:alternate" content="en_GB">
9+
<meta property="og:site_name" content="Example">
10+
<meta property="og:image:url" content="http://www.example.com/image1.jpg">
11+
</head></html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html><head>
3+
<meta property="og:type" content="website">
4+
<meta property="og:title" content="Title | Example">
5+
<meta property="og:url" content="http://www.example.com">
6+
<meta property="og:description" content="Description">
7+
<meta property="og:locale" content="en_US">
8+
<meta property="og:locale:alternate" content="en_GB">
9+
<meta property="og:site_name" content="Example">
10+
<meta property="og:image:url" content="http://www.example.com/image1.jpg">
11+
</head></html>

0 commit comments

Comments
 (0)