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

JsonParser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 10 2
A canParse() 0 4 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