Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsLink   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_types() 0 8 1
A get_description() 0 3 1
A get_types_description() 0 9 2
A create() 0 19 4
A callback() 0 3 1
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 = array();
57
		foreach ( self::get_types() as $key => $class ) {
58
			$types[ $key ] = call_user_func(
59
				array( $class, 'get_description' )
60
			);
61
		}
62
		return $types;
63
	}
64
65
	/**
66
	 * Factory: Creates a specific instance of MslsLink
67
	 *
68
	 * @param int $display
69
	 * @return MslsLink
70
	 */
71
	public static function create( $display ) {
72
		if ( has_filter( 'msls_link_create' ) ) {
73
			/**
74
			 * Returns custom MslsLink-Object
75
			 * @since 0.9.9
76
			 * @param int $display
77
			 * @return MslsLink
78
			 */
79
			$obj = apply_filters( 'msls_link_create', $display );
80
			if ( is_subclass_of( $obj, 'MslsLink' ) ) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
81
				return $obj;
82
			}
83
		}
84
		$types = self::get_types();
85
		if ( ! in_array( $display, array_keys( $types ), true ) ) {
86
			$display = 0;
87
		}
88
		return new $types[ $display ];
89
	}
90
91
	/**
92
	 * Callback function (no lambda here because PHP 5.2 might be still in use)
93
	 * @param mixed $x
94
	 * @return string
95
	 */
96
	public static function callback( $x ) {
97
		return '{' . $x . '}';
98
	}
99
100
	/**
101
	 * Handles the request to print the object
102
	 * @return string
103
	 */
104
	public function __toString() {
105
		$temp = $this->get_arr();
106
		return str_replace(
107
			array_map(
108
				array( $this, 'callback' ),
109
				array_keys( $temp )
110
			),
111
			$temp,
112
			$this->format_string
113
		);
114
	}
115
116
}
117