Completed
Push — master ( 98e719...571bb7 )
by Dawid
06:37
created

PropertyMetaData::setFieldName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Igni\Storage\Mapping\MetaData;
4
5
use Igni\Storage\Mapping\Strategy\Text;
6
use Closure;
7
8
final class PropertyMetaData
9
{
10
    private $attributes = [];
11
    private $propertyName;
12
    private $fieldName;
13
    private $type;
14
    private $accessor;
15
    private $writer;
16
17 22
    public function __construct(string $propertyName, string $type = Text::class)
18
    {
19 22
        $this->propertyName = $propertyName;
20 22
        $this->type = $type;
21
        $this->accessor = function () use ($propertyName) {
22 1
            return $this->$propertyName;
23
        };
24
        $this->writer = function ($value) use ($propertyName)  {
25 1
            $this->$propertyName = $value;
26 1
        };
27 22
    }
28
29 20
    public function getType(): string
30
    {
31 20
        return $this->type;
32
    }
33
34 20
    public function getName(): string
35
    {
36 20
        return $this->propertyName;
37
    }
38
39 19
    public function setAttributes(array $attributes): void
40
    {
41 19
        $this->attributes = $attributes;
42 19
    }
43
44 4
    public function getAttributes(): array
45
    {
46 4
        return $this->attributes;
47
    }
48
49
    public function hasAttribute(string $name): bool
50
    {
51
        return isset($this->attributes[$name]);
52
    }
53
54 17
    public function setFieldName(string $name): void
55
    {
56 17
        $this->fieldName = $name;
57 17
    }
58
59 12
    public function getFieldName(): string
60
    {
61 12
        return $this->fieldName ?? $this->propertyName;
62
    }
63
64 2
    public function setValue($object, $value): void
65
    {
66 2
        Closure::bind($this->writer, $object, $object)($value);
67 2
    }
68
69 2
    public function getValue($object)
70
    {
71 2
        return Closure::bind($this->accessor, $object, $object)();
72
    }
73
74 2
    public function __sleep()
75
    {
76
        return [
77 2
            'attributes',
78
            'propertyName',
79
            'fieldName',
80
            'type',
81
        ];
82
    }
83
84 2
    public function __wakeup()
85
    {
86 2
        $propertyName = $this->propertyName;
87
88
        $this->accessor = function () use ($propertyName) {
89 1
            return $this->$propertyName;
90
        };
91
        $this->writer = function ($value) use ($propertyName)  {
92 1
            $this->$propertyName = $value;
93 1
        };
94 2
    }
95
}
96