Changeset View
Changeset View
Standalone View
Standalone View
include/wb/Labels.php
<?php | <?php | ||||
# Boz-MW - Another MediaWiki API handler in PHP | # Boz-MW - Another MediaWiki API handler in PHP | ||||
# Copyright (C) 2017, 2018 Valerio Bozzolan | # Copyright (C) 2017-2023 Valerio Bozzolan | ||||
# | # | ||||
# 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; | ||||
/** | /** | ||||
* Label collector | * Label collector | ||||
*/ | */ | ||||
class Labels { | class Labels { | ||||
/** | /** | ||||
* Labels indexed by language code | |||||
* | |||||
* @var array | * @var array | ||||
*/ | */ | ||||
private $labels = []; | private $labels = []; | ||||
/** | /** | ||||
* Constructor | * Constructor | ||||
* | * | ||||
* @param $labels array | * @param $labels array | ||||
*/ | */ | ||||
public function __construct( $labels = [] ) { | public function __construct( $labels = [] ) { | ||||
foreach( $labels as $label ) { | foreach( $labels as $label ) { | ||||
$this->add( $label ); | $this->set( $label ); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Get a certain language | * Get a certain language | ||||
* | * | ||||
* @param $language string | * @param $language string | ||||
* @return Label | * @return Label | ||||
*/ | */ | ||||
public function get( $language ) { | public function get( $language ) { | ||||
foreach( $this->labels as $label ) { | return $this->labels[ $language ] ?? false; | ||||
if( $label->getLanguage() === $language ) { | } | ||||
return $label; | |||||
/** | |||||
* Get a certain Label value by its language | |||||
* | |||||
* @param $language string | |||||
* @return string The language value or NULL | |||||
*/ | |||||
public function getLanguageValue( $language ) { | |||||
$value = null; | |||||
$label = $this->get( $language ); | |||||
if( $label ) { | |||||
$value = $label->getValue(); | |||||
} | } | ||||
return $value; | |||||
} | } | ||||
return false; | |||||
/** | |||||
* Get a certain Label value by its language | |||||
* | |||||
* @param $language string | |||||
* @param string The language value or NULL | |||||
* @return self | |||||
*/ | |||||
public function setLanguageValue( $language, $value ) { | |||||
$label = $this->createSingleFromLanguageValue( $language, $value ); | |||||
return $this->set( $label ); | |||||
} | } | ||||
/** | /** | ||||
* Does it have a certain label? | * Do our labels have this one? | ||||
* | * | ||||
* @param $language string | * @param $language string | ||||
* @return bool | * @return bool | ||||
*/ | */ | ||||
public function have( $language ) { | public function have( $language ) { | ||||
return false !== $this->get( $language ); | return $this->get( $language ) !== false; | ||||
} | } | ||||
/** | /** | ||||
* Set/add a certain label | * Set/add a certain Label | ||||
* | * | ||||
* @param $label Label | * @param $label Label | ||||
* @return self | * @return self | ||||
*/ | */ | ||||
public function set( Label $label ) { | public function set( Label $label ) { | ||||
$existing = $this->get( $label->getLanguage() ); | $lang_code = $label->getLanguage(); | ||||
if( $existing ) { | $this->labels[ $lang_code ] = $label; | ||||
$existing = $language; | |||||
} else { | |||||
$this->labels[] = $label; | |||||
} | |||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Get all the labels | * Get all the labels | ||||
* | * | ||||
* @return array | * @return array | ||||
*/ | */ | ||||
Show All 12 Lines | protected function getImplodedLanguages( $glue = ',' ) { | ||||
$codes = []; | $codes = []; | ||||
foreach( $all as $label ) { | foreach( $all as $label ) { | ||||
$codes[] = $label->getLanguage(); | $codes[] = $label->getLanguage(); | ||||
} | } | ||||
return implode( $glue, $codes ); | return implode( $glue, $codes ); | ||||
} | } | ||||
/** | /** | ||||
* Create a single element from a language and its value | |||||
* | |||||
* @param $language string Language code | |||||
* @param $value string Label value | |||||
* @return Label | |||||
*/ | |||||
protected function createSingleFromLanguageValue( $language, $value ) { | |||||
return new Label( $language, $value ); | |||||
} | |||||
/** | |||||
* String rappresentation | * String rappresentation | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public function __toString() { | public function __toString() { | ||||
return sprintf( 'label: %s', $this->getImplodedLanguages() ); | return sprintf( 'label: %s', $this->getImplodedLanguages() ); | ||||
} | } | ||||
} | } |
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