| 1 | package Wikibase::Datatype::Struct::Value::Quantity; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 54 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 54 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 54 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 54 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 54 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 54 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 54 | ▶ | use Wikibase::Datatype::Value::Quantity; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 54 | Readonly::Array our @EXPORT_OK => qw(obj2struct struct2obj); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 54 | our $VERSION = 0.15; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | sub obj2struct { | ||||||||||||||||||||||||||
|
Subroutine
obj2struct: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 7 | my ($obj, $base_uri) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 7 | ▶ | if (! defined $obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | err "Object doesn't exist."; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 6 | ▶ | if (! $obj->isa('Wikibase::Datatype::Value::Quantity')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 1 | err "Object isn't 'Wikibase::Datatype::Value::Quantity'."; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | 5 | ▶ | if (! defined $base_uri) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1 | err 'Base URI is required.'; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | my $amount = $obj->value; | |||||||||||||||||||||||||||
| 30 | 4 | $amount = _add_plus($amount); | |||||||||||||||||||||||||||
| 31 | 4 | my $unit; | |||||||||||||||||||||||||||
| 32 | 4 | ▶ | if (defined $obj->unit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 3 | $unit = $base_uri.$obj->unit; | |||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||
| 35 | 1 | $unit = '1'; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 4 | ▶ | my $struct_hr = { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 38 | 'value' => { | ||||||||||||||||||||||||||||
| 39 | 'amount' => $amount, | ||||||||||||||||||||||||||||
| 40 | defined $obj->lower_bound ? ( | ||||||||||||||||||||||||||||
| 41 | 'lowerBound' => _add_plus($obj->lower_bound), | ||||||||||||||||||||||||||||
| 42 | ) : (), | ||||||||||||||||||||||||||||
| 43 | 'unit' => $unit, | ||||||||||||||||||||||||||||
| 44 | defined $obj->upper_bound ? ( | ||||||||||||||||||||||||||||
| 45 | 'upperBound' => _add_plus($obj->upper_bound), | ||||||||||||||||||||||||||||
| 46 | ) : (), | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | 'type' => 'quantity', | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | return $struct_hr; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | sub struct2obj { | ||||||||||||||||||||||||||
|
Subroutine
struct2obj: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 4 | my $struct_hr = shift; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | if (! exists $struct_hr->{'type'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not exists $struct_hr->{"type"} or $struct_hr->{"type"} ne "quantity"
MC/DC0%0/2
not exists $struct_hr->{"type"} or $struct_hr->{"type"} ne "quantity"
not exists $struct_hr->{"type"}
$struct_hr->{"type"} ne "quantity"
Truth table33%1/3
not(A) or B
A= not exists $struct_hr->{"type"}B= $struct_hr->{"type"} ne "quantity"
| |||||||||||||||||||||||||||||
| 58 | || $struct_hr->{'type'} ne 'quantity') { | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | err "Structure isn't for 'quantity' datatype."; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | my $amount = $struct_hr->{'value'}->{'amount'}; | |||||||||||||||||||||||||||
| 64 | 4 | $amount = _remove_plus($amount); | |||||||||||||||||||||||||||
| 65 | 4 | my $unit = $struct_hr->{'value'}->{'unit'}; | |||||||||||||||||||||||||||
| 66 | 4 | ▶ | if ($unit eq 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | $unit = undef; | |||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 3 | my $u = URI->new($unit); | |||||||||||||||||||||||||||
| 70 | 3 | my @path_segments = $u->path_segments; | |||||||||||||||||||||||||||
| 71 | 3 | $unit = $path_segments[-1]; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | my $obj = Wikibase::Datatype::Value::Quantity->new( | ||||||||||||||||||||||||||||
| 74 | $struct_hr->{'value'}->{'lowerBound'} ? ( | ||||||||||||||||||||||||||||
| 75 | 'lower_bound' => _remove_plus($struct_hr->{'value'}->{'lowerBound'}), | ||||||||||||||||||||||||||||
| 76 | ) : (), | ||||||||||||||||||||||||||||
| 77 | 'unit' => $unit, | ||||||||||||||||||||||||||||
| 78 | $struct_hr->{'value'}->{'upperBound'} ? ( | ||||||||||||||||||||||||||||
| 79 | 4 | ▶ | 'upper_bound' => _remove_plus($struct_hr->{'value'}->{'upperBound'}), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 80 | ) : (), | ||||||||||||||||||||||||||||
| 81 | 'value' => $amount, | ||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | return $obj; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | ▶ | sub _add_plus { | ||||||||||||||||||||||||||
|
Subroutine
_add_plus: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 88 | 6 | my $value = shift; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | if ($value =~ m/^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 6 | $value = '+'.$value; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 6 | return $value; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 6 | ▶ | sub _remove_plus { | ||||||||||||||||||||||||||
|
Subroutine
_remove_plus: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 98 | 6 | my $value = shift; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 6 | ▶ | if ($value =~ m/^\+(\d+)$/ms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 6 | $value = $1; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 6 | return $value; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 54 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||