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

Rule::getMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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