HttpBuildQueryBehavior   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A postProcessHttpQueryString() 0 11 3
A getHttpQueryCustomProcessor() 0 3 1
A httpBuildQuery() 0 5 1
A setHttpQueryCustomProcessor() 0 5 1
A getEncodeArraysUsingDuplication() 0 3 1
A setEncodeArraysUsingDuplication() 0 5 1
1
<?php
2
3
namespace GinoPane\NanoRest\Request;
4
5
use Closure;
6
7
/**
8
 * Class HttpBuildQueryBehavior
9
 *
10
 * By default http_build_query encodes arrays using php square brackets syntax,
11
 * which sometimes won't work for some endpoints which would expect simple
12
 * duplication of keys
13
 *
14
 * @link https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string
15
 *
16
 * @author Sergey <Gino Pane> Karavay
17
 */
18
trait HttpBuildQueryBehavior
19
{
20
    /**
21
     * Set to true
22
     *
23
     * @var bool
24
     */
25
    private $encodeArraysUsingDuplication = false;
26
27
    /**
28
     * @var Closure
29
     */
30
    private $httpQueryCustomProcessor = null;
31
32
    /**
33
     * @return Closure|null
34
     */
35
    public function getHttpQueryCustomProcessor(): ?Closure
36
    {
37
        return $this->httpQueryCustomProcessor;
38
    }
39
40
    /**
41
     * @param Closure $httpQueryCustomProcessor
42
     *
43
     * @return HttpBuildQueryBehavior|static
44
     */
45
    public function setHttpQueryCustomProcessor(Closure $httpQueryCustomProcessor): self
46
    {
47
        $this->httpQueryCustomProcessor = $httpQueryCustomProcessor;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    public function getEncodeArraysUsingDuplication(): bool
56
    {
57
        return $this->encodeArraysUsingDuplication;
58
    }
59
60
    /**
61
     * @param bool $encodeArraysUsingDuplication
62
     *
63
     * @return HttpBuildQueryBehavior|static
64
     */
65
    public function setEncodeArraysUsingDuplication(bool $encodeArraysUsingDuplication): self
66
    {
67
        $this->encodeArraysUsingDuplication = $encodeArraysUsingDuplication;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Wrapper for http_build_query
74
     *
75
     * @param array $data
76
     *
77
     * @return string
78
     */
79
    private function httpBuildQuery(array $data): string
80
    {
81
        $queryString = http_build_query($data, '', '&');
82
83
        return $this->postProcessHttpQueryString($queryString, $data);
84
    }
85
86
    /**
87
     * @param string $query
88
     * @param array $data
89
     *
90
     * @return string
91
     */
92
    private function postProcessHttpQueryString(string $query, array $data): string
93
    {
94
        if ($this->getEncodeArraysUsingDuplication()) {
95
            $query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query);
96
        }
97
98
        if (!is_null($processor = $this->getHttpQueryCustomProcessor())) {
99
            $query = $processor($query, $data);
100
        }
101
102
        return $query;
103
    }
104
}
105