Completed
Push — master ( 3e30e6...aad109 )
by mw
02:31
created

src/EmbeddedLinksReplacer.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SEQL;
4
5
use SMW\InTextAnnotationParser;
6
7
/**
8
 * Find and replace [[]] with an appropriate remote source link.
9
 *
10
 * @license GNU GPL v2+
11
 * @since 1.0
12
 *
13
 * @author mwjames
14
 */
15
class EmbeddedLinksReplacer {
16
17
	/**
18
	 * @var string
19
	 */
20
	private $querySource;
21
22
	/**
23
	 * @since 1.0
24
	 *
25
	 * @param string $querySource
26
	 */
27 16
	public function __construct( $querySource ) {
28 16
		$this->querySource = $querySource;
29 16
	}
30
31
	/**
32
	 * @since 1.0
33
	 *
34
	 * @param string $value
35
	 *
36
	 * @return string
37
	 */
38 8
	public function replace( $value ) {
39
40
		// Strip any [[ :: ]] from a value to avoid "foreign" annotation parsing
41
		// for annotation embedded within a value
42 8
		$value = InTextAnnotationParser::removeAnnotation(
0 ignored issues
show
The method removeAnnotation() does not seem to exist on object<SMW\InTextAnnotationParser>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
			$value
44 8
		);
45
46 8
		return $this->replaceEmbeddedLinksWith( $this->querySource, $value );
47
	}
48
49 8
	private function replaceEmbeddedLinksWith( $source, $value ) {
50 8
		$value = preg_replace_callback(
51 8
			'/\[\[(.*)\]\]/xu',
52
			function( array $matches ) use( $source ) {
53 2
				$caption = false;
54 2
				$value = '';
55
56 2
				if ( array_key_exists( 1, $matches ) ) {
57 2
					$parts = explode( '|', $matches[1] );
58 2
					$value = array_key_exists( 0, $parts ) ? $parts[0] : '';
59 2
					$caption = array_key_exists( 1, $parts ) ? $parts[1] : false;
60 2
				}
61
62 2
				if ( $caption === false ) {
63 1
					$caption = $value;
64 1
				}
65
66 2
				return '[[' . $source . ':' . $value . '|'  . $caption . ']]';
67 8
			},
68
			$value
69 8
		);
70
71 8
		return $value;
72
	}
73
74
}
75