package MjPage; # インスタンスを得る sub getPage{ return new MjPage(@_); } # コンストラクタ sub new{ my $class = shift; my($total, $per, $current) = @_; my $this = { total => int $total || 0, per => int $per || 5, current => int $current || 1 }; if($this->{'current'} <= 0 || $this->{'per'} * ($this->{'current'} - 1) + 1 > $this->{'total'}){ $this->{'current'} = 1; } bless $this, $class; return $this; } # 最初の項目のインデックス sub first{ my $this = shift; my $first = $this->{'per'} * ($this->{'current'} - 1) + 1; return ($first <= $this->{'total'}) ? $first : undef; } # 最後の項目のインデックス sub last{ my $this = shift; my $last = $this->{'per'} * $this->{'current'}; if($last > $this->{'total'}){ if($last - $this->{'per'} >= $this->{'total'}){ return undef; } else{ return $this->{'total'}; } } return $last; } # 最初のページ番号 sub firstPage{ return 1; } # 最後のページ番号 sub lastPage{ my $this = shift; my $lastPage = int $this->{'total'} / $this->{'per'}; if($this->{'total'} % $this->{'per'} > 0){ $lastPage++; } return $lastPage; } # 前のページのページ番号 sub prevPage{ my $this = shift; return ($this->{'current'} > 1) ? ($this->{'current'} - 1) : undef; } # 次のページのページ番号 sub nextPage{ my $this = shift; return ($this->{'current'} < $this->lastPage) ? ($this->{'current'} + 1) : undef; } # 総ページ数 sub total{ my $this = shift; my($value) = @_; if(defined $value){ $this->{'total'} = $value; } return $this->{'total'}; } # 現在のページ sub current{ my $this = shift; my($value) = @_; if(defined $value){ $this->{'current'} = $value; } return $this->{'current'}; } # 現在のページの項目数 sub entries{ my $this = shift; return $this->{'total'} ? $this->last - $this->first + 1 : 0; } # 1ページあたりの項目数 sub per{ my $this = shift; my($value) = @_; if(defined $value){ $this->{'per'} = $value; } return $this->{'per'}; } # リストの値を切り出す sub splice{ my $this = shift; my(@list) = @_; # for($) } 1;