Completed
Push — master ( 70c768...281d7e )
by Dennis
02:10
created

MslsBlog::get_language()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/**
3
 * MslsBlog
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
/**
9
 * Internal representation of a blog
10
 * @property int $userblog_id
11
 * @package Msls
12
 */
13
class MslsBlog {
14
15
	/**
16
	 * WordPress generates such an object
17
	 * @var StdClass
18
	 */
19
	private $obj;
20
21
	/**
22
	 * Language-code eg. de_DE
23
	 * @var string
24
	 */
25
	private $language;
26
27
	/**
28
	 * Description eg. Deutsch
29
	 * @var string
30
	 */
31
	private $description;
32
33
	/**
34
	 * x-default
35
	 * @var boolean
36
	 */
37
	private $x_default;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param StdClass $obj
43
	 * @param string $description
44
	 */
45
	public function __construct( $obj, $description ) {
46
		if ( is_object( $obj ) ) {
47
			$this->obj       = $obj;
48
			$this->language  = (string) get_blog_option(
49
				$this->obj->userblog_id, 'WPLANG'
50
			);
51
			$options = get_blog_option( $this->obj->userblog_id, 'msls' );
52
			$this->x_default = boolval( isset( $options['x_default'] ) );
53
		}
54
		$this->description = (string) $description;
55
	}
56
57
	/**
58
	 * Get a member of the StdClass-object by name
59
	 *
60
	 * The method return <em>null</em> if the requested member does not exists.
61
	 *
62
	 * @param string $key
63
	 *
64
	 * @return mixed|null
65
	 */
66
	final public function __get( $key ) {
67
		return ( isset( $this->obj->$key ) ? $this->obj->$key : null );
68
	}
69
70
	/**
71
	 * Is blog the target for the hreflang value 'x-default'
72
	 *
73
	 * @return bool
74
	 */
75
	public function is_x_default() {
76
		return $this->x_default;
77
	}
78
79
	/**
80
	 * Get the description stored in this object
81
	 *
82
	 * The method returns the stored language if the description is empty.
83
	 * @return string
84
	 */
85
	public function get_description() {
86
		return (
87
		empty( $this->description ) ?
88
			$this->get_language() :
89
			$this->description
90
		);
91
	}
92
93
	/**
94
	 * Get the language stored in this object
95
	 *
96
	 * This method returns the string 'us' if there is an empty value in language.
97
	 * @return string
98
	 */
99
	public function get_language() {
100
		return ( empty( $this->language ) ? 'us' : $this->language );
101
	}
102
103
	/**
104
	 * Get the alpha2-part of the language-code
105
	 *
106
	 * This method returns the string 'en' if the language-code contains just 'us'.
107
	 * @return string
108
	 */
109
	public function get_alpha2() {
110
		$alpha2 = substr( $this->get_language(), 0, 2 );
111
112
		return ( 'us' == $alpha2 ? 'en' : $alpha2 );
113
	}
114
115
	/**
116
	 * Sort objects helper
117
	 *
118
	 * @param string $a
119
	 * @param string $b
120
	 *
121
	 * @return integer
122
	 */
123
	public static function _cmp( $a, $b ) {
124
		if ( $a == $b ) {
125
			return 0;
126
		}
127
128
		return ( $a < $b ? ( - 1 ) : 1 );
129
	}
130
131
	/**
132
	 * Sort objects by language
133
	 *
134
	 * @param MslsBlog $a
135
	 * @param MslsBlog $b
136
	 *
137
	 * @return integer
138
	 */
139
	public static function language( MslsBlog $a, MslsBlog $b ) {
140
		return ( self::_cmp( $a->get_language(), $b->get_language() ) );
141
	}
142
143
	/**
144
	 * Sort objects by description
145
	 *
146
	 * @param MslsBlog $a
147
	 * @param MslsBlog $b
148
	 *
149
	 * @return integer
150
	 */
151
	public static function description( MslsBlog $a, MslsBlog $b ) {
152
		return ( self::_cmp( $a->get_description(), $b->get_description() ) );
153
	}
154
155
}
156