Predicate   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 126
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getArrayOperators() 0 3 1
A setOperator() 0 8 2
A setFields() 0 4 1
A createFromContract() 0 4 1
A getOperator() 0 4 1
A jsonSerializeFields() 0 9 2
A getAllowedOperators() 0 8 1
A jsonSerialize() 0 6 1
A normalizeFields() 0 8 3
1
<?php
2
3
namespace Meare\Juggler\Imposter\Stub\Predicate;
4
5
class Predicate implements IPredicate
6
{
7
    /**
8
     * @var array
9
     */
10
    private $fields = [];
11
12
    /**
13
     * @var string
14
     */
15
    private $operator;
16
17
    /**
18
     * @param string $operator
19
     * @param array  $fields
20
     */
21 13
    public function __construct($operator, array $fields)
22
    {
23 13
        $this->setOperator($operator);
24 11
        $this->setFields($fields);
25 11
    }
26
27
    /**
28
     * Returns list of operators that must be defined as array, e.g. 'or' operator:
29
     * "or": [
30
     *     { "startsWith": { "data": "start" } },
31
     *     ...
32
     * ]
33
     *
34
     * Other predicates must be defined as object:
35
     * "not": {
36
     *     "equals": {
37
     *          ...
38
     *     }
39
     *  }
40
     */
41 6
    public static function getArrayOperators() {
42 6
        return [self::OPERATOR_AND, self::OPERATOR_OR];
43
    }
44
45
    /**
46
     * @return array
47
     */
48 13
    public static function getAllowedOperators()
49
    {
50
        return [
51 13
            self::OPERATOR_EQUALS, self::OPERATOR_DEEP_EQUALS, self::OPERATOR_CONTAINS, self::OPERATOR_STARTS_WITH,
52 13
            self::OPERATOR_ENDS_WITH, self::OPERATOR_MATCHES, self::OPERATOR_EXISTS, self::OPERATOR_NOT,
53 13
            self::OPERATOR_OR, self::OPERATOR_AND, self::OPERATOR_INJECT,
54 13
        ];
55
    }
56
57
    /**
58
     * @param string $operator
59
     */
60 13
    private function setOperator($operator)
61
    {
62 13
        if (!in_array($operator, self::getAllowedOperators())) {
63 2
            throw new \InvalidArgumentException("Cannot create predicate object; Invalid operator: '$operator'");
64
        }
65
66 11
        $this->operator = $operator;
67 11
    }
68
69
    /**
70
     * @param array $fields
71
     */
72 11
    public function setFields(array $fields)
73
    {
74 11
        $this->fields = $fields;
75 11
    }
76
77
    /**
78
     * @param array|string $contract
79
     * @return Predicate
80
     */
81 1
    public static function createFromContract($contract)
82
    {
83 1
        return new self(key($contract), reset($contract));
84
    }
85
86
    /**
87
     * @return string
88
     */
89 1
    public function getOperator()
90
    {
91 1
        return $this->operator;
92
    }
93
94
    /**
95
     * @return array
96
     */
97 6
    public function jsonSerialize()
98
    {
99
        return [
100 6
            $this->operator => $this->jsonSerializeFields(),
101 6
        ];
102
    }
103
104
    /**
105
     * @return array|\StdClass
106
     */
107 6
    private function jsonSerializeFields()
108
    {
109 6
        $this->normalizeFields();
110 6
        if (in_array($this->operator, self::getArrayOperators())) {
111 2
            return $this->fields;
112
        } else {
113 4
            return \Meare\Juggler\json_object($this->fields);
114
        }
115
    }
116
117
    /**
118
     * There is no arrays in predicate fields - all empty fields must serialize as {}
119
     *
120
     * @return array
121
     */
122 6
    private function normalizeFields()
123
    {
124 6
        foreach ($this->fields as $key => $field) {
125 4
            if (is_array($field)) {
126 1
                $this->fields[$key] = \Meare\Juggler\json_object($field);
127 1
            }
128 6
        }
129
    }
130
}