PropertyRule   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 65
ccs 30
cts 30
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A rename() 0 5 1
A def() 0 5 1
A getter() 0 5 1
A setter() 0 5 1
A compile() 0 12 4
A reverseCompile() 0 11 3
1
<?php
2
namespace einfach\representer;
3
4
class PropertyRule
5
{
6
    public $object;
7
    public $name;
8
9
    protected $toName;
10
    protected $defaultValue;
11
    protected $getterCallable;
12
    protected $setterCallable;
13
14 10
    public function __construct($object, $name)
15
    {
16 10
        $this->object = $object;
17 10
        $this->name = $name;
18 10
    }
19
20 10
    public function rename($toName)
21
    {
22 10
        $this->toName = $toName;
23 10
        return $this;
24
    }
25
26 10
    public function def($defaultValue)
27
    {
28 10
        $this->defaultValue = $defaultValue;
29 10
        return $this;
30
    }
31
32 10
    public function getter($callable)
33
    {
34 10
        $this->getterCallable = $callable;
35 10
        return $this;
36
    }
37
38 10
    public function setter($callable)
39
    {
40 10
        $this->setterCallable = $callable;
41 10
        return $this;
42
    }
43
44 8
    public function compile()
45
    {
46 8
        $name = ($this->toName) ? $this->toName : $this->name;
47
48 8
        if (is_callable($this->getterCallable)) {
49 8
            $value = call_user_func($this->getterCallable, $this->object, $this->name);
50 8
        } else {
51 8
            $value = ($this->object->{$this->name}) ? $this->object->{$this->name} : $this->defaultValue;
52
        }
53
54 8
        return [$name => $value];
55
    }
56
57 5
    public function reverseCompile($projection)
58
    {
59 5
        $name = ($this->toName) ? $this->toName : $this->name;
60 5
        $value = $projection[$name];
61
62 5
        if (is_callable($this->setterCallable)) {
63 5
            $value = call_user_func($this->setterCallable, $this->object, $this->name, $value);
64 5
        }
65
66 5
        return [$this->name => $value];
67
    }
68
}
69