Passed
Push — master ( d8574a...8cadc1 )
by David
02:55
created

DocumentMetadata   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 21
eloc 53
c 4
b 1
f 0
dl 0
loc 132
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D setSpecificAttribute() 0 60 21
1
<?php
2
3
namespace Vaites\ApacheTika\Metadata;
4
5
/**
6
 * Metadata class for documents
7
 *
8
 * @author  David Martínez <[email protected]>
9
 */
10
class DocumentMetadata extends Metadata
11
{
12
    /**
13
     * Title (if not detected by Apache Tika, name without extension is used)
14
     *
15
     * @var string
16
     */
17
    public $title = null;
18
19
    /**
20
     * Description.
21
     *
22
     * @var string
23
     */
24
    public $description = null;
25
26
    /**
27
     * Keywords
28
     *
29
     * @var array
30
     */
31
    public $keywords = [];
32
33
    /**
34
     * Two-letter language code (ISO-639-1)
35
     *
36
     * @link https://en.wikipedia.org/wiki/ISO_639-1
37
     *
38
     * @var string
39
     */
40
    public $language = null;
41
42
    /**
43
     * Content encoding
44
     *
45
     * @var string
46
     */
47
    public $encoding = null;
48
49
    /**
50
     * Author
51
     *
52
     * @var string
53
     */
54
    public $author = null;
55
56
    /**
57
     * Software used to generate document
58
     *
59
     * @var string
60
     */
61
    public $generator = null;
62
63
    /**
64
     * Number of pages
65
     *
66
     * @var int
67
     */
68
    public $pages = 0;
69
70
    /**
71
     * Number of words.
72
     *
73
     * @var int
74
     */
75
    public $words = 0;
76
77
    /**
78
     * Sets an attribute
79
     *
80
     * @throws  \Exception
81
     */
82
    protected function setSpecificAttribute(string $key, $value): MetadataInterface
83
    {
84
        if(is_array($value))
85
        {
86
            $value = array_shift($value);
87
        }
88
89
        switch(mb_strtolower($key))
90
        {
91
            case 'title':
92
                $this->title = $value;
93
                break;
94
95
            case 'comments':
96
                $this->description = $value;
97
                break;
98
99
            case 'keyword':
100
            case 'keywords':
101
                $keywords = preg_split(preg_match('/,/', $value) ? '/\s*,\s*/' : '/\s+/', $value);
102
                $this->keywords = array_unique($keywords ?: []);
103
                break;
104
105
            case 'language':
106
                $this->language = mb_substr($value, 0, 2);
107
                break;
108
109
            case 'author':
110
            case 'initial-creator':
111
                $this->author = $value;
112
                break;
113
114
            case 'application-name':
115
            case 'generator':
116
            case 'producer':
117
                $value = preg_replace('/\$.+/', '', $value);
118
                $this->generator = trim($value);
119
                break;
120
121
            case 'nbpage':
122
            case 'page-count':
123
            case 'xmptpg:npages':
124
                $this->pages = (int) $value;
125
                break;
126
127
            case 'nbword':
128
            case 'word-count':
129
                $this->words = (int) $value;
130
                break;
131
132
            case 'content-encoding':
133
                $this->encoding = $value;
134
                break;
135
136
            case 'x-tika:content':
137
                $this->content = $value;
138
                break;
139
        }
140
141
        return $this;
142
    }
143
}
144