Skip to content

Commit e6e0400

Browse files
author
modbot
committed
Commit
1 parent 60b6544 commit e6e0400

File tree

2 files changed

+23
-52
lines changed

2 files changed

+23
-52
lines changed

.github/CONTRIBUTING.md

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

src/frontmatter.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* PHP YAML FrontMatter Class
55
* An easy to use class for handling YAML frontmatter in PHP.
66
*
7-
* @author Blaxus
87
* @package Modularr/YAML-FrontMatter
98
*/
109
class FrontMatter
@@ -14,7 +13,7 @@ class FrontMatter
1413
* @param array $data metadata & content
1514
*/
1615
public $data;
17-
16+
1817
/**
1918
* Constructor method, checks a file and then puts the contents into custom strings for usage
2019
* @param string $file The input file
@@ -24,13 +23,12 @@ public function __construct($file)
2423
$file = (file_exists($file)) ? $this->Read($file) : str_replace(array("\r\n", "\r", "\n"), "\n", $file);
2524
$this->yaml_separator = "---\n";
2625
$fm = $this->FrontMatter($file);
27-
28-
foreach($fm as $key => $value)
29-
{
26+
27+
foreach($fm as $key => $value) {
3028
$this->data[$key] = $value;
3129
}
3230
}
33-
31+
3432
/**
3533
* fetch method returns the value of a given key
3634
* @return string $value The value for a given key
@@ -39,7 +37,7 @@ public function fetch($key)
3937
{
4038
return $this->data[$key];
4139
}
42-
40+
4341
/**
4442
* keyExists method Checks to see if a key exists
4543
* @return bool
@@ -49,7 +47,7 @@ public function keyExists($key)
4947
#return (isset($this->data[$key])) ? true : false; # Isset Version
5048
return array_key_exists($key, $this->data); # array_key_exists version
5149
}
52-
50+
5351
/**
5452
* fetchKeys method returns an array of all meta data without the content
5553
* @return [array] collection of all meta keys provided to FrontMatter
@@ -58,22 +56,21 @@ public function fetchKeys()
5856
{
5957
# Cache the keys so we don't edit the native object data
6058
$keys = $this->data;
61-
59+
6260
# Remove $data[content] from the keys so we only have the meta data
6361
array_pop($keys);
64-
62+
6563
return $keys;
6664
}
67-
65+
6866
/**
6967
* FrontMatter method, rturns all the variables from a YAML Frontmatter input
7068
* @param string $input The input string
7169
* @return array $final returns all variables in an array
7270
*/
7371
function FrontMatter($input)
7472
{
75-
if (!$this->startsWith($input, $this->yaml_separator))
76-
{
73+
if (!$this->startsWith($input, $this->yaml_separator)) {
7774
# No front matter
7875
# Store Content in Final array
7976
$final['content'] = $input;
@@ -83,9 +80,8 @@ function FrontMatter($input)
8380

8481
# Explode Seperators. At most, make three pieces out of the input file
8582
$document = explode($this->yaml_separator,$input, 3);
86-
87-
switch( sizeof($document) )
88-
{
83+
84+
switch( sizeof($document) ) {
8985
case 0:
9086
case 1:
9187
// Empty document
@@ -102,21 +98,21 @@ function FrontMatter($input)
10298
$front_matter = $document[1];
10399
$content = $document[2];
104100
}
105-
101+
106102
# Parse YAML
107103
try {
108104
$final = Yaml::parse($front_matter);
109105
} catch (ParseException $e) {
110106
printf("Unable to parse the YAML string: %s", $e->getMessage());
111107
}
112-
108+
113109
# Store Content in Final array
114110
$final['content'] = $content;
115-
111+
116112
# Return Final array
117113
return $final;
118114
}
119-
115+
120116
/**
121117
* A convenience wrapper around strpos to check the start of a string
122118
* From http://stackoverflow.com/a/860509/270334
@@ -128,7 +124,7 @@ private function startsWith($haystack,$needle,$case=true)
128124
return strpos($haystack, $needle, 0) === 0;
129125
return stripos($haystack, $needle, 0) === 0;
130126
}
131-
127+
132128
/**
133129
* Read Method, Read file and returns it's contents
134130
* @return string $data returned data
@@ -137,25 +133,21 @@ protected function Read($file)
137133
{
138134
# Open File
139135
$fh = fopen($file, 'r');
140-
141136
$fileSize = filesize($file);
142-
143-
if(!empty($fileSize))
144-
{
137+
138+
if(!empty($fileSize)) {
145139
# Read Data
146140
$data = fread($fh, $fileSize);
147-
141+
148142
# Fix Data Stream to be the exact same format as PHP's strings
149143
$data = str_replace(array("\r\n", "\r", "\n"), "\n", $data);
150-
}
151-
else
152-
{
144+
} else {
153145
$data = '';
154146
}
155-
147+
156148
# Close File
157149
fclose($fh);
158-
150+
159151
# Return Data
160152
return $data;
161153
}

0 commit comments

Comments
 (0)