Biblio-SICI

 view release on metacpan or  search on metacpan

lib/Biblio/SICI/ItemSegment.pm  view on Meta::CPAN


package Biblio::SICI::ItemSegment;
{
  $Biblio::SICI::ItemSegment::VERSION = '0.04';
}

# ABSTRACT: The item segment of a SICI

use strict;
use warnings;
use 5.010001;

use Moo;
use Sub::Quote;

use Business::ISSN;

use Biblio::SICI;
with 'Biblio::SICI::Role::ValidSegment', 'Biblio::SICI::Role::RecursiveLink';


has 'issn' => ( is => 'rw', trigger => 1, predicate => 1, clearer => 1, );

sub _trigger_issn {
	my ( $self, $newVal ) = @_;
	my @problems = ();

	if ( $newVal !~ m!\A[0-9X-]+\Z! ) {
		push @problems, 'contains invalid characters';
	}

	if ( $newVal !~ m!\A[0-9]{4}\-[0-9]{3}[0-9X]\Z! ) {
		push @problems, 'structural error';
	}

	unless (@problems) {
		if ( my $is = Business::ISSN->new($newVal) ) {
			unless ( $is->is_valid ) {
				push @problems, 'invalid issn';
			}
		}
	}

	if (@problems) {
		$self->log_problem_on( 'issn' => [@problems] );
	}
	else {
		$self->clear_problem_on('issn');
	}

	return;
} ## end sub _trigger_issn


has 'chronology' => ( is => 'rw', trigger => 1, predicate => 1, clearer => 1, );

sub _trigger_chronology {
	my ( $self, $newVal ) = @_;

	# TODO calendar schemes other than Gregorian may be used?!

	my @problems = ();

	# 1 YYYY
	# 2 YYYY/YYYY with second YYYY > first YYYY
	# 3 YYYYMM
	# 4 YYYYMM/MM with second MM > first MM
	# 5 YYYYMM/YYYYMM with second YYYY > first YYYY
	# 6 YYYYMMDD
	# 7 YYYYMMDD/DD with second DD > first DD
	# 8 YYYYMMDD/MMDD with second MM > first MM
	# 9 YYYYMMDD/YYYYMMDD with second YYYY > first YYYY

	my (%e) = ();

	if ( $newVal =~ /\A[0-9]{4}\Z/ ) {
		$e{'1Y'} = $newVal;
	}
	elsif ( $newVal =~ /\A([0-9]{4})\/([0-9]{4})\Z/ ) {
		$e{'1Y'} = $1;
		$e{'2Y'} = $2;

		unless ( $e{'2Y'} > $e{'1Y'} ) {
			push @problems, 'if specified, second year must be larger than first year';
		}
	}
	elsif ( $newVal =~ /\A([0-9]{4})([0-9]{2})\Z/ ) {
		$e{'1Y'} = $1;
		$e{'1M'} = $2;
	}
	elsif ( $newVal =~ /\A([0-9]{4})([0-9]{2})\/([0-9]{2})\Z/ ) {
		$e{'1Y'} = $1;
		$e{'1M'} = $2;
		$e{'2M'} = $3;

		unless ( $e{'2M'} > $e{'1M'} ) {
			push @problems, 'if specified, second month must be larger than first month';
		}
	}
	elsif ( $newVal =~ /\A([0-9]{4})([0-9]{2})\/([0-9]{4})([0-9]{2})\Z/ ) {
		$e{'1Y'} = $1;
		$e{'1M'} = $2;
		$e{'2Y'} = $3;
		$e{'2M'} = $4;

		unless ( $e{'2Y'} > $e{'1Y'} ) {
			push @problems, 'if specified, second year must be larger than first year';
		}
	}
	elsif ( $newVal =~ /\A([0-9]{4})([0-9]{2})([0-9]{2})\Z/ ) {
		$e{'1Y'} = $1;
		$e{'1M'} = $2;
		$e{'1D'} = $3;
	}
	elsif ( $newVal =~ /\A([0-9]{4})([0-9]{2})([0-9]{2})\/([0-9]{2})\Z/ ) {
		$e{'1Y'} = $1;
		$e{'1M'} = $2;
		$e{'1D'} = $3;
		$e{'2D'} = $4;



( run in 1.152 second using v1.01-cache-2.11-cpan-364913b4093 )