Passed
Push — develop ( 446210...89516f )
by Schlaefer
02:39
created

Meta::parse()   B

Complexity

Conditions 6
Paths 15

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 18
cts 18
cp 1
rs 8.439
cc 6
eloc 19
nc 15
nop 1
crap 6
1
<?php
2
/**
3
 * The Meta Parser Interface
4
 */
5
namespace Phile\Plugin\Phile\ParserMeta\Parser;
6
7
use Phile\ServiceLocator\MetaInterface;
8
use Symfony\Component\Yaml\Yaml;
9
10
/**
11
 * Class Meta
12
 *
13
 * @author  PhileCMS
14
 * @link    https://philecms.com
15
 * @license http://opensource.org/licenses/MIT
16
 * @package Phile\Plugin\Phile\ParserMeta\Parser
17
 */
18
class Meta implements MetaInterface
19
{
20
    /**
21
 * @var array $config the configuration for this parser
22
*/
23
    private $config;
24
25
    /**
26
     * the constructor
27
     *
28
     * @param array $config
29
     */
30 27
    public function __construct(array $config = null)
31
    {
32 27
        if (!is_null($config)) {
33 27
            $this->config = $config;
34
        }
35 27
    }
36
37
    /**
38
     * parse the content and extract meta information
39
     *
40
     * @param  string $rawData raw page data
41
     * @return array with key/value store
42
     */
43 25
    public function parse($rawData)
44
    {
45 25
        $rawData = trim($rawData);
46 25
        $fences = $this->config['fences'];
47
48 25
        $start = $stop = null;
49 25
        foreach ($fences as $fence) {
50 25
            $start = $fence['open'];
51 25
            $length = strlen($start);
52 25
            if (substr($rawData, 0, $length) === $start) {
53 24
                $stop = $fence['close'];
54 25
                break;
55
            }
56
        }
57
58 25
        if ($stop === null) {
59 2
            return [];
60
        }
61
62 24
        $meta = trim(substr($rawData, strlen($start), strpos($rawData, $stop, strlen($start)) - (strlen($stop) + 1)));
63 24
        if (strtolower($this->config['format']) === 'yaml') {
64 1
            $meta = Yaml::parse($meta);
65
        } else {
66 23
            $meta = $this->parsePhileFormat($meta);
0 ignored issues
show
Deprecated Code introduced by
The method Phile\Plugin\Phile\Parse...eta::parsePhileFormat() has been deprecated with message: since 1.6.0 Phile is going to switch to YAML

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
67
        }
68 24
        $meta = ($meta === null) ? [] : $this->convertKeys($meta);
69 24
        return $meta;
70
    }
71
72
    /**
73
     * convert meta data keys
74
     *
75
     * Creates "compatible" keys allowing easy access e.g. as template var.
76
     *
77
     * Conversions applied:
78
     *
79
     * - lowercase all chars
80
     * - replace special chars and whitespace with underscore
81
     *
82
     * @param  array $meta meta-data
83
     * @return array
84
     */
85 23
    protected function convertKeys(array $meta)
86
    {
87 23
        $return = [];
88 23
        foreach ($meta as $key => $value) {
89 23
            if (is_array($value)) {
90 1
                $value = $this->convertKeys($value);
91
            }
92 23
            $newKey = strtolower($key);
93 23
            $newKey = preg_replace('/[^\w+]/', '_', $newKey);
94 23
            $return[$newKey] = $value;
95
        }
96 23
        return $return;
97
    }
98
99
    /**
100
     * Phile meta format parser.
101
     *
102
     * @param  string $string unparsed meta-data
103
     * @return array|null array with meta-tags; null: on meta-data found
104
     *
105
     * @deprecated since 1.6.0 Phile is going to switch to YAML
106
     */
107 23
    protected function parsePhileFormat($string)
108
    {
109 23
        if (empty($string)) {
110 1
            return null;
111
        }
112 22
        $meta = [];
113 22
        $lines = explode("\n", $string);
114 22
        foreach ($lines as $line) {
115 22
            $parts = explode(':', $line, 2);
116 22
            if (count($parts) !== 2) {
117
                continue;
118
            }
119 22
            $parts = array_map('trim', $parts);
120 22
            $meta[$parts[0]] = $parts[1];
121
        }
122 22
        return $meta;
123
    }
124
}
125