| 1 | package Wikibase::Datatype::Struct::Statement; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 28 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 28 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 28 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 28 | ▶ | use Error::Pure qw(err); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 28 | ▶ | use Readonly; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 28 | ▶ | use Wikibase::Datatype::Statement; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 28 | ▶ | use Wikibase::Datatype::Struct::Reference; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 28 | ▶ | use Wikibase::Datatype::Struct::Snak; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 28 | ▶ | use Wikibase::Datatype::Struct::Utils qw(obj_array_ref2struct struct2snaks_array_ref); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 28 | Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); | |||||||||||||
| 15 | |||||||||||||||
| 16 | 28 | our $VERSION = 0.15; | |||||||||||||
| 17 | |||||||||||||||
| 18 | 20 | ▶ | sub obj2struct { | ||||||||||||
|
Subroutine
obj2struct: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||
| 19 | 20 | my ($obj, $base_uri) = @_; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 20 | ▶ | if (! defined $obj) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | 1 | err "Object doesn't exist."; | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | 19 | ▶ | if (! $obj->isa('Wikibase::Datatype::Statement')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 1 | err "Object isn't 'Wikibase::Datatype::Statement'."; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | 18 | ▶ | if (! defined $base_uri) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 0 | err 'Base URI is required.'; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | my $struct_hr = { | ||||||||||||||
| 32 | defined $obj->id ? ('id' => $obj->id) : (), | ||||||||||||||
| 33 | 'mainsnak' => Wikibase::Datatype::Struct::Snak::obj2struct($obj->snak, $base_uri), | ||||||||||||||
| 34 | 18 | @{$obj->property_snaks} ? ( | |||||||||||||
| 35 | 2 | ▶ | %{obj_array_ref2struct($obj->property_snaks, 'qualifiers', $base_uri)}, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | ) : (), | ||||||||||||||
| 37 | 'rank' => $obj->rank, | ||||||||||||||
| 38 | 18 | @{$obj->references} ? ( | |||||||||||||
| 39 | 'references' => [ | ||||||||||||||
| 40 | 4 | map { Wikibase::Datatype::Struct::Reference::obj2struct($_, $base_uri); } | |||||||||||||
| 41 | 18 | ▶ | @{$obj->references}, | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 42 | ], | ||||||||||||||
| 43 | ) : (), | ||||||||||||||
| 44 | 'type' => 'statement', | ||||||||||||||
| 45 | }; | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 18 | return $struct_hr; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 10 | ▶ | sub struct2obj { | ||||||||||||
|
Subroutine
struct2obj: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 51 | 10 | my $struct_hr = shift; | |||||||||||||
| 52 | |||||||||||||||
| 53 | my $obj = Wikibase::Datatype::Statement->new( | ||||||||||||||
| 54 | exists $struct_hr->{'id'} ? ('id' => $struct_hr->{'id'}) : (), | ||||||||||||||
| 55 | 'property_snaks' => struct2snaks_array_ref($struct_hr, 'qualifiers'), | ||||||||||||||
| 56 | 'snak' => Wikibase::Datatype::Struct::Snak::struct2obj($struct_hr->{'mainsnak'}), | ||||||||||||||
| 57 | 'references' => [ | ||||||||||||||
| 58 | 4 | map { Wikibase::Datatype::Struct::Reference::struct2obj($_) } | |||||||||||||
| 59 | 10 | @{$struct_hr->{'references'}} | |||||||||||||
| 60 | ], | ||||||||||||||
| 61 | 10 | ▶ | 'rank' => $struct_hr->{'rank'}, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | ); | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 10 | return $obj; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 28 | 1; | |||||||||||||
| 68 | |||||||||||||||
| 69 | __END__ | ||||||||||||||