NAME

Wikibase::Datatype::Struct::Value::Quantity - Wikibase quantity structure serialization.

SYNOPSIS

 use Wikibase::Datatype::Struct::Value::Quantity qw(obj2struct struct2obj);

 my $struct_hr = obj2struct($obj, $base_uri);
 my $obj = struct2obj($struct_hr);

DESCRIPTION

This conversion is between objects defined in Wikibase::Datatype and structures serialized via JSON to MediaWiki.

SUBROUTINES

obj2struct

 my $struct_hr = obj2struct($obj, $base_uri);

Convert Wikibase::Datatype::Value::Quantity instance to structure. $base_uri is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/).

Returns reference to hash with structure.

struct2obj

 my $obj = struct2obj($struct_hr);

Convert structure of quantity to object.

Returns Wikibase::Datatype::Value::Quantity instance.

ERRORS

 obj2struct():
         Base URI is required.
         Object doesn't exist.
         Object isn't 'Wikibase::Datatype::Value::Quantity'.

 struct2obj():
         Structure isn't for 'quantity' datatype.

EXAMPLE1

 use strict;
 use warnings;

 use Data::Printer;
 use Wikibase::Datatype::Value::Quantity;
 use Wikibase::Datatype::Struct::Value::Quantity qw(obj2struct);

 # Object.
 my $obj = Wikibase::Datatype::Value::Quantity->new(
         'unit' => 'Q190900',
         'value' => 10,
 );

 # Get structure.
 my $struct_hr = obj2struct($obj, 'http://test.wikidata.org/entity/');

 # Dump to output.
 p $struct_hr;

 # Output:
 # \ {
 #     type    "quantity",
 #     value   {
 #         amount   "+10",
 #         unit     "http://test.wikidata.org/entity/Q190900"
 #     }
 # }

EXAMPLE2

 use strict;
 use warnings;

 use Wikibase::Datatype::Struct::Value::Quantity qw(struct2obj);

 # Quantity structure.
 my $struct_hr = {
         'type' => 'quantity',
         'value' => {
                 'amount' => '+10',
                 'unit' => 'http://test.wikidata.org/entity/Q190900',
         },
 };

 # Get object.
 my $obj = struct2obj($struct_hr);

 # Get type.
 my $type = $obj->type;

 # Get unit.
 my $unit = $obj->unit;

 # Get value.
 my $value = $obj->value;

 # Print out.
 print "Type: $type\n";
 if (defined $unit) {
         print "Unit: $unit\n";
 }
 print "Value: $value\n";

 # Output:
 # Type: quantity
 # Unit: Q190900
 # Value: 10

DEPENDENCIES

Error::Pure, Exporter, Readonly, URI, Wikibase::Datatype::Value::Property.

SEE ALSO

Wikibase::Datatype::Struct

Wikibase structure serialization.

Wikibase::Datatype::Value::Quantity

Wikibase quantity value datatype.

REPOSITORY

https://github.com/michal-josef-spacek/Wikibase-Datatype-Struct

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2020-2022 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.09