Passed
Pull Request — develop (#352)
by Schlaefer
02:14
created

Meta   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 97.92%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 45
c 3
b 0
f 0
dl 0
loc 130
ccs 47
cts 48
cp 0.9792
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A extractContent() 0 5 1
A extractMeta() 0 15 4
A parsePhileFormat() 0 16 4
A splitRawIntoMetaAndContent() 0 18 4
A convertKeys() 0 12 3
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.github.io
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 35
    public function __construct(array $config = null)
31
    {
32 35
        if (!is_null($config)) {
33 35
            $this->config = $config;
34
        }
35
    }
36
37
    /**
38
     * Implements MetaInterface::parse
39
     *
40
     * {@inheritdoc}
41
     */
42 28
    public function extractMeta($rawData)
43
    {
44 28
        list($meta) = $this->splitRawIntoMetaAndContent($rawData);
45
        
46 28
        if ($meta === null) {
47 2
            return [];
48
        }
49
50 27
        if (strtolower($this->config['format']) === 'yaml') {
51 1
            $meta = Yaml::parse($meta);
52
        } else {
53 26
            $meta = $this->parsePhileFormat($meta);
0 ignored issues
show
Deprecated Code introduced by
The function Phile\Plugin\Phile\Parse...eta::parsePhileFormat() has been deprecated: since 1.6.0 Phile is going to switch to YAML ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
            $meta = /** @scrutinizer ignore-deprecated */ $this->parsePhileFormat($meta);

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

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

Loading history...
54
        }
55 27
        $meta = ($meta === null) ? [] : $this->convertKeys($meta);
56 27
        return $meta;
57
    }
58
59
    /**
60
     * Implements MetaInterface::extractContent
61
     *
62
     * {@inheritdoc}
63
     */
64 22
    public function extractContent(?string $rawData): string
65
    {
66 22
        list(, $content) = $this->splitRawIntoMetaAndContent($rawData);
0 ignored issues
show
Bug introduced by
It seems like $rawData can also be of type null; however, parameter $rawData of Phile\Plugin\Phile\Parse...RawIntoMetaAndContent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        list(, $content) = $this->splitRawIntoMetaAndContent(/** @scrutinizer ignore-type */ $rawData);
Loading history...
67
        
68 22
        return $content;
69
    }
70
71
    /**
72
     * Inspects the text and splits meta-data and content
73
     *
74
     * @param string $rawData Text to inspect
75
     * @return array array with [meta-data, content]
76
     */
77 28
    protected function splitRawIntoMetaAndContent(string $rawData): array
78
    {
79 28
        $meta = null;
80 28
        $content = $rawData;
81
82 28
        if ($rawData !== null) {
0 ignored issues
show
introduced by
The condition $rawData !== null is always true.
Loading history...
83 28
            $rawData = trim($rawData);
84 28
            $fences = $this->config['fences'];
85 28
            foreach ($fences as $fence) {
86 28
                if (strncmp($fence['open'], $rawData, strlen($fence['open'])) === 0) {
87 27
                    $sub = substr($rawData, strlen($fence['open']));
88 27
                    list($meta, $content) = explode($fence['close'], $sub, 2);
89 27
                    break;
90
                }
91
            }
92
        }
93
94 28
        return [$meta, $content];
95
    }
96
97
    /**
98
     * convert meta data keys
99
     *
100
     * Creates "compatible" keys allowing easy access e.g. as template var.
101
     *
102
     * Conversions applied:
103
     *
104
     * - lowercase all chars
105
     * - replace special chars and whitespace with underscore
106
     *
107
     * @param  array $meta meta-data
108
     * @return array
109
     */
110 27
    protected function convertKeys(array $meta)
111
    {
112 27
        $return = [];
113 27
        foreach ($meta as $key => $value) {
114 26
            if (is_array($value)) {
115 1
                $value = $this->convertKeys($value);
116
            }
117 26
            $newKey = strtolower($key);
118 26
            $newKey = preg_replace('/[^\w+]/', '_', $newKey);
119 26
            $return[$newKey] = $value;
120
        }
121 27
        return $return;
122
    }
123
124
    /**
125
     * Phile meta format parser.
126
     *
127
     * @param  string $string unparsed meta-data
128
     * @return array|null array with meta-tags; null: on meta-data found
129
     *
130
     * @deprecated since 1.6.0 Phile is going to switch to YAML
131
     */
132 26
    protected function parsePhileFormat($string)
133
    {
134 26
        if (empty($string)) {
135
            return null;
136
        }
137 26
        $meta = [];
138 26
        $lines = explode("\n", $string);
139 26
        foreach ($lines as $line) {
140 26
            $parts = explode(':', $line, 2);
141 26
            if (count($parts) !== 2) {
142 26
                continue;
143
            }
144 25
            $parts = array_map('trim', $parts);
145 25
            $meta[$parts[0]] = $parts[1];
146
        }
147 26
        return $meta;
148
    }
149
}
150