Skip to content

Commit 02aa1ab

Browse files
committed
Make the builders even more developer friendly
1 parent 8153edf commit 02aa1ab

File tree

8 files changed

+346
-245
lines changed

8 files changed

+346
-245
lines changed

src/OpenGraph/OpenGraph.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct()
4646
$this->type = new Website();
4747
}
4848

49-
public function title(string $title): self
49+
public function title(?string $title): self
5050
{
5151
$this->title = $title;
5252

@@ -58,9 +58,11 @@ public function getTitle(): ?string
5858
return $this->title;
5959
}
6060

61-
public function type(TypeInterface $type): self
61+
public function type(?TypeInterface $type): self
6262
{
63-
$this->type = $type;
63+
if (null !== $type) {
64+
$this->type = $type;
65+
}
6466

6567
return $this;
6668
}
@@ -70,7 +72,7 @@ public function getType(): TypeInterface
7072
return $this->type;
7173
}
7274

73-
public function url(string $url): self
75+
public function url(?string $url): self
7476
{
7577
$this->url = $url;
7678

@@ -82,7 +84,7 @@ public function getUrl(): ?string
8284
return $this->url;
8385
}
8486

85-
public function description(string $description): self
87+
public function description(?string $description): self
8688
{
8789
$this->description = $description;
8890

@@ -98,7 +100,7 @@ public function getDescription(): ?string
98100
* Set the word that appears before this object's title in a sentence.
99101
* Should be one of: "a", "an", "the", "", "auto".
100102
*/
101-
public function determiner(string $determiner): self
103+
public function determiner(?string $determiner): self
102104
{
103105
$this->determiner = $determiner;
104106

@@ -114,7 +116,7 @@ public function getDeterminer(): ?string
114116
* Set the locale these tags are marked up in.
115117
* Format: language_TERRITORY (e.g., "en_US", "fr_FR").
116118
*/
117-
public function locale(string $locale): self
119+
public function locale(?string $locale): self
118120
{
119121
$this->locale = $locale;
120122

@@ -126,9 +128,11 @@ public function getLocale(): ?string
126128
return $this->locale;
127129
}
128130

129-
public function localeAlternate(string $locale): self
131+
public function localeAlternate(?string $locale): self
130132
{
131-
$this->localeAlternates[] = $locale;
133+
if (null !== $locale) {
134+
$this->localeAlternates[] = $locale;
135+
}
132136

133137
return $this;
134138
}
@@ -141,7 +145,7 @@ public function getLocaleAlternates(): array
141145
return $this->localeAlternates;
142146
}
143147

144-
public function siteName(string $siteName): self
148+
public function siteName(?string $siteName): self
145149
{
146150
$this->siteName = $siteName;
147151

@@ -156,10 +160,14 @@ public function getSiteName(): ?string
156160
/**
157161
* Add an image to represent your object.
158162
*
159-
* @param Image|string $image Image object or URL
163+
* @param Image|string|null $image Image object or URL
160164
*/
161-
public function image(Image|string $image): self
165+
public function image(Image|string|null $image): self
162166
{
167+
if (null === $image) {
168+
return $this;
169+
}
170+
163171
if (is_string($image)) {
164172
$image = new Image($image);
165173
}
@@ -180,10 +188,14 @@ public function getImages(): array
180188
/**
181189
* Add a video to complement your object.
182190
*
183-
* @param Video|string $video Video object or URL
191+
* @param Video|string|null $video Video object or URL
184192
*/
185-
public function video(Video|string $video): self
193+
public function video(Video|string|null $video): self
186194
{
195+
if (null === $video) {
196+
return $this;
197+
}
198+
187199
if (is_string($video)) {
188200
$video = new Video($video);
189201
}
@@ -204,10 +216,14 @@ public function getVideos(): array
204216
/**
205217
* Add an audio file to complement your object.
206218
*
207-
* @param Audio|string $audio Audio object or URL
219+
* @param Audio|string|null $audio Audio object or URL
208220
*/
209-
public function audio(Audio|string $audio): self
221+
public function audio(Audio|string|null $audio): self
210222
{
223+
if (null === $audio) {
224+
return $this;
225+
}
226+
211227
if (is_string($audio)) {
212228
$audio = new Audio($audio);
213229
}

src/OpenGraph/Property/Audio.php

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,48 @@
1111
*/
1212
final class Audio
1313
{
14-
/**
15-
* @param string $url The URL of the audio file
16-
* @param string|null $secureUrl An HTTPS URL for the audio file
17-
* @param string|null $type The MIME type of the audio file (e.g., "audio/mpeg")
18-
*/
19-
public function __construct(
20-
public readonly string $url,
21-
public readonly ?string $secureUrl = null,
22-
public readonly ?string $type = null,
23-
) {
14+
private ?string $secureUrl = null;
15+
16+
private ?string $type = null;
17+
18+
public function __construct(private string $url)
19+
{
20+
}
21+
22+
public function url(string $url): self
23+
{
24+
$this->url = $url;
25+
26+
return $this;
2427
}
2528

26-
public static function create(string $url): self
29+
public function getUrl(): string
2730
{
28-
return new self($url);
31+
return $this->url;
2932
}
3033

31-
public function withSecureUrl(string $secureUrl): self
34+
public function secureUrl(?string $secureUrl): self
3235
{
33-
return new self($this->url, $secureUrl, $this->type);
36+
$this->secureUrl = $secureUrl;
37+
38+
return $this;
39+
}
40+
41+
public function getSecureUrl(): ?string
42+
{
43+
return $this->secureUrl;
44+
}
45+
46+
public function type(?string $type): self
47+
{
48+
$this->type = $type;
49+
50+
return $this;
3451
}
3552

36-
public function withType(string $type): self
53+
public function getType(): ?string
3754
{
38-
return new self($this->url, $this->secureUrl, $type);
55+
return $this->type;
3956
}
4057

4158
/**

src/OpenGraph/Property/Image.php

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,98 @@
1111
*/
1212
final class Image
1313
{
14-
/**
15-
* @param string $url The URL of the image
16-
* @param string|null $secureUrl An HTTPS URL for the image
17-
* @param string|null $type The MIME type of the image (e.g., "image/jpeg")
18-
* @param int|null $width The width of the image in pixels
19-
* @param int|null $height The height of the image in pixels
20-
* @param string|null $alt A description of what is in the image (for accessibility)
21-
*/
22-
public function __construct(
23-
public readonly string $url,
24-
public readonly ?string $secureUrl = null,
25-
public readonly ?string $type = null,
26-
public readonly ?int $width = null,
27-
public readonly ?int $height = null,
28-
public readonly ?string $alt = null,
29-
) {
14+
private ?string $secureUrl = null;
15+
16+
private ?string $type = null;
17+
18+
private ?int $width = null;
19+
20+
private ?int $height = null;
21+
22+
private ?string $alt = null;
23+
24+
public function __construct(private string $url)
25+
{
3026
}
3127

32-
public static function create(string $url): self
28+
public function url(string $url): self
3329
{
34-
return new self($url);
30+
$this->url = $url;
31+
32+
return $this;
3533
}
3634

37-
public function withSecureUrl(string $secureUrl): self
35+
public function getUrl(): string
3836
{
39-
return new self($this->url, $secureUrl, $this->type, $this->width, $this->height, $this->alt);
37+
return $this->url;
4038
}
4139

42-
public function withType(string $type): self
40+
public function secureUrl(?string $secureUrl): self
4341
{
44-
return new self($this->url, $this->secureUrl, $type, $this->width, $this->height, $this->alt);
42+
$this->secureUrl = $secureUrl;
43+
44+
return $this;
45+
}
46+
47+
public function getSecureUrl(): ?string
48+
{
49+
return $this->secureUrl;
4550
}
4651

47-
public function withWidth(int $width): self
52+
public function type(?string $type): self
4853
{
49-
return new self($this->url, $this->secureUrl, $this->type, $width, $this->height, $this->alt);
54+
$this->type = $type;
55+
56+
return $this;
5057
}
5158

52-
public function withHeight(int $height): self
59+
public function getType(): ?string
5360
{
54-
return new self($this->url, $this->secureUrl, $this->type, $this->width, $height, $this->alt);
61+
return $this->type;
5562
}
5663

57-
public function withDimensions(int $width, int $height): self
64+
public function width(?int $width): self
5865
{
59-
return new self($this->url, $this->secureUrl, $this->type, $width, $height, $this->alt);
66+
$this->width = $width;
67+
68+
return $this;
69+
}
70+
71+
public function getWidth(): ?int
72+
{
73+
return $this->width;
74+
}
75+
76+
public function height(?int $height): self
77+
{
78+
$this->height = $height;
79+
80+
return $this;
81+
}
82+
83+
public function getHeight(): ?int
84+
{
85+
return $this->height;
86+
}
87+
88+
public function dimensions(?int $width, ?int $height): self
89+
{
90+
$this->width = $width;
91+
$this->height = $height;
92+
93+
return $this;
94+
}
95+
96+
public function alt(?string $alt): self
97+
{
98+
$this->alt = $alt;
99+
100+
return $this;
60101
}
61102

62-
public function withAlt(string $alt): self
103+
public function getAlt(): ?string
63104
{
64-
return new self($this->url, $this->secureUrl, $this->type, $this->width, $this->height, $alt);
105+
return $this->alt;
65106
}
66107

67108
/**

0 commit comments

Comments
 (0)