Completed
Pull Request — master (#1)
by Karsten
03:23
created

PropertyMarkedForSlumber::withMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * File was created 06.10.2015 06:34
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Core\LookUp;
7
8
use PeekAndPoke\Component\PropertyAccess\PropertyAccess;
9
use PeekAndPoke\Component\Psi\Functions\Unary\Matcher\IsInstanceOf;
10
use PeekAndPoke\Component\Psi\Psi;
11
use PeekAndPoke\Component\Slumber\Annotation\PropertyMappingMarker;
12
use PeekAndPoke\Component\Slumber\Annotation\PropertyMarker;
13
use PeekAndPoke\Component\Slumber\Core\Codec\Mapper;
14
15
/**
16
 * @author Karsten J. Gerber <[email protected]>
17
 */
18
class PropertyMarkedForSlumber
19
{
20
    /** @var string The name of the property */
21
    public $name;
22
    /** @var string The field name for the properties value in the storage. CAN BE equal to the propertyName */
23
    public $alias;
24
    /** @var PropertyMappingMarker The property marker */
25
    public $marker;
26
    /** @var PropertyMarker[] All additional markers on the property */
27
    public $allMarkers;
28
    /** @var Mapper */
29
    public $mapper;
30
    /** @var PropertyAccess Accessor for reading and writing the property */
31
    public $propertyAccess;
32
33
    /**
34
     * PropertyMarkedForSlumber constructor.
35
     *
36
     * @param string                $propertyName
37
     * @param string                $alias
38
     * @param PropertyMappingMarker $marker
39
     * @param PropertyMarker[]      $allMarkers
40
     * @param Mapper                $mapper
41
     * @param PropertyAccess        $propertyAccess
42
     *
43
     * @return PropertyMarkedForSlumber
44
     */
45
    public static function create(
46
        $propertyName,
47
        $alias,
48
        PropertyMappingMarker $marker,
49
        $allMarkers,
50
        Mapper $mapper,
51
        PropertyAccess $propertyAccess
52
    ) {
53
        $ret = new self;
54
55
        $ret->name           = $propertyName;
56
        $ret->alias          = $alias;
57
        $ret->marker         = $marker;
58
        $ret->allMarkers     = Psi::it($allMarkers)->filter(new IsInstanceOf(PropertyMarker::class))->toArray();
59
        $ret->mapper         = $mapper;
60
        $ret->propertyAccess = $propertyAccess;
61
62
        return $ret;
63
    }
64
65
    /**
66
     * @param string $alias
67
     *
68
     * @return PropertyMarkedForSlumber
69
     */
70
    public function withAlias($alias)
71
    {
72
        $clone        = clone $this;
73
        $clone->alias = $alias;
74
75
        return $clone;
76
    }
77
78
    /**
79
     * @param Mapper $mapper
80
     *
81
     * @return PropertyMarkedForSlumber
82
     */
83
    public function withMapper(Mapper $mapper)
84
    {
85
        $clone         = clone $this;
86
        $clone->mapper = $mapper;
87
88
        return $clone;
89
    }
90
91
    /**
92
     * @param string $type
93
     *
94
     * @return PropertyMarker|null
95
     */
96 1
    public function getFirstMarkerOf($type)
97
    {
98 1
        $is = new IsInstanceOf($type);
99
100 1
        foreach ($this->allMarkers as $additionalMarker) {
101 1
            if ($is($additionalMarker)) {
102 1
                return $additionalMarker;
103
            }
104
        }
105
106 1
        return null;
107
    }
108
109
    /**
110
     * @param string $type
111
     *
112
     * @return PropertyMarker[]
113
     */
114
    public function getMarkersOf($type)
115
    {
116
        return Psi::it($this->allMarkers)
117
            ->filter(new IsInstanceOf($type))
118
            ->toArray();
119
    }
120
}
121