Net-API-Nominatim
view release on metacpan or search on metacpan
lib/Net/API/Nominatim/Model/Address.pm view on Meta::CPAN
package Net::API::Nominatim::Model::Address;
use strict;
use warnings;
our $VERSION = '0.03';
use Data::Structure::Util qw/unbless/;
use Data::Roundtrip qw/perl2dump json2perl perl2json no-unicode-escape-permanently/;
use Net::API::Nominatim::Model::BoundingBox;
# export nothing otherwise we need to adjust our sub names
# to avoid clashes, e.g. fromHash, use these like
# Net::API::Nominatim::Model::Address::fromHash()
#use Exporter;
#our (@EXPORT_OK, %EXPORT_TAGS);
#BEGIN {
# @EXPORT_OK = qw/
# fromHash fromArray fromJSONHash
# fromJSONArray
# /;
# %EXPORT_TAGS = ( all => [@EXPORT_OK] );
#}
sub new {
my ($class, $params) = @_;
my $self = {
'place_id' => '',
'osm_type' => '',
'osm_id' => '',
'lat' => '',
'lon' => '',
'name' => '',
'type' => '',
'place_rank' => '',
'boundingbox' => undef,
'category' => '',
'addresstype' => '',
'importance' => '',
'display_name' => '',
'licence' => '',
};
bless $self => $class;
return $self unless defined $params;
if( ref($params)eq'HASH' ){
fromHash($params, $self);
} elsif( ref($params)eq'' ){
if( ! defined fromJSONHash($params, $self) ){ print STDERR __PACKAGE__."->new(), line ".__LINE__." : error, input JSON string was malformed, failed.\n"; return undef }
} elsif( ref($params) eq __PACKAGE__ ){
fromHash($params->toHash(), $self);
}
return $self;
}
###########################################
# getters and setters at the same time
#
sub fields { return sort keys %{$_[0]} }
sub place_id { return $_[1] ? $_[0]->{place_id} = $_[1] : $_[0]->{place_id} }
sub osm_type { return $_[1] ? $_[0]->{osm_type} = $_[1] : $_[0]->{osm_type} }
sub osm_id { return $_[1] ? $_[0]->{osm_id} = $_[1] : $_[0]->{osm_id} }
sub lat { return $_[1] ? $_[0]->{lat} = $_[1] : $_[0]->{lat} }
sub lon { return $_[1] ? $_[0]->{lon} = $_[1] : $_[0]->{lon} }
sub name { return $_[1] ? $_[0]->{name} = $_[1] : $_[0]->{name} }
sub type { return $_[1] ? $_[0]->{type} = $_[1] : $_[0]->{type} }
sub place_rank { return $_[1] ? $_[0]->{place_rank} = $_[1] : $_[0]->{place_rank} }
sub boundingbox { return $_[1] ? $_[0]->{boundingbox} = $_[1] : $_[0]->{boundingbox} }
sub category { return $_[1] ? $_[0]->{category} = $_[1] : $_[0]->{category} }
sub addresstype { return $_[1] ? $_[0]->{addresstype} = $_[1] : $_[0]->{addresstype} }
sub importance { return $_[1] ? $_[0]->{importance} = $_[1] : $_[0]->{importance} }
sub display_name { return $_[1] ? $_[0]->{display_name} = $_[1] : $_[0]->{display_name} }
sub licence { return $_[1] ? $_[0]->{licence} = $_[1] : $_[0]->{licence} }
# randomise all fields of CURRENT object to random strings
# (see randomString(length) on how this is done).
# By default empty and undef fields will not be randomised.
# Unless optional 2nd parameter is set to 1. Default is 0.
# It will/can also randomise the boundingbox object.
sub randomise {
my $self = $_[0];
# if a value is undef, shall we keep it as undef or randomise it, including the objects
# the empty strings included
my $keepUndefAndEmpty = $_[1] // 0;
# all the fields except 'boundingbox'
for(grep {$_ ne 'boundingbox'} $self->fields){
$self->{$_} = randomString(5 + int(rand(5)));
}
# the boundingbox
if( exists($self->{boundingbox}) && defined($self->{boundingbox}) ){
$self->{boundingbox}->randomise();
} elsif( $keepUndefAndEmpty > 0 ){
$self->{boundingbox} = Net::API::Nominatim::Model::BoundingBox::fromRandom();
if( ! defined $self->{boundingbox} ){ print STDERR __PACKAGE__."::fromRandom(): error, call to ".'Net::API::Nominatim::Model::BoundingBox::fromRandom()'." has failed.\n"; return undef }
}
return $self;
}
# Clone current object and return the new one.
# It can return undef on failure.
sub clone { return Net::API::Nominatim::Model::Address->new($_[0]->toHash()) }
( run in 1.100 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )