Completed
Push — bibtex-refactor ( 88494c...f5535a )
by mw
63:25 queued 43:27
created

Item::buildURI()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
cc 7
nc 12
nop 0
1
<?php
2
3
namespace SRF\BibTex;
4
5
use SMWDataValue as DataValue;
6
7
/**
8
 * @see http://www.semantic-mediawiki.org/wiki/BibTex
9
 *
10
 * @license GNU GPL v2+
11
 * @since 3.1
12
 *
13
 * @author mwjames
14
 */
15
class Item {
16
17
	/**
18
	 * @see https://en.wikipedia.org/wiki/BibTeX
19
	 *
20
	 * @var string
21
	 */
22
	private $type = '';
23
24
	/**
25
	 * @var []
26
	 */
27
	private $fields = [
28
		'address' => '',
29
		'annote' => '',
30
		'author' => [],
31
		'booktitle' => '',
32
		'chapter' => '',
33
		'crossref' => '',
34
		'doi' => '',
35
		'edition' => '',
36
		'editor' => [],
37
		'eprint' => '',
38
		'howpublished' => '',
39
		'institution' => '',
40
		'journal' => '',
41
		'key' => '',
42
		'month' => '',
43
		'note' => '',
44
		'number' => '',
45
		'organization' => '',
46
		'pages' => '',
47
		'publisher' => '',
48
		'school' => '',
49
		'series' => '',
50
		'title' => '',
51
		'url' => '',
52
		'volume' => '',
53
		'year' => ''
54
	];
55
56
	/**
57
	 * @var callable
58
	 */
59
	private $formatterCallback;
60
61
	/**
62
	 * @since 3.1
63
	 */
64
	public function __construct() {
65
		$this->type = 'Book';
66
	}
67
68
	/**
69
	 * @since 3.1
70
	 *
71
	 * @param callable $compoundLabelCallback
0 ignored issues
show
Bug introduced by
There is no parameter named $compoundLabelCallback. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
72
	 */
73
	public function setFormatterCallback( callable $formatterCallback ) {
74
		$this->formatterCallback = $formatterCallback;
75
	}
76
77
	/**
78
	 * @since 3.1
79
	 *
80
	 * @param $key
81
	 * @param string $text
82
	 *
83
	 * @return string
84
	 */
85
	public function replace( $key, $text ) {
86
87
		if ( $key === 'uri' ) {
88
			$text = str_replace(
89
				[ "Ä", "ä", "Ö", "ö", "Ü", "ü", "ß" ],
90
				[ 'Ae', 'ae', 'Oe', 'oe', 'Ue', 'ue', 'ss' ],
91
				$text
92
			);
93
			$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text );
94
		}
95
96
		return $text;
97
	}
98
99
	/**
100
	 * @since 3.1
101
	 *
102
	 * @param $key
103
	 * @param mixed $value
104
	 */
105
	public function set( $key, $value ) {
106
107
		$key = strtolower( $key );
108
109
		if ( $key === 'type' ) {
110
			$this->type = ucfirst( $value );
111
		}
112
113
		if ( isset( $this->fields[$key] ) ) {
114
			$this->fields[$key] = $value;
115
		}
116
	}
117
118
	/**
119
	 * @since 3.1
120
	 *
121
	 * @return string
122
	 */
123
	public function text() {
124
125
		$formatterCallback = $this->formatterCallback;
126
127
		$text = '@' . $this->type . '{' . $this->buildURI() . ",\r\n";
128
129
		foreach ( $this->fields as $key => $value ) {
130
131
			if ( ( $key === 'author' || $key === 'editor' ) && is_array( $value ) ) {
132
				if ( is_callable( $formatterCallback ) ) {
133
					$value = $formatterCallback( $key, $value );
134
				} else {
135
					$value = implode( ', ', $value );
136
				}
137
			}
138
139
			if ( $value === '' ) {
140
				continue;
141
			}
142
143
			$text .= '  ' . $key . ' = "' . $value . '", ' . "\r\n";
144
		}
145
146
		$text .= "}";
147
148
		return $text;
149
	}
150
151
	/**
152
	 * Consist of `author last name` + `year` + `first word of title`
153
	 *
154
	 * @return string
155
	 */
156
	private function buildURI() {
157
158
		$uri = '';
159
160
		if ( isset( $this->fields['author'] ) ) {
161
			foreach ( $this->fields['author'] as $key => $author ) {
162
				$elements = explode( ' ', $author );
163
				$uri .= array_pop( $elements );
164
				break;
165
			}
166
		}
167
168
		if ( isset( $this->fields['year'] ) ) {
169
			$uri .= $this->fields['year'];
170
		}
171
172
		if ( isset( $this->fields['title'] ) ) {
173
			foreach ( explode( ' ', $this->fields['title'] ) as $titleWord ) {
174
				$charsTitleWord = preg_split( '//', $titleWord, -1, PREG_SPLIT_NO_EMPTY );
175
176
				if ( !empty( $charsTitleWord ) ) {
177
					$uri .= $charsTitleWord[0];
178
				}
179
			}
180
		}
181
182
		return strtolower( $this->replace( 'uri', $uri ) );
183
	}
184
185
}
186