DocumentMetadata   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 25
eloc 57
c 4
b 1
f 0
dl 0
loc 137
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D setSpecificAttribute() 0 64 25
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
     * @param mixed $value
81
     * @throws  \Exception
82
     */
83
    protected function setSpecificAttribute(string $key, $value): MetadataInterface
84
    {
85
        if(is_array($value))
86
        {
87
            $value = array_shift($value);
88
        }
89
90
        switch(mb_strtolower($key))
91
        {
92
            case 'dc:title':
93
            case 'title':
94
                $this->title = $value;
95
                break;
96
97
            case 'comments':
98
            case 'w:Comments':
99
                $this->description = $value;
100
                break;
101
102
            case 'keyword':
103
            case 'keywords':
104
            case 'meta:keyword':
105
                $keywords = preg_split(preg_match('/,/', $value) ? '/\s*,\s*/' : '/\s+/', $value);
106
                $this->keywords = array_unique($keywords ?: []);
107
                break;
108
109
            case 'language':
110
                $this->language = mb_substr($value, 0, 2);
111
                break;
112
113
            case 'author':
114
            case 'dc:creator':
115
            case 'initial-creator':
116
                $this->author = $value;
117
                break;
118
119
            case 'application-name':
120
            case 'generator':
121
            case 'producer':
122
                $value = preg_replace('/\$.+/', '', $value);
123
                $this->generator = trim($value);
124
                break;
125
126
            case 'nbpage':
127
            case 'page-count':
128
            case 'xmptpg:npages':
129
                $this->pages = (int) $value;
130
                break;
131
132
            case 'nbword':
133
            case 'word-count':
134
                $this->words = (int) $value;
135
                break;
136
137
            case 'content-encoding':
138
                $this->encoding = $value;
139
                break;
140
141
            case 'x-tika:content':
142
                $this->content = $value;
143
                break;
144
        }
145
146
        return $this;
147
    }
148
}
149