| 1 | package JSONAPI::Document::Builder::Compound; | ||||||||||||
| 2 | 6 | $JSONAPI::Document::Builder::Compound::VERSION = '2.0'; | |||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | JSONAPI::Document::Builder::Compound - Compound Resource Document builder | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 VERSION | ||||||||||||
| 8 | |||||||||||||
| 9 | version 2.0 | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||
| 12 | |||||||||||||
| 13 | Builds a compound resource document, which is essentially a resource | ||||||||||||
| 14 | document with all of its relationships and attributes. | ||||||||||||
| 15 | |||||||||||||
| 16 | =cut | ||||||||||||
| 17 | |||||||||||||
| 18 | 6 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 6 | extends 'JSONAPI::Document::Builder'; | |||||||||||
| 20 | |||||||||||||
| 21 | 6 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 6 | ▶ | use JSONAPI::Document::Builder::Relationships; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | =head2 relationships | ||||||||||||
| 25 | |||||||||||||
| 26 | ArrayRef of relationships to include. This | ||||||||||||
| 27 | is populated by the C<include> param of | ||||||||||||
| 28 | a JSON API request. | ||||||||||||
| 29 | |||||||||||||
| 30 | =cut | ||||||||||||
| 31 | |||||||||||||
| 32 | has relationships => ( | ||||||||||||
| 33 | is => 'ro', | ||||||||||||
| 34 | 0 | ▶ | default => sub { [] }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 35 | 6 | ); | |||||||||||
| 36 | |||||||||||||
| 37 | =head2 primary_relationships, nested_relationships | ||||||||||||
| 38 | |||||||||||||
| 39 | Primary relationships are those belonging directly to C<row>, | ||||||||||||
| 40 | while nested relationships is an ArrayRef of HashRefs as follows: | ||||||||||||
| 41 | |||||||||||||
| 42 | [ { primary_related => [qw/primary relationships for primary_related/] }, { ... } ] | ||||||||||||
| 43 | |||||||||||||
| 44 | Where primary_related is the relationship for C<row>, and | ||||||||||||
| 45 | its associated ArrayRef contains relationships for it. | ||||||||||||
| 46 | |||||||||||||
| 47 | =cut | ||||||||||||
| 48 | |||||||||||||
| 49 | 6 | has primary_relationships => (is => 'lazy'); | |||||||||||
| 50 | 6 | has nested_relationships => (is => 'lazy'); | |||||||||||
| 51 | |||||||||||||
| 52 | sub _build_primary_relationships { | ||||||||||||
| 53 | 5 | ▶ | return [map { $_ } grep { ref($_) ne 'HASH' } @{ $_[0]->relationships }]; | ||||||||||
|
Subroutine
_build_primary_relationships: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | sub _build_nested_relationships { | ||||||||||||
| 57 | 5 | ▶ | return [map { $_ } grep { ref($_) eq 'HASH' } @{ $_[0]->relationships }]; | ||||||||||
|
Subroutine
_build_nested_relationships: called CC=1 SCAR=0.0
| |||||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | =head2 build_document : HashRef | ||||||||||||
| 61 | |||||||||||||
| 62 | Builds a HashRef for the primary resource document. | ||||||||||||
| 63 | |||||||||||||
| 64 | When C<relationships> is populated, will include | ||||||||||||
| 65 | a relationships entry in the document, populated | ||||||||||||
| 66 | with related links and identifiers. | ||||||||||||
| 67 | |||||||||||||
| 68 | =cut | ||||||||||||
| 69 | |||||||||||||
| 70 | sub build_document { | ||||||||||||
| 71 | 5 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
build_document: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 72 | |||||||||||||
| 73 | 5 | my $document = $self->build(); | |||||||||||
| 74 | |||||||||||||
| 75 | 5 | my %relationships; | |||||||||||
| 76 | 5 | foreach my $relationship (@{ $self->primary_relationships }, | |||||||||||
| 77 | 2 | map { $_ } map { keys(%$_) } @{ $self->nested_relationships }) | |||||||||||
| 78 | { | ||||||||||||
| 79 | 6 | my $relationship_type = $self->format_type($relationship); | |||||||||||
| 80 | 6 | $relationships{$relationship_type} = $self->build_relationship($relationship); | |||||||||||
| 81 | } | ||||||||||||
| 82 | 5 | ▶ | if (values(%relationships)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 5 | $document->{relationships} = \%relationships; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 5 | return $document; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | =head2 build_relationships : ArrayRef | ||||||||||||
| 90 | |||||||||||||
| 91 | Builds an ArrayRef containing all given relationships. | ||||||||||||
| 92 | These relationships are built with their attributes. | ||||||||||||
| 93 | |||||||||||||
| 94 | =cut | ||||||||||||
| 95 | |||||||||||||
| 96 | sub build_relationships { | ||||||||||||
| 97 | 5 | ▶ | my ($self, $relationships, $fields) = @_; | ||||||||||
|
Subroutine
build_relationships: called CC=15 SCAR=27.6
Pod
documented
| |||||||||||||
| 98 | 5 | ▶ | $fields //= {}; | ||||||||||
|
Condition50%1/2
$fields //= {}
MC/DC0%0/1
$fields //= {}
$fields
Truth table50%1/2
A
A= $fields
| |||||||||||||
| 99 | 5 | ▶ | return [] unless $relationships; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | |||||||||||||
| 101 | 5 | ▶ | if (ref($relationships) ne 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 0 | Carp::confess('Invalid request: relationships must be an array ref.'); | |||||||||||
| 103 | } | ||||||||||||
| 104 | |||||||||||||
| 105 | 5 | ▶ | return [] unless @$relationships; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | |||||||||||||
| 107 | 5 | my @included; | |||||||||||
| 108 | |||||||||||||
| 109 | 5 | foreach my $relation (sort @{ $self->primary_relationships }) { | |||||||||||
| 110 | 4 | my $result = $self->build_relationship($relation, $fields->{$relation}, { with_attributes => 1 }); | |||||||||||
| 111 | 4 | ▶ | if (my $related_docs = $result->{data}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 4 | ▶ | if (ref($related_docs) eq 'ARRAY') { # plural relations | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 2 | push @included, @$related_docs; | |||||||||||
| 114 | } else { # singular relations | ||||||||||||
| 115 | 2 | push @included, $related_docs; | |||||||||||
| 116 | } | ||||||||||||
| 117 | } | ||||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | # Note that this fetches the relationship on $self->row, so it shouldn't be done above. | ||||||||||||
| 121 | 5 | foreach my $nested (@{ $self->nested_relationships }) { | |||||||||||
| 122 | 2 | my ($relation_source) = keys(%$nested); | |||||||||||
| 123 | my $result_ref = | ||||||||||||
| 124 | 2 | $self->build_relationship($relation_source, $fields->{$relation_source}, { with_attributes => 1 })->{data}; | |||||||||||
| 125 | |||||||||||||
| 126 | 2 | ▶ | if (ref($result_ref) eq 'ARRAY') { # The source relation is a has_many, link the nested resources for each one. | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | 1 | my $source_row = $self->row->$relation_source; | |||||||||||
| 128 | 1 | ▶ | if ($source_row->can('all')) { # Check if any overlaying dbix resultset class can do "all" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | my $includes = | ||||||||||||
| 130 | 1 | $self->build_nested_from_resultset($source_row, $result_ref, $nested->{$relation_source}, $fields); | |||||||||||
| 131 | 1 | push @included, $_ for @$includes; | |||||||||||
| 132 | } | ||||||||||||
| 133 | } else { | ||||||||||||
| 134 | 1 | my $source_row = $self->row->$relation_source; | |||||||||||
| 135 | 1 | my %relationships; | |||||||||||
| 136 | 1 | foreach my $relationship (@{ $nested->{$relation_source} }) { | |||||||||||
| 137 | 1 | my $relationship_type = $self->format_type($relationship); | |||||||||||
| 138 | my ($related_data, $includes) = $self->build_nested_relationship( | ||||||||||||
| 139 | $source_row, $relationship, | ||||||||||||
| 140 | 1 | $fields->{$relationship}, | |||||||||||
| 141 | { with_attributes => 1 }); | ||||||||||||
| 142 | 1 | $relationships{$relationship_type} = $related_data; | |||||||||||
| 143 | 1 | push @included, $_ for @$includes; | |||||||||||
| 144 | } | ||||||||||||
| 145 | 1 | ▶ | if (values(%relationships)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | 1 | $result_ref->{relationships} = \%relationships; | |||||||||||
| 147 | } | ||||||||||||
| 148 | 1 | push @included, $result_ref; | |||||||||||
| 149 | } | ||||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | 5 | return \@included; | |||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | =head2 build_nested_relationship(Str $primary, Str $relationship, ArrayRef $fields, HashRef $options?) : Array | ||||||||||||
| 156 | |||||||||||||
| 157 | Uses build_relationship with the rows related resource as | ||||||||||||
| 158 | the C<row> argument so the builder can find the relationship. | ||||||||||||
| 159 | |||||||||||||
| 160 | =cut | ||||||||||||
| 161 | |||||||||||||
| 162 | sub build_nested_relationship { | ||||||||||||
| 163 | 3 | ▶ | my ($self, $primary_row, $relationship, $fields, $options) = @_; | ||||||||||
|
Subroutine
build_nested_relationship: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 164 | 3 | ▶ | $options //= {}; | ||||||||||
|
Condition50%1/2
$options //= {}
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||
| 165 | my $builder = JSONAPI::Document::Builder::Relationships->new({ | ||||||||||||
| 166 | api_url => $self->api_url, | ||||||||||||
| 167 | chi => $self->chi, | ||||||||||||
| 168 | segmenter => $self->segmenter, | ||||||||||||
| 169 | fields => $fields, | ||||||||||||
| 170 | kebab_case_attrs => $self->kebab_case_attrs, | ||||||||||||
| 171 | row => $primary_row, | ||||||||||||
| 172 | relationship => $relationship, | ||||||||||||
| 173 | with_attributes => $options->{with_attributes}, | ||||||||||||
| 174 | 3 | }); | |||||||||||
| 175 | 3 | my $document = $builder->build(); | |||||||||||
| 176 | 3 | my ($data, $included); | |||||||||||
| 177 | 3 | ▶ | if (my $doc_data = $document->{data}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | 3 | ▶ | if (ref($doc_data) eq 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 179 | 1 | $data = []; | |||||||||||
| 180 | 1 | foreach my $doc (@$doc_data) { | |||||||||||
| 181 | 1 | push @$data, { id => $doc->{id}, type => $doc->{type} }; | |||||||||||
| 182 | 1 | push @$included, $doc; | |||||||||||
| 183 | } | ||||||||||||
| 184 | } else { | ||||||||||||
| 185 | 2 | $data = { id => $doc_data->{id}, type => $doc_data->{type} }; | |||||||||||
| 186 | 2 | push @$included, $doc_data; | |||||||||||
| 187 | } | ||||||||||||
| 188 | } | ||||||||||||
| 189 | 3 | return ({ data => $data }, $included); | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | sub build_nested_from_resultset { | ||||||||||||
| 193 | 1 | ▶ | my ($self, $source_row, $primary_docs, $nested_relations, $fields) = @_; | ||||||||||
|
Subroutine
build_nested_from_resultset: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||
| 194 | 1 | my @included; | |||||||||||
| 195 | 1 | my @results = $source_row->all(); | |||||||||||
| 196 | 1 | foreach my $primary_doc (@$primary_docs) { | |||||||||||
| 197 | 2 | ▶ | my $row = List::Util::first { $_->id eq $primary_doc->{id} } @results; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 198 | 2 | my %relationships; | |||||||||||
| 199 | 2 | foreach my $relationship (@$nested_relations) { | |||||||||||
| 200 | 2 | my $relationship_type = $self->format_type($relationship); | |||||||||||
| 201 | my ($related_data, $includes) = $self->build_nested_relationship( | ||||||||||||
| 202 | $row, $relationship, | ||||||||||||
| 203 | 2 | $fields->{$relationship}, | |||||||||||
| 204 | { with_attributes => 1 }, | ||||||||||||
| 205 | ); | ||||||||||||
| 206 | 2 | $relationships{$relationship_type} = $related_data; | |||||||||||
| 207 | 2 | foreach my $include (@$includes) { | |||||||||||
| 208 | 2 | ▶ | unless ( # avoid having multiple nested relationships of the same type | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 209 | List::Util::any { | ||||||||||||
| 210 | $_->{id} eq $include->{id} | ||||||||||||
| 211 | && $_->{type} eq $include->{type} | ||||||||||||
| 212 | 1 | ▶ | } | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 213 | @included | ||||||||||||
| 214 | ) | ||||||||||||
| 215 | { | ||||||||||||
| 216 | 1 | push @included, $_ for @$includes; | |||||||||||
| 217 | } | ||||||||||||
| 218 | } | ||||||||||||
| 219 | } | ||||||||||||
| 220 | 2 | ▶ | if (values(%relationships)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | 2 | $primary_doc->{relationships} = \%relationships; | |||||||||||
| 222 | } | ||||||||||||
| 223 | 2 | push @included, $primary_doc; | |||||||||||
| 224 | } | ||||||||||||
| 225 | 1 | return \@included; | |||||||||||
| 226 | } | ||||||||||||
| 227 | |||||||||||||
| 228 | 6 | 1; | |||||||||||