Skip to content

Commit c1ea5ea

Browse files
committed
integrate astrotomic/php-conditional-proxy
1 parent 818832c commit c1ea5ea

File tree

11 files changed

+21
-37
lines changed

11 files changed

+21
-37
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^7.4"
18+
"php": "^7.4",
19+
"astrotomic/php-conditional-proxy": "^0.1.0"
1920
},
2021
"require-dev": {
2122
"pestphp/pest": "^0.1.5",

src/BaseObject.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,21 @@
22

33
namespace Astrotomic\OpenGraph;
44

5-
use Closure;
5+
use Astrotomic\ConditionalProxy\HasConditionalCalls;
66

77
abstract class BaseObject
88
{
9+
use HasConditionalCalls;
10+
911
/** @var BaseObject[] */
1012
protected array $tags = [];
1113

12-
/**
13-
* @param $condition
14-
* @param Closure|null $callback
15-
*
16-
* @return $this|ConditionalProxy
17-
*/
18-
public function when($condition, ?Closure $callback = null)
19-
{
20-
if ($callback === null) {
21-
return new ConditionalProxy($this, boolval($condition));
22-
}
23-
24-
if ($condition) {
25-
$callback($this);
26-
}
27-
28-
return $this;
29-
}
30-
31-
protected function setProperty(string $prefix, string $property, string $content)
14+
public function setProperty(string $prefix, string $property, string $content)
3215
{
3316
$this->tags[$prefix.':'.$property] = Property::make($prefix, $property, $content);
3417
}
3518

36-
protected function addProperty(string $prefix, string $property, string $content)
19+
public function addProperty(string $prefix, string $property, string $content)
3720
{
3821
$this->tags[] = Property::make($prefix, $property, $content);
3922
}

src/TwitterType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ abstract class TwitterType extends BaseObject
99
public function __construct(?string $title = null)
1010
{
1111
$this->setProperty(self::PREFIX, 'card', $this->type);
12-
$this->when(! empty($title), fn () => $this->title($title));
12+
$this->when($title)->title($title);
1313
}
1414

1515
public static function make(?string $title = null)
@@ -41,7 +41,7 @@ public function description(string $description)
4141
public function image(string $image, ?string $alt = null)
4242
{
4343
$this->setProperty(self::PREFIX, 'image', $image);
44-
$this->when(! empty($alt), fn () => $this->setProperty(self::PREFIX, 'image:alt', $alt));
44+
$this->when($alt)->setProperty(self::PREFIX, 'image:alt', $alt);
4545

4646
return $this;
4747
}

src/Type.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class Type extends BaseObject
1313
public function __construct(?string $title = null)
1414
{
1515
$this->setProperty('og', 'type', $this->type);
16-
$this->when(! empty($title), fn () => $this->title($title));
16+
$this->when($title)->title($title);
1717
}
1818

1919
public static function make(?string $title = null)
@@ -116,7 +116,7 @@ public function audio($audio)
116116
return $this;
117117
}
118118

119-
protected function addStructuredProperty(BaseObject $property)
119+
public function addStructuredProperty(BaseObject $property)
120120
{
121121
$this->tags[] = $property;
122122
}

src/Types/Music/Album.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public function musician(string $url)
2121
public function song(string $url, ?int $disc = null, ?int $track = null)
2222
{
2323
$this->addProperty(self::PREFIX, 'song', $url);
24-
$this->when($disc > 0, fn () => $this->addProperty(self::PREFIX, 'song:disc', $disc));
25-
$this->when($track > 0, fn () => $this->addProperty(self::PREFIX, 'song:track', $track));
24+
$this->when($disc > 0)->addProperty(self::PREFIX, 'song:disc', $disc);
25+
$this->when($track > 0)->addProperty(self::PREFIX, 'song:track', $track);
2626

2727
return $this;
2828
}

src/Types/Music/Playlist.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function creator(string $url)
2020
public function song(string $url, ?int $disc = null, ?int $track = null)
2121
{
2222
$this->addProperty(self::PREFIX, 'song', $url);
23-
$this->when($disc > 0, fn () => $this->addProperty(self::PREFIX, 'song:disc', $disc));
24-
$this->when($track > 0, fn () => $this->addProperty(self::PREFIX, 'song:track', $track));
23+
$this->when($disc > 0)->addProperty(self::PREFIX, 'song:disc', $disc);
24+
$this->when($track > 0)->addProperty(self::PREFIX, 'song:track', $track);
2525

2626
return $this;
2727
}

src/Types/Music/Song.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function musician(string $url)
2727
public function album(string $url, ?int $disc = null, ?int $track = null)
2828
{
2929
$this->addProperty(self::PREFIX, 'album', $url);
30-
$this->when($disc > 0, fn () => $this->addProperty(self::PREFIX, 'album:disc', $disc));
31-
$this->when($track > 0, fn () => $this->addProperty(self::PREFIX, 'album:track', $track));
30+
$this->when($disc > 0)->addProperty(self::PREFIX, 'album:disc', $disc);
31+
$this->when($track > 0)->addProperty(self::PREFIX, 'album:track', $track);
3232

3333
return $this;
3434
}

src/Types/Video/Episode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function series(string $url)
2121
public function actor(string $url, ?string $role = null)
2222
{
2323
$this->addProperty(self::PREFIX, 'actor', $url);
24-
$this->when(! empty($role), fn () => $this->addProperty(self::PREFIX, 'actor:role', $role));
24+
$this->when($role)->addProperty(self::PREFIX, 'actor:role', $role);
2525

2626
return $this;
2727
}

src/Types/Video/Movie.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Movie extends Type
1414
public function actor(string $url, ?string $role = null)
1515
{
1616
$this->addProperty(self::PREFIX, 'actor', $url);
17-
$this->when(! empty($role), fn () => $this->addProperty(self::PREFIX, 'actor:role', $role));
17+
$this->when($role)->addProperty(self::PREFIX, 'actor:role', $role);
1818

1919
return $this;
2020
}

src/Types/Video/Other.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Other extends Type
1414
public function actor(string $url, ?string $role = null)
1515
{
1616
$this->addProperty(self::PREFIX, 'actor', $url);
17-
$this->when(! empty($role), fn () => $this->addProperty(self::PREFIX, 'actor:role', $role));
17+
$this->when($role)->addProperty(self::PREFIX, 'actor:role', $role);
1818

1919
return $this;
2020
}

0 commit comments

Comments
 (0)