HEX
Server: LiteSpeed
System: Linux server484.bertina.biz 4.18.0-553.34.1.lve.el8.x86_64 #1 SMP Thu Jan 9 16:30:32 UTC 2025 x86_64
User: alvnails (1268)
PHP: 8.2.29
Disabled: mail
Upload Files
File: /home/alvnails/public_html/wp-content/plugins/multisite-language-switcher/includes/MslsJson.php
<?php declare( strict_types=1 );

namespace lloc\Msls;

/**
 * Container for an array which will used in JavaScript as object in JSON
 *
 * @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/1c4f62e1de57ca48f19c37e3a63e7dc311b76b2f/MslsJson.php
 * @package Msls
 */
class MslsJson {

	/**
	 * @var array<int, array<string, string|int>>
	 */
	protected array $arr = array();

	/** MslsLanguageArray
	 * Adds a value label pair to the internal class container
	 *
	 * @param mixed $value
	 * @param mixed $label
	 *
	 * @return MslsJson
	 */
	public function add( $value, $label ) {
		$this->arr[] = array(
			'value' => intval( $value ),
			'label' => strval( $label ),
		);

		return $this;
	}

	/**
	 * Compare the item with the key "label" of the array $a and the array $b
	 *
	 * @param array<string, string> $a
	 * @param array<string, string> $b
	 *
	 * @return int
	 */
	public static function compare( array $a, array $b ) {
		return strnatcmp( $a['label'], $b['label'] );
	}

	/**
	 * Get the array container sorted by label
	 *
	 * @return array<int, array<string, string|int>>
	 */
	public function get(): array {
		$arr = $this->arr;

		usort( $arr, array( __CLASS__, 'compare' ) );

		return $arr;
	}

	/**
	 * Encodes object and returns it as a json-string
	 *
	 * @return string
	 */
	public function encode(): string {
		return wp_json_encode( $this->get() );
	}

	/**
	 * Return the encoded object as a string using the encode-method
	 *
	 * @return string
	 */
	public function __toString() {
		return $this->encode();
	}
}