Changeset View
Changeset View
Standalone View
Standalone View
include/wb/Label.php
<?php | <?php | ||||
# Boz-MW - Another MediaWiki API handler in PHP | # Boz-MW - Another MediaWiki API handler in PHP | ||||
# Copyright (C) 2017 Valerio Bozzolan | # Copyright (C) 2017-2023 Valerio Bozzolan, contributors | ||||
# | # | ||||
# This program is free software: you can redistribute it and/or modify | # This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License as published by | # it under the terms of the GNU Affero General Public License as published by | ||||
# the Free Software Foundation, either version 3 of the License, or | # the Free Software Foundation, either version 3 of the License, or | ||||
# (at your option) any later version. | # (at your option) any later version. | ||||
# | # | ||||
# This program is distributed in the hope that it will be useful, | # This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU Affero General Public License for more details. | # GNU Affero General Public License for more details. | ||||
# | # | ||||
# You should have received a copy of the GNU General Public License | # You should have received a copy of the GNU General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# Wikibase | # Wikibase | ||||
namespace wb; | namespace wb; | ||||
/** | /** | ||||
* An entity Label. | * A Wikibase Label | ||||
* | * | ||||
* @see https://www.wikidata.org/wiki/Wikidata:Glossary#Label | * https://www.wikidata.org/wiki/Wikidata:Glossary#Label | ||||
*/ | */ | ||||
class Label { | class Label { | ||||
/** | |||||
* @var string | |||||
*/ | |||||
public $language; | public $language; | ||||
/** | |||||
* @var string | |||||
*/ | |||||
public $value; | public $value; | ||||
/** | |||||
* Constructor | |||||
* | |||||
* @var $language string Language code | |||||
* @var $value string Label value | |||||
*/ | |||||
public function __construct( $language, $value ) { | public function __construct( $language, $value ) { | ||||
$this->setLanguage( $language ) | $this->setLanguage( $language ) | ||||
->setValue( $value ); | ->setValue( $value ); | ||||
} | } | ||||
/** | |||||
* Get the language code | |||||
* | |||||
* @return string Language code | |||||
*/ | |||||
public function getLanguage() { | public function getLanguage() { | ||||
return $this->language; | return $this->language; | ||||
} | } | ||||
/** | |||||
* Get the language value | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function getValue() { | public function getValue() { | ||||
return $this->value; | return $this->value; | ||||
} | } | ||||
public function setLanguage( $language ) { | /** | ||||
* Change the language | |||||
* | |||||
* This is more an internal method and should be avoided. | |||||
* | |||||
* @param $language string Language code | |||||
* @return self | |||||
*/ | |||||
protected function setLanguage( $language ) { | |||||
$this->language = $language; | $this->language = $language; | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* Set the label value | |||||
* | |||||
* @param $value Label value | |||||
* @return self | |||||
*/ | |||||
public function setValue( $value ) { | public function setValue( $value ) { | ||||
$this->value = $value; | $this->value = $value; | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* Create a Label object from raw array data | |||||
* | |||||
* @param $data array | |||||
* @return self | |||||
*/ | |||||
public static function createFromData( $data ) { | public static function createFromData( $data ) { | ||||
if( ! isset( $data['language'], $data['value'] ) ) { | if( ! isset( $data['language'], $data['value'] ) ) { | ||||
throw new WrongDataException( self::class ); | throw new WrongDataException( self::class ); | ||||
} | } | ||||
return new static( $data['language'], $data['value'] ); | return new static( $data['language'], $data['value'] ); | ||||
} | } | ||||
/** | /** | ||||
* String rappresentation | * String rappresentation | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public function __toString() { | public function __toString() { | ||||
return sprintf( '%s: %s', $this->getLanguage(), $this->getValue() ); | return sprintf( '%s: %s', $this->getLanguage(), $this->getValue() ); | ||||
} | } | ||||
} | } |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register