| 1 | package JSON::Pointer::Extend; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use JSON::Pointer; | ||
| 8 | use Carp qw(); | ||
| 9 | |||
| 10 | our $VERSION = '0.01'; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($class, %opt) = @_; | ||
| 14 | my $self = {}; | ||
| 15 | bless $self, $class; | ||
| 16 | |||
| 17 | $opt{'-pointer'} && $self->pointer(delete $opt{'-pointer'}); | ||
| 18 | $opt{'-document'} && $self->document(delete $opt{'-document'}); | ||
| 19 | |||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub process { | ||
| 24 | my ($self) = @_; | ||
| 25 | my $pointer = $self->pointer // Carp::croak("'pointer' not defined"); | ||
| 26 | |||
| 27 | for my $key (keys %$pointer) { | ||
| 28 | $self->_recursive_process($self->document, $key, $pointer->{$key}); | ||
| 29 | } | ||
| 30 | return 1; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _recursive_process { | ||
| 34 | my ($self, $document, $pointer, $cb) = @_; | ||
| 35 | |||
| 36 | if ($pointer =~ /(.*?)\/\*(.+)?/) { | ||
| 37 | my $path = $1; | ||
| 38 | my $tail = $2; | ||
| 39 | my $arr_ref = JSON::Pointer->get($document, $path); | ||
| 40 | if (ref($arr_ref) ne 'ARRAY') { | ||
| 41 | Carp::croak("Path '$path' not array"); | ||
| 42 | } | ||
| 43 | |||
| 44 | my @arr = @$arr_ref; | ||
| 45 | if ($tail) { | ||
| 46 | for my $el (@arr) { | ||
| 47 | $self->_recursive_process($el, $tail, $cb); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | my ($root_pointer, $field_name) = $pointer =~ /(.*)\/(.+)/; | ||
| 52 | for my $el (@arr) { | ||
| 53 | $cb->($el, JSON::Pointer->get($document, $root_pointer), $field_name); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | elsif ($pointer eq '') { | ||
| 58 | my $path = ''; | ||
| 59 | my $arr_ref = JSON::Pointer->get($document, $path); | ||
| 60 | if (ref($arr_ref) ne 'ARRAY') { | ||
| 61 | Carp::croak("Path '$path' not array"); | ||
| 62 | } | ||
| 63 | |||
| 64 | my @arr = @$arr_ref; | ||
| 65 | for my $el (@arr) { | ||
| 66 | $cb->($el, $arr_ref, undef); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | my ($root_pointer, $field_name) = $pointer =~ /(.+)?\/(.+)/; | ||
| 71 | $cb->(JSON::Pointer->get($document, $pointer), $root_pointer ? JSON::Pointer->get($document, $root_pointer) : $document, $field_name); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | ############################### GET/SET METHODS ############################## | ||
| 76 | |||
| 77 | sub document { | ||
| 78 | if (scalar(@_) > 1) { | ||
| 79 | my $ref = ref($_[1]); | ||
| 80 | if ($ref ne 'HASH' && $ref ne 'ARRAY') { | ||
| 81 | Carp::croak("'document' must be a hashref or arrayref"); | ||
| 82 | } | ||
| 83 | $_[0]->{'document'} = $_[1]; | ||
| 84 | } | ||
| 85 | return $_[0]->{'document'}; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub pointer { | ||
| 89 | if (scalar(@_) > 1) { | ||
| 90 | if (ref($_[1]) ne 'HASH') { | ||
| 91 | Carp::croak("'pointer' must be a hashref"); | ||
| 92 | } | ||
| 93 | $_[0]->{'pointer'} = $_[1]; | ||
| 94 | } | ||
| 95 | return $_[0]->{'pointer'}; | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | __END__ |