Definition::setManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
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 = $this->getName();
93
        if (empty($name)) {
94
            return;
95
        }
96
        $name = inflector()->pluralize($name);
97
        $this->setLabel($name);
98
    }
99
100
101
    /**
102
     * @return string
103
     */
104
    public function getDefaultValue(): ?string
105
    {
106
        if ($this->defaultValue === null) {
107
            $this->initDefaultValue();
108
        }
109
110
        return $this->defaultValue;
111
    }
112
113
    /**
114
     * @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...
115
     */
116
    public function setDefaultValue($defaultValue)
117
    {
118
        $this->defaultValue = $defaultValue;
119
    }
120
121
122
    protected function initDefaultValue()
123
    {
124
        $managerDefaultValue = $this->getDefaultValueFromManager();
125
        if ($managerDefaultValue && $this->hasItem($managerDefaultValue)) {
126
            $defaultValue = $managerDefaultValue;
127
        } else {
128
            $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

128
            $keys = array_keys(/** @scrutinizer ignore-type */ $this->getItems());
Loading history...
129
            $defaultValue = reset($keys);
130
        }
131
        $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

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