Passed
Push — master ( 81d3f9...4ab6f6 )
by Gabriel
11:06
created

Definition::getPropertyClass()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 1
nop 1
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Definitions;
4
5
use ByTIC\Models\SmartProperties\RecordsTraits\HasSmartProperties\RecordsTrait;
6
use Nip\Records\RecordManager;
7
8
/**
9
 * Class Definition
10
 * @package ByTIC\Models\SmartProperties\Definitions
11
 */
12
class Definition implements \Serializable
13
{
14
    use Definition\CanBuild;
15
    use Definition\HasName;
16
    use Definition\HasPlaces;
17
    use Definition\HasProperties;
18
    use Definition\HasPropertiesNamespaces;
19
    use Definition\Serializable;
20
21
    /**
22
     * @var RecordManager|RecordsTrait
23
     */
24
    protected $manager;
25
26
    /**
27
     * @var string
28
     */
29
    protected $label = null;
30
31
    /**
32
     * @var string
33
     */
34
    protected $field;
35
36
    protected $defaultValue = null;
37
38
    /**
39
     * @return string|null
40
     */
41
    public function getField(): ?string
42
    {
43
        return $this->field;
44
    }
45
46
    /**
47
     * @param mixed $field
48
     */
49
    public function setField($field)
50
    {
51
        $this->field = $field;
52
    }
53
54
    /**
55
     * @return RecordManager
56
     */
57
    public function getManager()
58
    {
59
        return $this->manager;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->manager also could return the type ByTIC\Models\SmartProper...Properties\RecordsTrait which is incompatible with the documented return type Nip\Records\RecordManager.
Loading history...
60
    }
61
62
    /**
63
     * @param RecordManager|RecordsTrait $manager
64
     */
65
    public function setManager($manager)
66
    {
67
        $this->manager = $manager;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getLabel(): ?string
74
    {
75
        if ($this->label === null) {
76
            $this->initLabel();
77
        }
78
79
        return $this->label;
80
    }
81
82
    /**
83
     * @param string $label
84
     */
85
    public function setLabel(string $label)
86
    {
87
        $this->label = $label;
88
    }
89
90
    protected function initLabel()
91
    {
92
        $name = inflector()->pluralize($this->getName());
93
        $this->setLabel($name);
94
    }
95
96
97
    /**
98
     * @return string
99
     */
100
    public function getDefaultValue(): ?string
101
    {
102
        if ($this->defaultValue === null) {
103
            $this->initDefaultValue();
104
        }
105
106
        return $this->defaultValue;
107
    }
108
109
    /**
110
     * @param null $defaultValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $defaultValue is correct as it would always require null to be passed?
Loading history...
111
     */
112
    public function setDefaultValue($defaultValue)
113
    {
114
        $this->defaultValue = $defaultValue;
115
    }
116
117
118
    protected function initDefaultValue()
119
    {
120
        $managerDefaultValue = $this->getDefaultValueFromManager();
121
        if ($managerDefaultValue && $this->hasItem($managerDefaultValue)) {
122
            $defaultValue = $managerDefaultValue;
123
        } else {
124
            $keys = array_keys($this->getItems());
0 ignored issues
show
Bug introduced by
It seems like $this->getItems() can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
            $keys = array_keys(/** @scrutinizer ignore-type */ $this->getItems());
Loading history...
125
            $defaultValue = reset($keys);
126
        }
127
        $this->setDefaultValue($defaultValue);
0 ignored issues
show
Bug introduced by
It seems like $defaultValue can also be of type string and true; however, parameter $defaultValue of ByTIC\Models\SmartProper...tion::setDefaultValue() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
        $this->setDefaultValue(/** @scrutinizer ignore-type */ $defaultValue);
Loading history...
128
    }
129
130
    /**
131
     * @return bool|string
132
     */
133
    protected function getDefaultValueFromManager()
134
    {
135
        $method = 'getDefault' . $this->getName();
136
        if (method_exists($this->getManager(), $method)) {
137
            return $this->getManager()->{$method}();
138
        }
139
140
        return false;
141
    }
142
143
144
    /**
145
     * @param $name
146
     *
147
     * @return array
148
     */
149
    public function getValues($name): array
150
    {
151
        $return = [];
152
        $items = $this->getItems();
153
154
        foreach ($items as $type) {
155
            $method = 'get' . ucfirst($name);
156
            if (method_exists($type, $method)) {
157
                $return[] = $type->$method();
158
            } else {
159
                $return[] = $type->{$name};
160
            }
161
        }
162
163
        return $return;
164
    }
165
}
166