Completed
Push — master ( 75aba3...58dc52 )
by Jared
01:34
created

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