Completed
Push — master ( 1bf18c...56d3b9 )
by Jared
02:30
created

Property::isPersisted()   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 array|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 bool */
45
    private $in_array = true;
46
47
    /** @var string|null */
48
    private $relation;
49
50
    /** @var string|null */
51
    private $relation_type;
52
53
    /** @var string|null */
54
    private $foreign_key;
55
56
    /** @var string|null */
57
    private $local_key;
58
59
    /** @var string|null */
60
    private $pivot_tablename;
61
62
    /** @var array|null */
63
    private $morphs_to;
64
65
    public function __construct(array $values = [], string $name = '')
66
    {
67
        $this->name = $name;
68
        foreach ($values as $k => $v) {
69
            $this->$k = $v;
70
        }
71
        $this->hasDefault = array_key_exists('default', $values);
72
    }
73
74
    public function getName(): string
75
    {
76
        return $this->name;
77
    }
78
79
    /**
80
     * Gets the humanized name of this property.
81
     */
82
    public function getTitle(Model $model): string
83
    {
84
        // look up the property from the translator first
85
        if ($translator = $model->getErrors()->getTranslator()) {
86
            $k = 'pulsar.properties.'.$model::modelName().'.'.$this->name;
87
            $title = $translator->translate($k);
88
            if ($title != $k) {
89
                return $title;
90
            }
91
        }
92
93
        // otherwise just attempt to title-ize the property name
94
        return Inflector::get()->titleize($this->name);
95
    }
96
97
    public function getType(): ?string
98
    {
99
        return $this->type;
100
    }
101
102
    public function isMutable(): bool
103
    {
104
        return self::MUTABLE == $this->mutable;
105
    }
106
107
    public function isMutableCreateOnly(): bool
108
    {
109
        return self::MUTABLE_CREATE_ONLY == $this->mutable;
110
    }
111
112
    public function isImmutable(): bool
113
    {
114
        return self::IMMUTABLE == $this->mutable;
115
    }
116
117
    public function isNullable(): bool
118
    {
119
        return $this->null;
120
    }
121
122
    public function isRequired(): bool
123
    {
124
        return $this->required;
125
    }
126
127
    /**
128
     * @return array|string|null
129
     */
130
    public function getValidationRules()
131
    {
132
        return $this->validate;
133
    }
134
135
    /**
136
     * @return mixed
137
     */
138
    public function getDefault()
139
    {
140
        return $this->default;
141
    }
142
143
    public function hasDefault(): bool
144
    {
145
        return $this->hasDefault;
146
    }
147
148
    public function isPersisted(): bool
149
    {
150
        return $this->persisted;
151
    }
152
153
    public function isEncrypted(): bool
154
    {
155
        return $this->encrypted;
156
    }
157
158
    public function isInArray(): bool
159
    {
160
        return $this->in_array;
161
    }
162
163
    public function getForeignModelClass(): ?string
164
    {
165
        return $this->relation;
166
    }
167
168
    public function getRelationshipType(): ?string
169
    {
170
        return $this->relation_type;
171
    }
172
173
    public function getForeignKey(): ?string
174
    {
175
        return $this->foreign_key;
176
    }
177
178
    public function getLocalKey(): ?string
179
    {
180
        return $this->local_key;
181
    }
182
183
    public function getPivotTablename(): ?string
184
    {
185
        return $this->pivot_tablename;
186
    }
187
188
    public function getMorphsTo(): ?array
189
    {
190
        return $this->morphs_to;
191
    }
192
193
    public function toArray(): array
194
    {
195
        return [
196
            'type' => $this->type,
197
            'mutable' => $this->mutable,
198
            'null' => $this->null,
199
            'required' => $this->required,
200
            'validate' => $this->validate,
201
            'default' => $this->default,
202
            'persisted' => $this->persisted,
203
            'encrypted' => $this->encrypted,
204
            'in_array' => $this->in_array,
205
            'relation' => $this->relation,
206
            'relation_type' => $this->relation_type,
207
            'foreign_key' => $this->foreign_key,
208
            'local_key' => $this->local_key,
209
            'pivot_tablename' => $this->pivot_tablename,
210
            'morphs_to' => $this->morphs_to,
211
        ];
212
    }
213
214
    public function offsetExists($offset)
215
    {
216
        return property_exists($this, $offset) && $this->$offset !== null;
217
    }
218
219
    public function offsetGet($offset)
220
    {
221
        if (!property_exists($this, $offset)) {
222
            return null;
223
        }
224
225
        return $this->$offset;
226
    }
227
228
    public function offsetSet($offset, $value)
229
    {
230
        throw new \RuntimeException('Modifying a model property is not allowed.');
231
    }
232
233
    public function offsetUnset($offset)
234
    {
235
        throw new \RuntimeException('Modifying a model property is not allowed.');
236
    }
237
}
238