Completed
Branch master (1f9106)
by Nils
02:44
created

ValidRule::doValidation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Json;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\Rule;
7
use whm\Smoke\Rules\StandardRule;
8
use whm\Smoke\Rules\ValidationFailedException;
9
10
/**
11
 * This rule checks if a given json file is valid.
12
 */
13
class ValidRule extends StandardRule
14
{
15
    protected $contentTypes = ['json'];
16
17
    private $json_errors = array(
18
        JSON_ERROR_NONE => 'No Error',
19
        JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
20
        JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
21
        JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
22
        JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
23
        JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
24
    );
25
26
    public function doValidation(Response $response)
27
    {
28
        $result = json_decode($response->getBody());
29
        if ($result === null) {
30
            throw new ValidationFailedException("The given JSON data can not be validated (last error: '" . $this->json_errors[json_last_error()] . "').");
31
        }
32
    }
33
}
34