Matter::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 2
rs 9.9666
1
<?php
2
3
namespace Staticka;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
/**
8
 * TODO: Remove this file after v1.0.0.
9
 * Move the code to PageFactory::parse instead.
10
 *
11
 * YAML Front Matter Parser
12
 *
13
 * @package Staticka
14
 * @author  Rougin Gutib <[email protected]>
15
 */
16
class Matter
17
{
18
    /**
19
     * Retrieves the contents from a YAML format.
20
     *
21
     * @param  string $content
22
     * @return array
23
     */
24 12
    public static function parse($content)
25
    {
26 12
        $matter = array();
27
28 12
        $text = str_replace(PHP_EOL, $id = uniqid(), $content);
29
30 12
        $regex = '/^---' . $id . '(.*?)' . $id . '---/';
31
32 12
        if (preg_match($regex, $text, $matches) === 1)
33 4
        {
34 3
            $yaml = str_replace($id, PHP_EOL, $matches[1]);
35
36 3
            $matter = (array) Yaml::parse(trim($yaml));
37
38 3
            $body = str_replace($matches[0], '', $text);
39
40 3
            $content = str_replace($id, PHP_EOL, $body);
41 1
        }
42
43 12
        return array($matter, (string) trim($content));
44
    }
45
}
46