Completed
Push — master ( 0591cb...33c12c )
by
unknown
07:17
created

BibtexProcessor::doProcess()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 22
cts 22
cp 1
rs 8.0835
c 0
b 0
f 0
cc 8
nc 5
nop 1
crap 8
1
<?php
2
3
namespace SCI\Bibtex;
4
5
use SMW\ParserParameterProcessor;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.0
10
 */
11
class BibtexProcessor {
12
13
	/**
14
	 * @var BibtexParser
15
	 */
16
	private $bibtexParser;
17
18
	/**
19
	 * @var BibtexAuthorListParser
20
	 */
21
	private $bibtexAuthorListParser;
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @param BibtexParser $bibtexParser
27
	 * @param BibtexAuthorListParser $bibtexAuthorListParser
28
	 */
29 19
	public function __construct( BibtexParser $bibtexParser, BibtexAuthorListParser $bibtexAuthorListParser ) {
30 19
		$this->bibtexParser = $bibtexParser;
31 19
		$this->bibtexAuthorListParser = $bibtexAuthorListParser;
32 19
	}
33
34
	/**
35
	 * @since 1.0
36
	 *
37
	 * @param ParserParameterProcessor $parserParameterProcessor
38
	 */
39 5
	public function doProcess( ParserParameterProcessor $parserParameterProcessor ) {
40
41 5
		$bibtex = $this->doPreprocess(
42 5
			$parserParameterProcessor->getParameterValuesFor( 'bibtex' )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\ParserParameterProce...getParameterValuesFor() has been deprecated with message: since 2.5, use ParserParameterProcessor::getParameterValuesByKey

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
43 5
		);
44
45 5
		$parameters = $this->bibtexParser->parse( $bibtex );
46
47 5
		foreach ( $parameters as $key => $value ) {
48
49
			// The explicit parameters precedes the one found in bibtex
50 5
			if ( $key === 'reference' && ( $parserParameterProcessor->hasParameter( 'reference' ) || $parserParameterProcessor->getFirstParameter() !== '' ) ) {
51 2
				continue;
52
			}
53
54 4
			if ( $key === 'type' && $parserParameterProcessor->hasParameter( 'type' ) ) {
55 2
				continue;
56
			}
57
58 3
			if ( $key === 'author' ) {
59
60 2
				$parserParameterProcessor->setParameter(
61 2
					$key,
62 2
					$this->bibtexAuthorListParser->parse( $value )
63 2
				);
64
65
				// Add the original value for easier post-processing in a template
66
				// as hidden parameter
67 2
				$key = 'bibtex-author';
68 2
			}
69
70 3
			$parserParameterProcessor->addParameter(
71 3
				$key,
72
				$value
73 3
			);
74 5
		}
75 5
	}
76
77 5
	private function doPreprocess( array $bibtex ) {
78
79 5
		$bibtex = end( $bibtex );
80
81
		// Avoid things like {{Stable theories}}" which are not supported in MW
82
		// since the parser replaces it with [[:Template:Stable theories]]
83 5
		$this->replace( '{{', "{", $bibtex );
84 5
		$this->replace( '}}', "}", $bibtex );
85
86 5
		$this->replace( '{\textquotesingle}', "'", $bibtex );
87 5
		$this->replace( '$\upgamma$', "γ", $bibtex );
88
89 5
		return $bibtex;
90
	}
91
92 5
	private function replace( $search, $with, &$on ) {
93 5
		$on = strpos( $on, $search ) !== false ? str_replace( $search, $with, $on ) : $on;
94 5
	}
95
96
}
97