Passed
Push — master ( 2d09f8...2e744a )
by Dayle
03:23 queued 41s
created

Document::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kurenai;
4
5
/**
6
 * Class Document
7
 *
8
 * @package \Kurenai
9
 */
10
class Document
11
{
12
    /**
13
     * Raw document content.
14
     *
15
     * @var string
16
     */
17
    protected $raw;
18
19
    /**
20
     * Document metadata.
21
     *
22
     * @var mixed
23
     */
24
    protected $metadata;
25
26
    /**
27
     * Document content.
28
     *
29
     * @var string
30
     */
31
    protected $content;
32
33
    /**
34
     * Document constructor.
35
     *
36
     * @param string $raw
37
     * @param mixed  $metdata
38
     * @param string $content
39
     */
40 8
    public function __construct($raw, $metdata, $content)
41
    {
42 8
        $this->raw      = $raw;
43 8
        $this->metadata = $metdata;
44 8
        $this->content  = $content;
45 8
    }
46
47
    /**
48
     * Get the raw document.
49
     *
50
     * @return string
51
     */
52 2
    public function getRaw()
53
    {
54 2
        return $this->raw;
55
    }
56
57
    /**
58
     * Get the document metadata.
59
     *
60
     * @return mixed
61
     */
62 2
    public function getMetadata()
63
    {
64 2
        return $this->metadata;
65
    }
66
67
    /**
68
     * Get the document content.
69
     *
70
     * @return string
71
     */
72 2
    public function getContent()
73
    {
74 2
        return $this->content;
75
    }
76
77
    /**
78
     * Get a metadata value using dot notation.
79
     *
80
     * @param string $key
81
     * @param mixed  $default
82
     *
83
     * @return mixed
84
     */
85 2
    public function get($key, $default = null)
86
    {
87 2
        $array = $this->metadata;
88
89 2
        if (is_null($key)) {
90
            return $array;
91
        }
92 2
        if (isset($array[$key])) {
93
            return $array[$key];
94
        }
95 2
        foreach (explode('.', $key) as $segment) {
96 2
            if (! is_array($array) || ! array_key_exists($segment, $array)) {
97 1
                return $default;
98
            }
99 1
            $array = $array[$segment];
100 1
        }
101 1
        return $array;
102
    }
103
}
104