Completed
Push — master ( 3b4efc...fefeeb )
by Andres
02:22 queued 02:19
created

JsonValidator::items()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 1
dl 8
loc 8
rs 9.4285
1
<?php
2
namespace Comfort\Validator;
3
4
use Comfort\Comfort;
5
6
/**
7
 * Class JsonValidator
8
 * @package Comfort\Validator
9
 * @method $this array()
10
 */
11
class JsonValidator extends AbstractValidator
12
{
13
    public function __construct(Comfort $comfort)
14
    {
15
        parent::__construct($comfort);
16
17
        $this->toBool(false);
18
19
        $this->add(function($value, $nameKey) {
20
            json_decode($value);
21
            if (json_last_error() != JSON_ERROR_NONE) {
22
                return $this->createError('json.invalid', $value, $nameKey);
23
            }
24
        });
25
    }
26
27
    /**
28
     * Apply rules to a given schema
29
     *
30
     * @param array $definition
31
     * @return $this
32
     */
33 View Code Duplication
    public function keys(array $definition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        return $this->add(function($value, $nameKey) use($definition) {
0 ignored issues
show
Unused Code introduced by
The parameter $nameKey is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
            $decodedValue = json_decode($value, true);
37
            $validation = $this->array()->keys($definition);
38
            return $validation($decodedValue);
39
        });
40
    }
41
42
    /**
43
     * Apply a definition to items in a json structure
44
     *
45
     * @param AbstractValidator $definition
46
     * @return $this
47
     */
48 View Code Duplication
    public function items(AbstractValidator $definition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        return $this->add(function($value, $nameKey) use($definition) {
0 ignored issues
show
Unused Code introduced by
The parameter $nameKey is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
            $decodedValue = json_decode($value, true);
52
            $validation = $this->array()->items($definition);
53
            return $validation($decodedValue);
54
        });
55
    }
56
}