diff --git a/include/wb/Label.php b/include/wb/Label.php index ebb4146..4649153 100644 --- a/include/wb/Label.php +++ b/include/wb/Label.php @@ -1,112 +1,112 @@ . # Wikibase namespace wb; /** * A Wikibase Label * * https://www.wikidata.org/wiki/Wikidata:Glossary#Label */ class Label { /** * @var string */ public $language; /** * @var string */ public $value; /** * Constructor * * @var $language string Language code * @var $value string Label value */ public function __construct( $language, $value ) { $this->setLanguage( $language ) ->setValue( $value ); } /** * Get the language code * * @return string Language code */ public function getLanguage() { return $this->language; } /** * Get the language value * * @return string */ public function getValue() { return $this->value; } /** * Change the language * * This is more an internal method and should be avoided. * * @param $language string Language code * @return self */ - public function setLanguage( $language ) { + protected function setLanguage( $language ) { $this->language = $language; return $this; } /** * Set the label value * * @param $value Label value * @return self */ public function setValue( $value ) { $this->value = $value; return $this; } /** * Create a Label object from raw array data * * @param $data array * @return self */ public static function createFromData( $data ) { if( ! isset( $data['language'], $data['value'] ) ) { throw new WrongDataException( self::class ); } return new static( $data['language'], $data['value'] ); } /** * String rappresentation * * @return string */ public function __toString() { return sprintf( '%s: %s', $this->getLanguage(), $this->getValue() ); } } diff --git a/include/wb/Labels.php b/include/wb/Labels.php index a89eeff..b1b5cf1 100644 --- a/include/wb/Labels.php +++ b/include/wb/Labels.php @@ -1,115 +1,108 @@ . # Wikibase namespace wb; /** * Label collector */ class Labels { /** + * Labels indexed by language code + * * @var array */ private $labels = []; /** * Constructor * * @param $labels array */ public function __construct( $labels = [] ) { foreach( $labels as $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; + return $this->labels[ $language ] ?? false; } /** - * Does it have a certain label? + * Do our labels have this one? * * @param $language string * @return bool */ 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 * @return self */ public function set( Label $label ) { - $existing = $this->get( $label->getLanguage() ); - if( $existing ) { - $existing = $language; - } else { - $this->labels[] = $label; - } + $lang_code = $label->getLanguage(); + $this->labels[ $lang_code ] = $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() ); } } diff --git a/phpunit/WikibaseLabelsTest.php b/phpunit/WikibaseLabelsTest.php new file mode 100644 index 0000000..e76b2e2 --- /dev/null +++ b/phpunit/WikibaseLabelsTest.php @@ -0,0 +1,37 @@ +assertEquals( count( $labels->getAll() ), 0 ); + } + + public function testSetOne() { + $labels = new \wb\Labels(); + $label = new \wb\Label( 'it', 'Asd' ); + $this->assertEquals( $labels->have( 'it' ), false ); + $this->assertEquals( count( $labels->getAll() ), 0 ); + + $labels->set( $label ); + $this->assertEquals( $labels->have( 'it' ), true ); + $this->assertEquals( count( $labels->getAll() ), 1 ); + } + + public function testSetOverride() { + $labels = new \wb\Labels(); + $label1 = new \wb\Label( 'it', 'Asd1' ); + $label2 = new \wb\Label( 'it', 'Asd2' ); + $label3 = new \wb\Label( 'it', 'Asd3' ); + $labels->set( $label1 ); + $labels->set( $label2 ); + $labels->set( $label3 ); + $this->assertEquals( count( $labels->getAll() ), 1 ); + } + +}