summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/ValueParsers/MonolingualTextValueParser.php
blob: fdf1e5ec51a7a750af8ed7cbb23ff44a49b7e693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace SMW\DataValues\ValueParsers;

use SMW\Localizer;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class MonolingualTextValueParser implements ValueParser {

	/**
	 * @var array
	 */
	private $errors = [];

	/**
	 * @since 2.4
	 *
	 * @return array
	 */
	public function getErrors() {
		return $this->errors;
	}

	/**
	 * @since 2.4
	 *
	 * @param string|array $userValue
	 *
	 * @return array
	 */
	public function parse( $userValue ) {

		// Allow things like [ "en" => "Foo ..." ] when retrieved from a JSON string
		if ( is_array( $userValue ) ) {
			foreach ( $userValue as $key => $value ) {
				$languageCode = is_string( $key ) ? $key : '';
				$text = is_string( $value ) ? $value : '';
			}
		} else {
			$text = $userValue;
			$languageCode = mb_substr( strrchr( $userValue, "@" ), 1 );

			// Remove the language code and marker from the text
			if ( $languageCode !== '' ) {
				$text = substr_replace( $userValue, '', ( mb_strlen( $languageCode ) + 1 ) * -1 );
			}
		}

		return [ $text, Localizer::asBCP47FormattedLanguageCode( $languageCode ) ];
	}

}