Source of file MslsBlog.php

Size: 2,650 Bytes - Last Modified: 2018-02-05T14:23:18+00:00

/home/dennisploetner/Projects/Multisite-Language-Switcher/includes/MslsBlog.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
<?php
/**
 * MslsBlog
 * @author Dennis Ploetner <re@lloc.de>
 * @since 0.9.8
 */

namespace lloc\Msls;

/**
 * Internal representation of a blog
 * @property int $userblog_id
 * @package Msls
 */
class MslsBlog {

	/**
	 * WordPress generates such an object
	 * @var \StdClass
	 */
	private $obj;

	/**
	 * Language-code eg. de_DE
	 * @var string
	 */
	private $language;

	/**
	 * Description eg. Deutsch
	 * @var string
	 */
	private $description;

	/**
	 * Constructor
	 *
	 * @param \StdClass $obj
	 * @param string $description
	 */
	public function __construct( $obj, $description ) {
		if ( is_object( $obj ) ) {
			$this->obj      = $obj;
			$this->language = MslsBlogCollection::get_blog_language( $this->obj->userblog_id );
		}

		$this->description = (string) $description;
	}

	/**
	 * Get a member of the \StdClass-object by name
	 *
	 * The method return <em>null</em> if the requested member does not exists.
	 *
	 * @param string $key
	 *
	 * @return mixed|null
	 */
	final public function __get( $key ) {
		return isset( $this->obj->$key ) ? $this->obj->$key : null;
	}

	/**
	 * Get the description stored in this object
	 *
	 * The method returns the stored language if the description is empty.
	 * @return string
	 */
	public function get_description() {
		return (
			empty( $this->description ) ?
			$this->get_language() :
			$this->description
		);
	}

	/**
	 * Get the language stored in this object
	 *
	 * This method returns the string 'us' if there is an empty value in language.
	 * @return string
	 */
	public function get_language() {
		return ( empty( $this->language ) ? 'us' : $this->language );
	}

	/**
	 * Get the alpha2-part of the language-code
	 *
	 * This method returns the string 'en' if the language-code contains just 'us'.
	 * @return string
	 */
	public function get_alpha2() {
		$alpha2 = substr( $this->get_language(), 0, 2 );

		return ( 'us' == $alpha2 ? 'en' : $alpha2 );
	}

	/**
	 * Sort objects helper
	 *
	 * @param string $a
	 * @param string $b
	 *
	 * @return int
	 */
	public static function _cmp( $a, $b ) {
		if ( $a == $b ) {
			return 0;
		}

		return ( $a < $b ? ( - 1 ) : 1 );
	}

	/**
	 * Sort objects by language
	 *
	 * @param MslsBlog $a
	 * @param MslsBlog $b
	 *
	 * @return int
	 */
	public static function language( MslsBlog $a, MslsBlog $b ) {
		return ( self::_cmp( $a->get_language(), $b->get_language() ) );
	}

	/**
	 * Sort objects by description
	 *
	 * @param MslsBlog $a
	 * @param MslsBlog $b
	 *
	 * @return int
	 */
	public static function description( MslsBlog $a, MslsBlog $b ) {
		return ( self::_cmp( $a->get_description(), $b->get_description() ) );
	}

}