diff --git a/include/network/HTTPRequest.php b/include/network/HTTPRequest.php --- a/include/network/HTTPRequest.php +++ b/include/network/HTTPRequest.php @@ -328,7 +328,7 @@ if( !$http_status ) { // oh nose! - Log::error( "Huston, we have not valid headers" ); + Log::error( "Houston, we have not valid headers" ); // retry but without DOSsing $args = array_replace( $args, [ @@ -344,7 +344,7 @@ if( $http_status->isServerError() ) { // oh nose! - Log::error( sprintf( "Huston, we have the code %s: %s", + Log::error( sprintf( "Houston, we have the code %s: %s", $http_status->getCode(), $http_status->getMessage() ) ); diff --git a/include/wb/DataModel.php b/include/wb/DataModel.php --- a/include/wb/DataModel.php +++ b/include/wb/DataModel.php @@ -1,6 +1,6 @@ labels->have( $language ); } + /** + * Get a label in a specific language + * + * @param $language Language code, as accepted by Wikidata + * @return string|null + */ + public function getLabelValue( $language ) { + return $this->labels->getLanguageValue( $language ); + } + + /** + * Set a label in a specific language + * + * @param $language Language code, as accepted by Wikidata + * @param $value Language value + * @return self + */ + public function setLabelValue( $language, $value ) { + $this->labels->setLanguageValue( $language, $value ); + return $this; + } + /** * Set, delete, preserve if it exists, a label. + * + * You may want to use setLabelValue() instead that is more user-friendly. + * + * @param $label Label object + * @return self */ public function setLabel( $label ) { $this->labels->set( $label ); @@ -278,15 +307,37 @@ } /** - * Check if a label of a certain language exists + * Check if a description exists in a certain language * - * @param $language string + * @param $language string Language code, as accepted by Wikidata * @return bool */ public function hasDescriptionInLanguage( $language ) { return $this->descriptions->have( $language ); } + /** + * Get a description in a specific language + * + * @param $language Language code, as accepted by Wikidata + * @return string|null + */ + public function getDescriptionValue( $language ) { + return $this->descriptions->getLanguageValue( $language ); + } + + /** + * Set a label in a specific language + * + * @param $language Language code, as accepted by Wikidata + * @param $value Language value + * @return self + */ + public function setDescriptionValue( $language, $value ) { + $this->descriptions->setLanguageValue( $language, $value ); + return $this; + } + /** * Set, delete, preserve if it exists, a description. * diff --git a/include/wb/Descriptions.php b/include/wb/Descriptions.php --- a/include/wb/Descriptions.php +++ b/include/wb/Descriptions.php @@ -1,6 +1,6 @@ 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; } - 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; 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 ); diff --git a/include/wb/Labels.php b/include/wb/Labels.php --- a/include/wb/Labels.php +++ b/include/wb/Labels.php @@ -1,6 +1,6 @@ add( $label ); + $this->set( $label ); } } @@ -46,37 +48,55 @@ * @return Label */ public function get( $language ) { - foreach( $this->labels as $label ) { - if( $label->getLanguage() === $language ) { - return $label; - } + 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 false; + return $value; } /** - * Does it have a certain label? + * 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 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; } @@ -104,6 +124,17 @@ 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 * diff --git a/phpunit/WikibaseDataModelTest.php b/phpunit/WikibaseDataModelTest.php new file mode 100644 --- /dev/null +++ b/phpunit/WikibaseDataModelTest.php @@ -0,0 +1,26 @@ +setLabelValue( 'it', 'pizza' ); + $asd = $data->getLabelValue( 'it' ); + $this->assertEquals( $asd, 'pizza' ); + } + + public function testSetDescription() { + $data = new DataModel(); + $data->setDescriptionValue( 'it', 'pizza' ); + $asd = $data->getDescriptionValue( 'it' ); + $this->assertEquals( $asd, 'pizza' ); + } + +} diff --git a/phpunit/WikibaseDescriptionsTest.php b/phpunit/WikibaseDescriptionsTest.php new file mode 100644 --- /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 new file mode 100644 --- /dev/null +++ b/phpunit/WikibaseLabelsTest.php @@ -0,0 +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 ); + } + +}