MslsLink::callback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsLink
4
 *
5
 * @author Dennis Ploetner <[email protected]>
6
 * @since 0.9.8
7
 */
8
9
namespace lloc\Msls;
10
11
/**
12
 * Link type: Image and text
13
 * @package Msls
14
 * @property string $txt
15
 * @property string $src
16
 * @property string $alt
17
 * @property string $url
18
 */
19
class MslsLink extends MslsGetSet {
20
21
	/**
22
	 * Output format
23
	 * @var string
24
	 */
25
	protected $format_string = '<img src="{src}" alt="{alt}"/> {txt}';
26
27
	/**
28
	 * Gets all link types as array with "id => name"-items
29
	 *
30
	 * @return string[]
31
	 */
32
	public static function get_types() {
33
		return [
34
			MslsLink::class,
35
			MslsLinkTextOnly::class,
36
			MslsLinkImageOnly::class,
37
			MslsLinkTextImage::class,
38
		];
39
	}
40
41
	/**
42
	 * Gets the link description.
43
	 *
44
	 * @return string
45
	 */
46
	public static function get_description() {
47
		return __( 'Flag and description', 'multisite-language-switcher' );
48
	}
49
50
	/**
51
	 * Gets an array with all link descriptions
52
	 *
53
	 * @return array
54
	 */
55
	public static function get_types_description() {
56
		$types = [];
57
58
		foreach ( self::get_types() as $key => $class ) {
59
			$types[ $key ] = call_user_func( [ $class, 'get_description' ] );
60
		}
61
62
		return $types;
63
	}
64
65
	/**
66
	 * Factory: Creates a specific instance of MslsLink
67
	 *
68
	 * @codeCoverageIgnore
69
	 *
70
	 * @param int $display
71
	 *
72
	 * @return MslsLink
73
	 */
74
	public static function create( $display ) {
75
		if ( has_filter( 'msls_link_create' ) ) {
76
			/**
77
			 * Returns custom MslsLink-Object
78
			 * @since 0.9.9
79
			 *
80
			 * @param int $display
81
			 *
82
			 * @return MslsLink
83
			 */
84
			$obj = apply_filters( 'msls_link_create', $display );
85
			if ( is_subclass_of( $obj, __CLASS__ ) ) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if __CLASS__ can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
86
				return $obj;
87
			}
88
		}
89
90
		$types = self::get_types();
91
		if ( ! in_array( $display, array_keys( $types ), true ) ) {
92
			$display = 0;
93
		}
94
95
		return new $types[ $display ];
96
	}
97
98
	/**
99
	 * Callback function (no lambda here because PHP 5.2 might be still in use)
100
	 *
101
	 * @param mixed $x
102
	 *
103
	 * @return string
104
	 */
105
	public static function callback( $x ) {
106
		return '{' . $x . '}';
107
	}
108
109
	/**
110
	 * Handles the request to print the object
111
	 *
112
	 * @return string
113
	 */
114
	public function __toString() {
115
		$temp = $this->get_arr();
116
117
		return str_replace(
118
			array_map( [ $this, 'callback' ], array_keys( $temp ) ),
119
			$temp,
120
			$this->format_string
121
		);
122
	}
123
124
}
125