| 1 | package Poz::Types::is; | ||
| 2 | use parent 'Poz::Types::scalar'; | ||
| 3 | use 5.032; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my ($class, $is) = @_; | ||
| 9 | my $opts = { | ||
| 10 | is => $is, | ||
| 11 | required_error => 'required', | ||
| 12 | invalid_type_error => 'Not a ' . $is, | ||
| 13 | }; | ||
| 14 | my $self = $class->SUPER::new($opts); | ||
| 15 | return $self; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub rule { | ||
| 19 | my ($self, $value) = @_; | ||
| 20 | return $self->{required_error} unless defined $value; | ||
| 21 | return $self->{invalid_type_error} unless $value->isa($self->{is}); | ||
| 22 | return; | ||
| 23 | } | ||
| 24 | |||
| 25 | 1; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Poz::Types::is - Type handling for Poz framework | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use Poz qw/z/; | ||
| 34 | |||
| 35 | my $is_type = z->is('Some::Class'); | ||
| 36 | my $obj = bless {}, 'Some::Class'; | ||
| 37 | my $other_obj = bless {}, 'Some::Other::Class'; | ||
| 38 | $is_type->parse($obj); # returns $obj | ||
| 39 | $is_type->parse($other_obj); # throws exception | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | Poz::Types::is provides methods to handle types based on their class within the Poz. It allows setting default values, marking values as nullable or optional, and coercing values. | ||
| 44 | |||
| 45 | =head2 METHODS | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item new | ||
| 50 | |||
| 51 | Creates a new instance of Poz::Types::is. | ||
| 52 | |||
| 53 | =item rule | ||
| 54 | |||
| 55 | Validates the value against the type. | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =head1 SEE ALSO | ||
| 60 | |||
| 61 | L<Poz::Types>, L<Poz::Types::scalar> | ||
| 62 | |||
| 63 | =head1 AUTHOR | ||
| 64 | |||
| 65 | ytnobody E<lt>ytnobody@gmail.comE<gt> | ||
| 66 | |||
| 67 | =cut |