Passed
Branch issue_11 (3b88d6)
by Jay
13:12 queued 23s
created

Json::getJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Request;
3
4
/**
5
 * Class Json
6
 * @package FMUP\Request
7
 */
8
class Json extends Http
9
{
10
    private $json;
11
12
    /**
13
     * Transform request json body to array
14
     * @return array
15
     */
16 2
    private function getJson()
17
    {
18 2
        return $this->json = (array)($this->json ?: json_decode($this->getRequestContent(), true));
19
    }
20
21
    /**
22
     * Retrieve content of the request - do not use
23
     * @codeCoverageIgnore
24
     * @return string|null
25
     */
26
    protected function getRequestContent()
27
    {
28
        return file_get_contents('php://input');
29
    }
30
31
    /**
32
     * Return requested value in Json structure
33
     * @param string $name Name to retrieve
34
     * @param mixed $defaultValue Value returned if name is not defined in query
35
     * @return mixed
36
     */
37 1
    public function get($name, $defaultValue = null)
38
    {
39 1
        return $this->has($name) ? $this->getJson()[$name] : $defaultValue;
40
    }
41
42
    /**
43
     * Check if a parameter name exists
44
     * @param string $name
45
     * @return bool
46
     */
47 2
    public function has($name)
48
    {
49 2
        return array_key_exists($name, $this->getJson());
50
    }
51
}
52