Completed
Push — master ( 3e728c...994056 )
by Jared
01:32
created

Property::getMorphsTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Pulsar;
4
5
use ArrayAccess;
6
use ICanBoogie\Inflector;
7
8
final class Property implements ArrayAccess
9
{
10
    const IMMUTABLE = 'immutable';
11
    const MUTABLE_CREATE_ONLY = 'mutable_create_only';
12
    const MUTABLE = 'mutable';
13
14
    /** @var string */
15
    private $name;
16
17
    /** @var string|null */
18
    private $type = null;
19
20
    /** @var string */
21
    private $mutable = self::MUTABLE;
22
23
    /** @var bool */
24
    private $null = false;
25
26
    /** @var bool */
27
    private $required = false;
28
29
    /** @var callable|string|null */
30
    private $validate = null;
31
32
    /** @var mixed|null */
33
    private $default = null;
34
35
    /** @var bool */
36
    private $hasDefault;
37
38
    /** @var bool */
39
    private $encrypted = false;
40
41
    /** @var bool */
42
    private $persisted = true;
43
44
    /** @var string|null */
45
    private $relation;
46
47
    /** @var string|null */
48
    private $relation_type;
49
50
    /** @var string|null */
51
    private $foreign_key;
52
53
    /** @var string|null */
54
    private $local_key;
55
56
    /** @var string|null */
57
    private $pivot_tablename;
58
59
    /** @var array|null */
60
    private $morphs_to;
61
62
    public function __construct(array $values = [], string $name = '')
63
    {
64
        $this->name = $name;
65
        foreach ($values as $k => $v) {
66
            $this->$k = $v;
67
        }
68
        $this->hasDefault = array_key_exists('default', $values);
69
    }
70
71
    public function getName(): string
72
    {
73
        return $this->name;
74
    }
75
76
    /**
77
     * Gets the humanized name of this property.
78
     */
79
    public function getTitle(Model $model): string
80
    {
81
        // look up the property from the translator first
82
        if ($translator = $model->getErrors()->getTranslator()) {
83
            $k = 'pulsar.properties.'.$model::modelName().'.'.$this->name;
84
            $title = $translator->translate($k);
85
            if ($title != $k) {
86
                return $title;
87
            }
88
        }
89
90
        // otherwise just attempt to title-ize the property name
91
        return Inflector::get()->titleize($this->name);
92
    }
93
94
    public function getType(): ?string
95
    {
96
        return $this->type;
97
    }
98
99
    public function isMutable(): bool
100
    {
101
        return self::MUTABLE == $this->mutable;
102
    }
103
104
    public function isMutableCreateOnly(): bool
105
    {
106
        return self::MUTABLE_CREATE_ONLY == $this->mutable;
107
    }
108
109
    public function isImmutable(): bool
110
    {
111
        return self::IMMUTABLE == $this->mutable;
112
    }
113
114
    public function isNullable(): bool
115
    {
116
        return $this->null;
117
    }
118
119
    public function isRequired(): bool
120
    {
121
        return $this->required;
122
    }
123
124
    /**
125
     * @return callable|string|null
126
     */
127
    public function getValidationRules()
128
    {
129
        return $this->validate;
130
    }
131
132
    /**
133
     * @return mixed
134
     */
135
    public function getDefault()
136
    {
137
        return $this->default;
138
    }
139
140
    public function hasDefault(): bool
141
    {
142
        return $this->hasDefault;
143
    }
144
145
    public function isPersisted(): bool
146
    {
147
        return $this->persisted;
148
    }
149
150
    public function isEncrypted(): bool
151
    {
152
        return $this->encrypted;
153
    }
154
155
    public function getForeignModelClass(): ?string
156
    {
157
        return $this->relation;
158
    }
159
160
    public function getRelationshipType(): ?string
161
    {
162
        return $this->relation_type;
163
    }
164
165
    public function getForeignKey(): ?string
166
    {
167
        return $this->foreign_key;
168
    }
169
170
    public function getLocalKey(): ?string
171
    {
172
        return $this->local_key;
173
    }
174
175
    public function getPivotTablename(): ?string
176
    {
177
        return $this->pivot_tablename;
178
    }
179
180
    public function getMorphsTo(): ?array
181
    {
182
        return $this->morphs_to;
183
    }
184
185
    public function toArray(): array
186
    {
187
        return [
188
            'type' => $this->type,
189
            'mutable' => $this->mutable,
190
            'null' => $this->null,
191
            'required' => $this->required,
192
            'validate' => $this->validate,
193
            'default' => $this->default,
194
            'persisted' => $this->persisted,
195
            'encrypted' => $this->encrypted,
196
            'relation' => $this->relation,
197
            'relation_type' => $this->relation_type,
198
            'foreign_key' => $this->foreign_key,
199
            'local_key' => $this->local_key,
200
            'pivot_tablename' => $this->pivot_tablename,
201
            'morphs_to' => $this->morphs_to,
202
        ];
203
    }
204
205
    public function offsetExists($offset)
206
    {
207
        return property_exists($this, $offset) && $this->$offset !== null;
208
    }
209
210
    public function offsetGet($offset)
211
    {
212
        if (!property_exists($this, $offset)) {
213
            return null;
214
        }
215
216
        return $this->$offset;
217
    }
218
219
    public function offsetSet($offset, $value)
220
    {
221
        throw new \RuntimeException('Modifying a model property is not allowed.');
222
    }
223
224
    public function offsetUnset($offset)
225
    {
226
        throw new \RuntimeException('Modifying a model property is not allowed.');
227
    }
228
}
229