Skip to content

Commit 4088c43

Browse files
refactored everything
1 parent c3453d9 commit 4088c43

File tree

6 files changed

+128
-78
lines changed

6 files changed

+128
-78
lines changed

src/OpenGraph.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Types/OpenGraphBaseType.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Spatie\OpenGraph\Types;
4+
5+
6+
use Spatie\OpenGraph\OpenGraphImage;
7+
use Spatie\OpenGraph\OpenGraphObject;
8+
9+
abstract class OpenGraphBaseType extends OpenGraphObject
10+
{
11+
/** @var string */
12+
protected $type;
13+
14+
public function __construct(?string $title = '', ?string $url, $image = null)
15+
{
16+
$this->type($this->type);
17+
$this->title($title);
18+
$this->url($url);
19+
$this->image($image);
20+
}
21+
22+
/**
23+
* Creates a new Open Graph object with the correct Open Graph object type.
24+
*
25+
* This method's `title`, `url` and `image` parameters are required
26+
* properties for an Open Graph object.
27+
* You can set them here or use the methods on the returned OpenGraphBaseType
28+
* instance.
29+
*
30+
* @param null|string $title
31+
* @param null|string $url
32+
* @param null|string|\Spatie\OpenGraph\OpenGraphImage $image
33+
*
34+
* @return OpenGraphBaseType
35+
*/
36+
public static function create(?string $title = '', ?string $url, $image = null)
37+
{
38+
return new static(...func_get_args());
39+
}
40+
41+
public function title($title)
42+
{
43+
$this->addTag('title', $title, 'og');
44+
45+
return $this;
46+
}
47+
48+
public function url($title)
49+
{
50+
$this->addTag('url', $title, 'og');
51+
52+
return $this;
53+
}
54+
55+
public function image($image)
56+
{
57+
if (is_string($image)) {
58+
$image = new OpenGraphImage($image);
59+
}
60+
61+
$this->objects[] = $image;
62+
63+
return $this;
64+
}
65+
66+
public function description(string $description)
67+
{
68+
$this->addTag('description', $description, 'og');
69+
70+
return $this;
71+
}
72+
73+
public function determiner(string $determiner)
74+
{
75+
$this->addTag('determiner', $determiner, 'og');
76+
77+
return $this;
78+
}
79+
80+
public function locale(string $locale = 'en_US')
81+
{
82+
$this->addTag('locale', $locale, 'og');
83+
84+
return $this;
85+
}
86+
87+
public function alternateLocale(string $locale = 'en_US')
88+
{
89+
$this->addTag('locale:alternate', $locale, 'og');
90+
91+
return $this;
92+
}
93+
94+
public function siteName(string $siteName)
95+
{
96+
$this->addTag('site_name', $siteName, 'og');
97+
98+
return $this;
99+
}
100+
101+
protected function type(string $type)
102+
{
103+
$this->addTag('type', $type, 'og');
104+
105+
return $this;
106+
}
107+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
namespace Spatie\OpenGraph;
3+
namespace Spatie\OpenGraph\Types;
44

5-
class OpenGraphBook extends OpenGraph
5+
6+
class OpenGraphBook extends OpenGraphBaseType
67
{
78
/** @var string */
89
protected $prefix = 'book';
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
namespace Spatie\OpenGraph;
3+
namespace Spatie\OpenGraph\Types;
44

5-
class OpenGraphMusic extends OpenGraph
5+
6+
class OpenGraphMusic extends OpenGraphBaseType
67
{
78
/** @var string */
89
protected $prefix = 'music';

src/Types/OpenGraphWebsite.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Spatie\OpenGraph\Types;
4+
5+
6+
class OpenGraphWebsite extends OpenGraphBaseType
7+
{
8+
/** @var string */
9+
protected $type = 'website';
10+
}

tests/GenerateOpenGraphTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Spatie\Skeleton\Test;
44

55
use PHPUnit\Framework\TestCase;
6-
use Spatie\OpenGraph\OpenGraph;
7-
use Spatie\OpenGraph\OpenGraphBook;
6+
use Spatie\OpenGraph\Types\OpenGraphWebsite;
7+
use Spatie\OpenGraph\Types\OpenGraphBook;
88
use Spatie\OpenGraph\OpenGraphImage;
99
use Spatie\Snapshots\MatchesSnapshots;
1010

@@ -15,7 +15,7 @@ class GenerateOpenGraphTest extends TestCase
1515
/** @test */
1616
public function it_can_generate_basic_metadata()
1717
{
18-
$metaTags = OpenGraph::create('Title', 'http://www.example.com', 'http://example.com/image.jpg')
18+
$metaTags = OpenGraphWebsite::create('Title', 'http://www.example.com', 'http://example.com/image.jpg')
1919
->getMetaTags();
2020

2121
$this->assertMatchesSnapshot($metaTags);
@@ -24,7 +24,7 @@ public function it_can_generate_basic_metadata()
2424
/** @test */
2525
public function it_can_generate_optional_metadata()
2626
{
27-
$metaTags = OpenGraph::create('Title', 'http://www.example.com', 'http://example.com/image.jpg')
27+
$metaTags = OpenGraphWebsite::create('Title', 'http://www.example.com', 'http://example.com/image.jpg')
2828
->description('This is the page description')
2929
->siteName('Test Name')
3030
->locale('en_US')
@@ -38,7 +38,7 @@ public function it_can_generate_optional_metadata()
3838
/** @test */
3939
public function it_can_generate_metadata_for_multiple_images()
4040
{
41-
$metaTags = OpenGraph::create('Title', 'http://www.example.com', 'http://example.com/image.jpg')
41+
$metaTags = OpenGraphWebsite::create('Title', 'http://www.example.com', 'http://example.com/image.jpg')
4242
->image(OpenGraphImage::create('http://example.com/image2.jpg', 'https://example.com/image2.jpg'))
4343
->image(OpenGraphImage::create('http://example.com/image3.jpg', 'https://example.com/image3.jpg', 'image/jpeg', 800, 600))
4444
->getMetaTags();

0 commit comments

Comments
 (0)