diff --git a/include/wb/Labels.php b/include/wb/Labels.php index 8fbc169..a89eeff 100644 --- a/include/wb/Labels.php +++ b/include/wb/Labels.php @@ -1,115 +1,115 @@ . # Wikibase namespace wb; /** * Label collector */ class Labels { /** * @var array */ private $labels = []; /** * Constructor * * @param $labels array */ public function __construct( $labels = [] ) { foreach( $labels as $label ) { - $this->add( $label ); + $this->set( $label ); } } /** * Get a certain language * * @param $language string * @return Label */ public function get( $language ) { foreach( $this->labels as $label ) { if( $label->getLanguage() === $language ) { return $label; } } return false; } /** * Does it have a certain label? * * @param $language string * @return bool */ public function have( $language ) { return false !== $this->get( $language ); } /** * Set/add a certain label * * @param $label Label * @return self */ public function set( Label $label ) { $existing = $this->get( $label->getLanguage() ); if( $existing ) { $existing = $language; } else { $this->labels[] = $label; } return $this; } /** * Get all the labels * * @return array */ public function getAll() { return $this->labels; } /** * Get all the languages imploded * * @param $glue string * @return string */ protected function getImplodedLanguages( $glue = ',' ) { $all = $this->getAll(); $codes = []; foreach( $all as $label ) { $codes[] = $label->getLanguage(); } return implode( $glue, $codes ); } /** * String rappresentation * * @return string */ public function __toString() { return sprintf( 'label: %s', $this->getImplodedLanguages() ); } }