6
6
use Hyde \Framework \Models \MarkdownPost ;
7
7
use Hyde \Framework \Models \Metadata ;
8
8
use Hyde \Framework \Services \AuthorService ;
9
- use JetBrains \PhpStorm \ArrayShape ;
10
9
11
10
/**
12
11
* Generates metadata for page models that have front matter.
16
15
*/
17
16
trait GeneratesPageMetadata
18
17
{
19
- public ?Metadata $ metadata = null ;
18
+ public array $ metadata = [];
19
+ public array $ properties = [];
20
20
21
21
public function constructMetadata (): void
22
22
{
23
- $ this ->metadata = new Metadata ();
24
-
25
23
$ this ->parseFrontMatterMetadata ();
26
24
27
25
if ($ this instanceof MarkdownPost || $ this instanceof \Tests \TestCase) {
28
26
$ this ->makeOpenGraphPropertiesForArticle ();
29
27
}
30
28
}
31
29
32
- #[ArrayShape(['name ' => "\content " ])]
33
30
public function getMetadata (): array
34
31
{
35
- if (! isset ($ this ->metadata )) {
36
- return [];
37
- }
38
-
39
- return $ this ->metadata ->metadata ;
32
+ return $ this ->metadata ;
40
33
}
41
34
42
- #[ArrayShape(['property ' => 'content ' ])]
43
35
public function getMetaProperties (): array
44
36
{
45
- if (! isset ($ this ->metadata )) {
46
- return [];
47
- }
48
-
49
- return $ this ->metadata ->properties ;
37
+ return $ this ->properties ;
50
38
}
51
39
52
40
/**
@@ -56,15 +44,15 @@ public function getMetaProperties(): array
56
44
protected function parseFrontMatterMetadata (): void
57
45
{
58
46
if (isset ($ this ->matter ['description ' ])) {
59
- $ this ->metadata -> add ( 'description ' , $ this ->matter ['description ' ]) ;
47
+ $ this ->metadata [ 'description ' ] = $ this ->matter ['description ' ];
60
48
}
61
49
62
50
if (isset ($ this ->matter ['author ' ])) {
63
- $ this ->metadata -> add ( 'author ' , AuthorService::getAuthorName ($ this ->matter ['author ' ]) );
51
+ $ this ->metadata [ 'author ' ] = AuthorService::getAuthorName ($ this ->matter ['author ' ]);
64
52
}
65
53
66
54
if (isset ($ this ->matter ['category ' ])) {
67
- $ this ->metadata -> add ( 'keywords ' , $ this ->matter ['category ' ]) ;
55
+ $ this ->metadata [ 'keywords ' ] = $ this ->matter ['category ' ];
68
56
}
69
57
}
70
58
@@ -73,30 +61,28 @@ protected function parseFrontMatterMetadata(): void
73
61
*/
74
62
protected function makeOpenGraphPropertiesForArticle (): void
75
63
{
76
- $ this ->metadata ->addProperty ('og:type ' , 'article ' );
77
-
64
+ $ this ->properties ['og:type ' ] = 'article ' ;
78
65
if (Hyde::uriPath ()) {
79
- $ this ->metadata -> addProperty ( 'og:url ' , Hyde::uriPath (Hyde::pageLink ('posts/ ' .$ this ->slug .'.html ' ) ));
66
+ $ this ->properties [ 'og:url ' ] = Hyde::uriPath (Hyde::pageLink ('posts/ ' .$ this ->slug .'.html ' ));
80
67
}
81
68
82
69
if (isset ($ this ->matter ['title ' ])) {
83
- $ this ->metadata -> addProperty ( 'og:title ' , $ this ->matter ['title ' ]) ;
70
+ $ this ->properties [ 'og:title ' ] = $ this ->matter ['title ' ];
84
71
}
85
72
86
73
if (isset ($ this ->matter ['date ' ])) {
87
- $ date = date ('c ' , strtotime ($ this ->matter ['date ' ]));
88
- $ this ->metadata ->addProperty ('og:article:published_time ' , $ date );
74
+ $ this ->properties ['og:article:published_time ' ] = date ('c ' , strtotime ($ this ->matter ['date ' ]));
89
75
}
90
76
91
77
if (isset ($ this ->matter ['image ' ])) {
92
78
if (is_string ($ this ->matter ['image ' ])) {
93
- $ this ->metadata -> addProperty ( 'og:image ' , $ this ->matter ['image ' ]) ;
79
+ $ this ->properties [ 'og:image ' ] = $ this ->matter ['image ' ];
94
80
} else {
95
81
if (isset ($ this ->matter ['image ' ]['path ' ])) {
96
- $ this ->metadata -> addProperty ( 'og:image ' , $ this ->matter ['image ' ]['path ' ]) ;
82
+ $ this ->properties [ 'og:image ' ] = $ this ->matter ['image ' ]['path ' ];
97
83
}
98
84
if (isset ($ this ->matter ['image ' ]['uri ' ])) {
99
- $ this ->metadata -> addProperty ( 'og:image ' , $ this ->matter ['image ' ]['uri ' ]) ;
85
+ $ this ->properties [ 'og:image ' ] = $ this ->matter ['image ' ]['uri ' ];
100
86
}
101
87
}
102
88
}
0 commit comments