Completed
Pull Request — develop (#22)
by Adam
01:21
created

AbstractHydrator::isJsonResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace IBM\Watson\Common\Hydrator;
6
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * AbstractHydrator provides common protected methods other hydrators might need to use.
11
 */
12
abstract class AbstractHydrator implements HydratorInterface
13
{
14
    const HEADER_CONTENT_TYPE = 'Content-Type';
15
16
    /**
17
     * @param \Psr\Http\Message\ResponseInterface $response Response to check.
18
     *
19
     * @return bool
20
     *
21
     */
22
    protected function isJsonResponse(ResponseInterface $response): bool
23
    {
24
        return \strpos($response->getHeaderLine(self::HEADER_CONTENT_TYPE), 'application/json') === 0;
25
    }
26
27
    /**
28
     * @param \Psr\Http\Message\ResponseInterface $response Response to parse.
29
     *
30
     * @return string
31
     *
32
     */
33
    protected function getBody(ResponseInterface $response): string
34
    {
35
        return $response->getBody()->__toString();
36
    }
37
38
    /**
39
     * @param \Psr\Http\Message\ResponseInterface $response Response to parse.
40
     *
41
     * @return array|null
42
     *
43
     */
44
    protected function getBodyContent(ResponseInterface $response): ?array
45
    {
46
        return json_decode($this->getBody($response), true);
47
    }
48
}
49