Completed
Push — master ( 0d4fd4...557d64 )
by Denis
02:02
created

Rule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 105
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A createDefault() 0 14 2
A assign() 0 7 1
A getClass() 0 4 1
A getMap() 0 4 1
A getReflectedMap() 0 4 1
1
<?php
2
3
namespace PhpJsonRpc\Common\TypeAdapter;
4
5
class Rule
6
{
7
    /**
8
     * @var string
9
     */
10
    private $class;
11
12
    /**
13
     * Property => Key
14
     *
15
     * @var array
16
     */
17
    private $map = [];
18
19
    /**
20
     * Key => Property
21
     *
22
     * @var array
23
     */
24
    private $reflectedMap = [];
25
26
    /**
27
     * Rule constructor.
28
     *
29
     * @param string $class
30
     */
31
    public function __construct($class)
32
    {
33
        $this->class = $class;
34
    }
35
36
    /**
37
     * Create new rule
38
     *
39
     * @param string $class
40
     *
41
     * @return Rule
42
     */
43
    public static function create(string $class): Rule
44
    {
45
        return new Rule($class);
46
    }
47
48
    /**
49
     * Create rule with one-to-one mapping
50
     *
51
     * @param string $class
52
     *
53
     * @return Rule
54
     */
55
    public static function createDefault(string $class): Rule
56
    {
57
        $rule = new Rule($class);
58
59
        $reflection = new \ReflectionClass($class);
60
        $properties = $reflection->getProperties();
61
62
        foreach ($properties as $property) {
63
            /** @var \ReflectionProperty $property */
64
            $rule->assign($property->getName(), $property->getName());
65
        }
66
67
        return $rule;
68
    }
69
70
    /**
71
     * Add property-key pair
72
     *
73
     * @param string $property
74
     * @param string $key
75
     *
76
     * @return $this
77
     */
78
    public function assign(string $property, string $key)
79
    {
80
        $this->map[$property] = $key;
81
        $this->reflectedMap[$key] = $property;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getClass(): string
90
    {
91
        return $this->class;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getMap(): array
98
    {
99
        return $this->map;
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function getReflectedMap(): array
106
    {
107
        return $this->reflectedMap;
108
    }
109
}
110