File Coverage

blib/lib/Perinci/Sub/Wrapper/Patch/HandlePHPArray.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 21 22 95.4


line stmt bran cond sub pod time code
1             package Perinci::Sub::Wrapper::Patch::HandlePHPArray;
2              
3             our $DATE = '2017-07-10'; # DATE
4             our $VERSION = '0.04'; # VERSION
5              
6 1     1   88791 use 5.010001;
  1         5  
7 1     1   7 use strict;
  1         3  
  1         23  
8 1     1   6 use warnings;
  1         3  
  1         32  
9 1     1   3795 use Log::ger;
  1         116  
  1         7  
10              
11 1     1   2002 use parent qw(Module::Patch);
  1         316  
  1         7  
12              
13             my $code = sub {
14             my $ctx = shift;
15              
16             my ($self, %args) = @_;
17              
18             $ctx->{orig}->(@_);
19              
20             $self->select_section('before_call_before_arg_validation');
21              
22             my $args = $self->{_meta}{args};
23             for my $an (sort keys %$args) {
24             my $aspec = $args->{$an};
25             next unless $aspec->{schema};
26             if ($aspec->{schema}[0] eq 'array') {
27             $self->push_lines("if (ref(\$args{$an}) eq 'HASH' && !keys(\%{\$args{$an}})) { \$args{$an} = [] }");
28             }
29             if ($aspec->{schema}[0] eq 'hash') {
30             $self->push_lines("if (ref(\$args{$an}) eq 'ARRAY' && !\@{\$args{$an}}) { \$args{$an} = {} }");
31             }
32             }
33             };
34              
35             sub patch_data {
36             return {
37 1     1 0 60 v => 3,
38             patches => [
39             {
40             action => 'wrap',
41             sub_name => 'handle_args',
42             code => $code,
43             },
44             ],
45             };
46             }
47              
48             1;
49             # ABSTRACT: Convert {} to [] or vice versa to match functions' expectations
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             Perinci::Sub::Wrapper::Patch::HandlePHPArray - Convert {} to [] or vice versa to match functions' expectations
60              
61             =head1 VERSION
62              
63             This document describes version 0.04 of Perinci::Sub::Wrapper::Patch::HandlePHPArray (from Perl distribution Perinci-Sub-Wrapper-Patch-HandlePHPArray), released on 2017-07-10.
64              
65             =head1 SYNOPSIS
66              
67             use Perinci::Sub::Wrapper::HandlePHPArray;
68              
69             =head1 DESCRIPTION
70              
71             This module patches L<Perinci::Sub::Wrapper> so the generated function wrapper
72             code can convert argument C<{}> to C<[]> when function expects argument to be an
73             array, or vice versa C<[]> to C<{}> when function expects a hash argument. This
74             can help if function is being called by PHP clients, because in PHP C<Array()>
75             is ambiguous, it can be an empty hash or an empty array.
76              
77             To make this work, you have to specify schema in your argument specification in
78             your Rinci metadata, and the type must be hash or array.
79              
80             This is a temporary/stop-gap solution. The more "official" solution is to use
81             L<Perinci::Access::HTTP::Server> which has the C<deconfuse_php_clients> option
82             (by default turned on).
83              
84             =head1 HOMEPAGE
85              
86             Please visit the project's homepage at L<https://metacpan.org/release/Perinci-Sub-Wrapper-Patch-HandlePHPArray>.
87              
88             =head1 SOURCE
89              
90             Source repository is at L<https://github.com/perlancar/perl-Perinci-Sub-Wrapper-Patch-HandlePHPArray>.
91              
92             =head1 BUGS
93              
94             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-Wrapper-Patch-HandlePHPArray>
95              
96             When submitting a bug or request, please include a test-file or a
97             patch to an existing test-file that illustrates the bug or desired
98             feature.
99              
100             =head1 AUTHOR
101              
102             perlancar <perlancar@cpan.org>
103              
104             =head1 COPYRIGHT AND LICENSE
105              
106             This software is copyright (c) 2017, 2015, 2014, 2013 by perlancar@cpan.org.
107              
108             This is free software; you can redistribute it and/or modify it under
109             the same terms as the Perl 5 programming language system itself.
110              
111             =cut