Completed
Push — master ( d24e06...1436e2 )
by Dennis
02:14
created

MslsBlog::get_description()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 2
b 0
f 1
nc 2
nop 0
dl 0
loc 7
rs 9.4285
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
	 * Constructor
35
	 *
36
	 * @param StdClass $obj
37
	 * @param string $description
38
	 */
39
	public function __construct( $obj, $description ) {
40
		if ( is_object( $obj ) ) {
41
			$this->obj      = $obj;
42
			$this->language = (string) get_blog_option(
43
				$this->obj->userblog_id, 'WPLANG'
44
			);
45
		}
46
		$this->description = (string) $description;
47
	}
48
49
	/**
50
	 * Get a member of the StdClass-object by name
51
	 *
52
	 * The method return <em>null</em> if the requested member does not exists.
53
	 *
54
	 * @param string $key
55
	 *
56
	 * @return mixed|null
57
	 */
58
	final public function __get( $key ) {
59
		return ( isset( $this->obj->$key ) ? $this->obj->$key : null );
60
	}
61
62
	/**
63
	 * Get the description stored in this object
64
	 *
65
	 * The method returns the stored language if the description is empty.
66
	 * @return string
67
	 */
68
	public function get_description() {
69
		return (
70
		empty( $this->description ) ?
71
			$this->get_language() :
72
			$this->description
73
		);
74
	}
75
76
	/**
77
	 * Get the language stored in this object
78
	 *
79
	 * This method returns the string 'us' if there is an empty value in language.
80
	 * @return string
81
	 */
82
	public function get_language() {
83
		return ( empty( $this->language ) ? 'us' : $this->language );
84
	}
85
86
	/**
87
	 * Get the alpha2-part of the language-code
88
	 *
89
	 * This method returns the string 'en' if the language-code contains just 'us'.
90
	 * @return string
91
	 */
92
	public function get_alpha2() {
93
		$alpha2 = substr( $this->get_language(), 0, 2 );
94
95
		return ( 'us' == $alpha2 ? 'en' : $alpha2 );
96
	}
97
98
	/**
99
	 * Sort objects helper
100
	 *
101
	 * @param string $a
102
	 * @param string $b
103
	 *
104
	 * @return int
105
	 */
106
	public static function _cmp( $a, $b ) {
107
		if ( $a == $b ) {
108
			return 0;
109
		}
110
111
		return ( $a < $b ? ( - 1 ) : 1 );
112
	}
113
114
	/**
115
	 * Sort objects by language
116
	 *
117
	 * @param MslsBlog $a
118
	 * @param MslsBlog $b
119
	 *
120
	 * @return int
121
	 */
122
	public static function language( MslsBlog $a, MslsBlog $b ) {
123
		return ( self::_cmp( $a->get_language(), $b->get_language() ) );
124
	}
125
126
	/**
127
	 * Sort objects by description
128
	 *
129
	 * @param MslsBlog $a
130
	 * @param MslsBlog $b
131
	 *
132
	 * @return int
133
	 */
134
	public static function description( MslsBlog $a, MslsBlog $b ) {
135
		return ( self::_cmp( $a->get_description(), $b->get_description() ) );
136
	}
137
138
}
139