4
4
* PHP YAML FrontMatter Class
5
5
* An easy to use class for handling YAML frontmatter in PHP.
6
6
*
7
- * @author Blaxus
8
7
* @package Modularr/YAML-FrontMatter
9
8
*/
10
9
class FrontMatter
@@ -14,7 +13,7 @@ class FrontMatter
14
13
* @param array $data metadata & content
15
14
*/
16
15
public $ data ;
17
-
16
+
18
17
/**
19
18
* Constructor method, checks a file and then puts the contents into custom strings for usage
20
19
* @param string $file The input file
@@ -24,13 +23,12 @@ public function __construct($file)
24
23
$ file = (file_exists ($ file )) ? $ this ->Read ($ file ) : str_replace (array ("\r\n" , "\r" , "\n" ), "\n" , $ file );
25
24
$ this ->yaml_separator = "--- \n" ;
26
25
$ fm = $ this ->FrontMatter ($ file );
27
-
28
- foreach ($ fm as $ key => $ value )
29
- {
26
+
27
+ foreach ($ fm as $ key => $ value ) {
30
28
$ this ->data [$ key ] = $ value ;
31
29
}
32
30
}
33
-
31
+
34
32
/**
35
33
* fetch method returns the value of a given key
36
34
* @return string $value The value for a given key
@@ -39,7 +37,7 @@ public function fetch($key)
39
37
{
40
38
return $ this ->data [$ key ];
41
39
}
42
-
40
+
43
41
/**
44
42
* keyExists method Checks to see if a key exists
45
43
* @return bool
@@ -49,7 +47,7 @@ public function keyExists($key)
49
47
#return (isset($this->data[$key])) ? true : false; # Isset Version
50
48
return array_key_exists ($ key , $ this ->data ); # array_key_exists version
51
49
}
52
-
50
+
53
51
/**
54
52
* fetchKeys method returns an array of all meta data without the content
55
53
* @return [array] collection of all meta keys provided to FrontMatter
@@ -58,22 +56,21 @@ public function fetchKeys()
58
56
{
59
57
# Cache the keys so we don't edit the native object data
60
58
$ keys = $ this ->data ;
61
-
59
+
62
60
# Remove $data[content] from the keys so we only have the meta data
63
61
array_pop ($ keys );
64
-
62
+
65
63
return $ keys ;
66
64
}
67
-
65
+
68
66
/**
69
67
* FrontMatter method, rturns all the variables from a YAML Frontmatter input
70
68
* @param string $input The input string
71
69
* @return array $final returns all variables in an array
72
70
*/
73
71
function FrontMatter ($ input )
74
72
{
75
- if (!$ this ->startsWith ($ input , $ this ->yaml_separator ))
76
- {
73
+ if (!$ this ->startsWith ($ input , $ this ->yaml_separator )) {
77
74
# No front matter
78
75
# Store Content in Final array
79
76
$ final ['content ' ] = $ input ;
@@ -83,9 +80,8 @@ function FrontMatter($input)
83
80
84
81
# Explode Seperators. At most, make three pieces out of the input file
85
82
$ document = explode ($ this ->yaml_separator ,$ input , 3 );
86
-
87
- switch ( sizeof ($ document ) )
88
- {
83
+
84
+ switch ( sizeof ($ document ) ) {
89
85
case 0 :
90
86
case 1 :
91
87
// Empty document
@@ -102,21 +98,21 @@ function FrontMatter($input)
102
98
$ front_matter = $ document [1 ];
103
99
$ content = $ document [2 ];
104
100
}
105
-
101
+
106
102
# Parse YAML
107
103
try {
108
104
$ final = Yaml::parse ($ front_matter );
109
105
} catch (ParseException $ e ) {
110
106
printf ("Unable to parse the YAML string: %s " , $ e ->getMessage ());
111
107
}
112
-
108
+
113
109
# Store Content in Final array
114
110
$ final ['content ' ] = $ content ;
115
-
111
+
116
112
# Return Final array
117
113
return $ final ;
118
114
}
119
-
115
+
120
116
/**
121
117
* A convenience wrapper around strpos to check the start of a string
122
118
* From http://stackoverflow.com/a/860509/270334
@@ -128,7 +124,7 @@ private function startsWith($haystack,$needle,$case=true)
128
124
return strpos ($ haystack , $ needle , 0 ) === 0 ;
129
125
return stripos ($ haystack , $ needle , 0 ) === 0 ;
130
126
}
131
-
127
+
132
128
/**
133
129
* Read Method, Read file and returns it's contents
134
130
* @return string $data returned data
@@ -137,25 +133,21 @@ protected function Read($file)
137
133
{
138
134
# Open File
139
135
$ fh = fopen ($ file , 'r ' );
140
-
141
136
$ fileSize = filesize ($ file );
142
-
143
- if (!empty ($ fileSize ))
144
- {
137
+
138
+ if (!empty ($ fileSize )) {
145
139
# Read Data
146
140
$ data = fread ($ fh , $ fileSize );
147
-
141
+
148
142
# Fix Data Stream to be the exact same format as PHP's strings
149
143
$ data = str_replace (array ("\r\n" , "\r" , "\n" ), "\n" , $ data );
150
- }
151
- else
152
- {
144
+ } else {
153
145
$ data = '' ;
154
146
}
155
-
147
+
156
148
# Close File
157
149
fclose ($ fh );
158
-
150
+
159
151
# Return Data
160
152
return $ data ;
161
153
}
0 commit comments