1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\DataValues\ValueFormatters; |
4
|
|
|
|
5
|
|
|
use SMWDataValue as DataValue; |
6
|
|
|
use SMWOutputs as Outputs; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @license GNU GPL v2+ |
10
|
|
|
* @since 2.4 |
11
|
|
|
* |
12
|
|
|
* @author mwjames |
13
|
|
|
*/ |
14
|
|
|
class CodeStringValueFormatter extends StringValueFormatter { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @since 2.4 |
18
|
|
|
* |
19
|
|
|
* {@inheritDoc} |
20
|
|
|
*/ |
21
|
172 |
|
public function isFormatterFor( DataValue $dataValue ) { |
22
|
172 |
|
return $dataValue->getTypeID() === '_cod'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see StringValueFormatter::doFormatFinalOutputFor |
27
|
|
|
*/ |
28
|
9 |
|
protected function doFormatFinalOutputFor( $type, $linker ) { |
29
|
|
|
|
30
|
9 |
|
$abbreviate = $type === self::WIKI_LONG || $type === self::HTML_LONG; |
31
|
9 |
|
$text = $this->dataValue->getDataItem()->getString(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
// Escape and wrap values of type Code. The result is escaped to be |
34
|
|
|
// HTML-safe (it will also work in wiki context). The result will |
35
|
|
|
// contain mark-up that must not be escaped again. |
36
|
|
|
|
37
|
9 |
|
Outputs::requireResource( 'ext.smw.style' ); |
38
|
|
|
|
39
|
|
|
if ( $this->isJson( $text ) ) { |
40
|
9 |
|
$result = self::formatAsPrettyJson( $text ); |
41
|
9 |
|
} else { |
42
|
9 |
|
// This disables all active wiki and HTML markup: |
43
|
|
|
$result = str_replace( |
44
|
|
|
array( '<', '>', ' ', '[', '{', '=', "'", ':', "\n" ), |
45
|
9 |
|
array( '<', '>', ' ', '[', '{', '=', ''', ':', "<br />" ), |
46
|
6 |
|
$text |
47
|
|
|
); |
48
|
|
|
} |
49
|
9 |
|
|
50
|
|
|
if ( $abbreviate ) { |
51
|
|
|
$result = "<div style=\"min-height:5em; overflow:auto;\">$result</div>"; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return "<div class=\"smwpre\">$result</div>"; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @since 2.5 |
59
|
|
|
* |
60
|
|
|
* @param string $string |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public static function formatAsPrettyJson( $string ) { |
65
|
|
|
return defined( 'JSON_PRETTY_PRINT' ) ? json_encode( json_decode( $string ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) : $string; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function isJson( $string ) { |
69
|
|
|
|
70
|
|
|
// Don't bother |
71
|
|
|
if ( substr( $string, 0, 1 ) !== '{' ) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
json_decode( $string ); |
76
|
|
|
|
77
|
|
|
return ( json_last_error() == JSON_ERROR_NONE ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: