IsJsonString::validate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 4
rs 10
1
<?php
2
3
namespace kalanis\kw_rules\Rules;
4
5
6
use kalanis\kw_rules\Interfaces\IValidate;
7
use kalanis\kw_rules\Exceptions\RuleException;
8
9
10
/**
11
 * Class IsJsonString
12
 * @package kalanis\kw_rules\Rules
13
 * Check if input is JSON string
14
 */
15
class IsJsonString extends ARule
16
{
17 7
    public function validate(IValidate $entry): void
18
    {
19 7
        if (!(is_string($entry->getValue()) || is_numeric($entry->getValue()))) {
20 2
            throw new RuleException($this->errorText);
21
        }
22 5
        json_decode(strval($entry->getValue()));
23 5
        if (JSON_ERROR_NONE !== json_last_error()) {
24 2
            throw new RuleException($this->errorText, 0, new RuleException(json_last_error_msg()));
25
        }
26 3
    }
27
}
28