diff --git a/include/wb/Descriptions.php b/include/wb/Descriptions.php index 7ba3ec6..ac6a0c8 100644 --- a/include/wb/Descriptions.php +++ b/include/wb/Descriptions.php @@ -1,35 +1,46 @@ . # Wikibase namespace wb; /** * Description collector */ class Descriptions extends Labels { + /** + * Create a single element from a language and its value + * + * @param $language string Language code + * @param $value string Label value + * @return Description + */ + protected function createSingleFromLanguageValue( $language, $value ) { + return new Description( $language, $value ); + } + /** * String rappresentation * * @return string */ public function __toString() { return sprintf( 'description: %s', $this->getImplodedLanguages() ); } } diff --git a/include/wb/Labels.php b/include/wb/Labels.php index b1b5cf1..75bb7ad 100644 --- a/include/wb/Labels.php +++ b/include/wb/Labels.php @@ -1,108 +1,146 @@ . # 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 ) { return $this->labels[ $language ] ?? false; } + /** + * 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; + } + + /** + * 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 ); + } + /** * Do our labels have this one? * * @param $language string * @return bool */ public function have( $language ) { return $this->get( $language ) !== false; } /** * Set/add a certain Label * * @param $label Label * @return self */ public function set( Label $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 ); } + /** + * 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 * * @return string */ public function __toString() { return sprintf( 'label: %s', $this->getImplodedLanguages() ); } } diff --git a/phpunit/WikibaseDescriptionsTest.php b/phpunit/WikibaseDescriptionsTest.php new file mode 100644 index 0000000..c233c63 --- /dev/null +++ b/phpunit/WikibaseDescriptionsTest.php @@ -0,0 +1,48 @@ +assertEquals( count( $labels->getAll() ), 0 ); + } + + public function testGetLanguageValueEmpty() { + $labels = new \wb\Descriptions(); + $this->assertEquals( $labels->getLanguageValue( 'it' ), null ); + } + + public function testSetGetLanguageValue() { + $labels = new \wb\Descriptions(); + $labels->setLanguageValue( 'it', 'Pizza' ); + $this->assertEquals( $labels->getLanguageValue( 'it' ), 'Pizza' ); + } + + public function testSetOne() { + $labels = new \wb\Descriptions(); + $label = new \wb\Description( '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\Descriptions(); + $label1 = new \wb\Description( 'it', 'Asd1' ); + $label2 = new \wb\Description( 'it', 'Asd2' ); + $label3 = new \wb\Description( 'it', 'Asd3' ); + $labels->set( $label1 ); + $labels->set( $label2 ); + $labels->set( $label3 ); + $this->assertEquals( count( $labels->getAll() ), 1 ); + } + +} diff --git a/phpunit/WikibaseLabelsTest.php b/phpunit/WikibaseLabelsTest.php index e76b2e2..6adfac7 100644 --- a/phpunit/WikibaseLabelsTest.php +++ b/phpunit/WikibaseLabelsTest.php @@ -1,37 +1,48 @@ assertEquals( count( $labels->getAll() ), 0 ); } + public function testGetLanguageValueEmpty() { + $labels = new \wb\Labels(); + $this->assertEquals( $labels->getLanguageValue( 'it' ), null ); + } + + public function testSetGetLanguageValue() { + $labels = new \wb\Labels(); + $labels->setLanguageValue( 'it', 'Pizza' ); + $this->assertEquals( $labels->getLanguageValue( 'it' ), 'Pizza' ); + } + 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 ); } }