NAME

Wikibase::Datatype::Struct::Utils - Wikibase structure serialization utilities.

SYNOPSIS

use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct struct2snaks_array_ref);

my $snaks_hr = obj_array_ref2struct($snaks_ar, $key, $base_uri, $snak_obj, $struct_snak_obj);
my $snaks_ar = struct2snaks_array_ref($struct_hr, $key, $struct_snak_obj);

SUBROUTINES

obj_array_ref2struct

my $snaks_hr = obj_array_ref2struct($snaks_ar, $key, $base_uri, $snak_obj);

Helper subroutine for converting list of Snak objects to snaks structure. This subroutine is used in Statement and Reference module. $base_uri is base URI of Wikibase system (e.g. http://test.wikidata.org/entity/). $snak_obj is object for snak (default value is 'Wikibase::Datatype::Snak'). $struct_snak_obj is object for struct snak (default value is 'Wikibase::Datatype::Struct::Snak').

Returns structure with multiple snaks.

struct2snaks_array_ref

my $snaks_ar = struct2snaks_array_ref($struct_hr, $key, $struct_snak_obj);

Helper subroutine for converting snaks structure to list of Snak objects. This subroutine is used in Statement and Reference module. $struct_snak_obj is object for struct snak (default value is 'Wikibase::Datatype::Struct::Snak').

Returns reference to array with snaks objects.

ERRORS

obj_array_ref2struct():
        Base URI is required.
        Object isn't 'Wikibase::Datatype::Snak'.

struct2snaks_array_ref():
        From Wikibase::Datatype::Snak::new():
                From Wikibase::Datatype::Utils::check_required():
                        Parameter 'datatype' is required.
                        Parameter 'datavalue' is required.
                        Parameter 'property' is required.
                From Wikibase::Datatype::Utils::check_isa():
                        Parameter 'datavalue' must be a 'Wikibase::Datatype::Value::%s' object.
                Parameter 'datatype' = '%s' isn't supported.
                Parameter 'property' must begin with 'P' and number after it.
                Parameter 'snaktype' = '%s' isn't supported.
        From Wikibase::Datatype::Struct::Snak::struct2obj():
                From Wikibase::Datatype::Struct::Value::struct2obj():
                        Entity type '%s' is unsupported.
                        Type doesn't exist.
                        Type '%s' is unsupported.

EXAMPLE1

use strict;
use warnings;

use Data::Printer;
use Wikibase::Datatype::Snak;
use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct);
use Wikibase::Datatype::Value::Item;
use Wikibase::Datatype::Value::String;

my $snak1 = Wikibase::Datatype::Snak->new(
        'datatype' => 'wikibase-item',
        'datavalue' => Wikibase::Datatype::Value::Item->new(
                'value' => 'Q5',
        ),
        'property' => 'P31',
);
my $snak2 = Wikibase::Datatype::Snak->new(
        'datatype' => 'math',
        'datavalue' => Wikibase::Datatype::Value::String->new(
                'value' => 'E = m c^2',
        ),
        'property' => 'P2534',
);

# Convert list of snak objects to structure.
my $snaks_ar = obj_array_ref2struct([$snak1, $snak2], 'snaks',
        'http://test.wikidata.org/entity/');

# Dump to output.
p $snaks_ar;

# Output:
# \ {
#     snaks         {
#         P31     [
#             [0] {
#                 datatype    "wikibase-item",
#                 datavalue   {
#                     type    "wikibase-entityid",
#                     value   {
#                         entity-type   "item",
#                         id            "Q5",
#                         numeric-id    5
#                     }
#                 },
#                 property    "P31",
#                 snaktype    "value"
#             }
#         ],
#         P2534   [
#             [0] {
#                 datatype    "math",
#                 datavalue   {
#                     type    "string",
#                     value   "E = m c^2"
#                 },
#                 property    "P2534",
#                 snaktype    "value"
#             }
#         ]
#     },
#     snaks-order   [
#         [0] "P31",
#         [1] "P2534"
#     ]
# }

EXAMPLE2

use strict;
use warnings;

use Wikibase::Datatype::Struct::Utils qw(struct2snaks_array_ref);

my $struct_hr = {
        'snaks' => {
                'P31' => [{
                        'datatype' => 'wikibase-item',
                        'datavalue' => {
                                'type' => 'wikibase-entityid',
                                'value' => {
                                        'entity-type' => 'item',
                                        'id' => 'Q5',
                                        'numeric-id' => 5,
                                },
                        },
                        'property' => 'P31',
                        'snaktype' => 'value',

                }],
                'P2534' => [{
                        'datatype' => 'math',
                        'datavalue' => {
                                'type' => 'string',
                                'value' => 'E = m c^2',
                        },
                        'property' => 'P2534',
                        'snaktype' => 'value',
                }],
        },
        'snaks-order' => [
                'P31',
                'P2534',
        ],
};

# Convert snaks structure to list of Snak objects.
my $snaks_ar = struct2snaks_array_ref($struct_hr, 'snaks');

# Print out. 
foreach my $snak (@{$snaks_ar}) {
        print 'Property: '.$snak->property."\n";
        print 'Type: '.$snak->datatype."\n";
        print 'Value: '.$snak->datavalue->value."\n";
        print "\n";
}

# Output:
# Property: P31
# Type: wikibase-item
# Value: Q5
#
# Property: P2534
# Type: math
# Value: E = m c^2
#

DEPENDENCIES

Error::Pure, Exporter, List::MoreUtils.

SEE ALSO

Wikibase::Datatype::Struct

Wikibase structure serialization.

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-2023 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.12