Completed
Pull Request — master (#16)
by Dayle
07:52 queued 05:44
created

Parser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Kurenai;
4
5
use Kurenai\Contracts\ContentParser;
6
use Kurenai\Contracts\MetadataParser;
7
8
/**
9
 * Class Parser
10
 *
11
 * @package \Kurenai
12
 */
13
class Parser
14
{
15
    /**
16
     * Regular expression to split content from metadata.
17
     *
18
     * @var string
19
     */
20
    protected $pattern = '/\s+={3,}\s+/';
21
22
    /**
23
     * The metadata parser instance.
24
     *
25
     * @var \Kurenai\Contracts\MetadataParser
26
     */
27
    protected $metadataParser;
28
29
    /**
30
     * The content parser instance.
31
     *
32
     * @var \Kurenai\Contracts\ContentParser
33
     */
34
    protected $contentParser;
35
36
    /**
37
     * Inject the parser instances.
38
     *
39
     * @param \Kurenai\Contracts\MetadataParser $metadataParser
40
     * @param \Kurenai\Contracts\ContentParser  $contentParser
41
     */
42
    public function __construct(MetadataParser $metadataParser, ContentParser $contentParser)
43
    {
44
        $this->metadataParser = $metadataParser;
45
        $this->contentParser  = $contentParser;
46
    }
47
48
    /**
49
     * Parse a document from string or file path.
50
     *
51
     * @param string $path
52
     *
53
     * @return void
54
     */
55
    public function parse($path)
56
    {
57
        $rawDocument = $this->loadDocument($path);
58
        list($rawMetadata, $rawContent) = $this->splitDocument($rawDocument);
59
        return $this->createDocument($rawDocument, $rawMetadata, $rawContent);
60
    }
61
62
    /**
63
     * Load raw content of document.
64
     *
65
     * @param string $path
66
     *
67
     * @return string
68
     */
69
    protected function loadDocument($path)
70
    {
71
        if (!file_exists($path)) {
72
            return $path;
73
        }
74
75
        return file_get_contents($path);
76
    }
77
78
    /**
79
     * Split a document into content and metadata.
80
     *
81
     * @param string $rawDocument
82
     *
83
     * @return array
84
     */
85
    protected function splitDocument($rawDocument)
86
    {
87
        return preg_split($this->pattern, $rawDocument, 2);
88
    }
89
90
    /**
91
     * Create a new parsed document instance.
92
     *
93
     * @param string $raw
94
     * @param string $rawMetadata
95
     * @param string $rawContent
96
     *
97
     * @return \Kurenai\Document
98
     */
99
    protected function createDocument($raw, $rawMetadata, $rawContent)
100
    {
101
        return new Document(
102
            $raw,
103
            $this->parseMetadata(trim($rawMetadata)),
104
            $this->parseContent(trim($rawContent))
105
        );
106
    }
107
108
    /**
109
     * Parse metadata from raw metadata.
110
     *
111
     * @param string $rawMetadata
112
     *
113
     * @return mixed
114
     */
115
    protected function parseMetadata($rawMetadata)
116
    {
117
        return $this->metadataParser->parse($rawMetadata);
118
    }
119
120
    /**
121
     * Parse content from raw content.
122
     *
123
     * @param string $rawContent
124
     *
125
     * @return string
126
     */
127
    protected function parseContent($rawContent)
128
    {
129
        return $this->contentParser->parse($rawContent);
130
    }
131
}
132