Completed
Push — master ( bca24d...1b0d8b )
by John
03:18
created

JsonParser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\PhpApi\Descriptions\Description\Document\Parser;
10
11
/**
12
 * @author John Kleijn <[email protected]>
13
 */
14
class JsonParser implements Parser
15
{
16
    /**
17
     * @param string $string
18
     *
19
     * @return mixed
20
     * @throws ParseException
21
     */
22
    public function parse(string $string)
23
    {
24
        $content = json_decode($string);
25
26
        if (JSON_ERROR_NONE !== json_last_error()) {
27
            throw new ParseException("Failed to parse as JSON");
28
        }
29
30
        return $content;
31
    }
32
33
    /**
34
     * @param string $contentType
35
     *
36
     * @return bool
37
     */
38
    public function canParse(string $contentType): bool
39
    {
40
        return strpos($contentType, 'json') == strlen($contentType) - 4;
41
    }
42
}
43