Completed
Push — develop ( 2a5993...441247 )
by Evan
02:20
created

Model::expandAlias()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.4285
1
<?php
2
3
namespace Silk\Type;
4
5
use Silk\Meta\ObjectMeta;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * @property-read int    $id
10
 * @property-read object $object
11
 */
12
abstract class Model
13
{
14
    /**
15
     * The core model object
16
     * @var object
17
     */
18
    protected $object;
19
20
    /**
21
     * Type object property aliases
22
     * @var array
23
     */
24
    protected $objectAliases = [
25
        // 'aliasName' => 'propertyNameOnObject'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
    ];
27
28
    /**
29
     * The object type in WordPress
30
     */
31
    const OBJECT_TYPE = '';
32
33
    /**
34
     * The name of the primary ID property on the object
35
     */
36
    const ID_PROPERTY = '';
37
38
    /**
39
    * Get a new query builder for the model.
40
    *
41
    * @return \Silk\Contracts\BuildsQueries
42
    */
43
    abstract public function newQuery();
44
45
    /**
46
     * Save the changes to the database.
47
     *
48
     * @return $this
49
     */
50
    abstract public function save();
51
52
    /**
53
     * Delete the modeled record from the database.
54
     *
55
     * @return $this
56
     */
57
    abstract public function delete();
58
59
    /**
60
     * Reload the object from the database.
61
     *
62
     * @return $this
63
     */
64
    abstract public function refresh();
65
66
    /**
67
     * Make new instance.
68
     *
69
     * All provided arguments are forwarded to the constructor of the called class.
70
     *
71
     * @return static
72
     */
73
    public static function make()
74
    {
75
        if ($arguments = func_get_args()) {
76
            return (new \ReflectionClass(static::class))->newInstanceArgs($arguments);
77
        }
78
79
        return new static;
80
    }
81
82
    /**
83
     * Create a new model of the model's type, and save it to the database.
84
     *
85
     * @param  array $attributes
86
     *
87
     * @return static
88
     */
89
    public static function create($attributes = [])
90
    {
91
        $model = new static;
92
93
        Collection::make($attributes)
94
                  ->each(function ($value, $key) use ($model) {
95
                      $model->$key = $value;
96
                  })
97
        ;
98
99
        return $model->save();
100
    }
101
102
    /**
103
     * Create a new query builder instance for this model type.
104
     *
105
     * @return \Silk\Contracts\BuildsQueries
106
     */
107
    public static function query()
108
    {
109
        return (new static)->newQuery();
110
    }
111
112
    /**
113
     * Meta API for this type
114
     *
115
     * @param  string $key  Meta key to retrieve or empty to retrieve all.
116
     *
117
     * @return ObjectMeta|\Silk\Meta\Meta
118
     */
119
    public function meta($key = '')
120
    {
121
        $meta = new ObjectMeta(static::OBJECT_TYPE, $this->id);
122
123
        if ($key) {
124
            return $meta->get($key);
125
        }
126
127
        return $meta;
128
    }
129
130
    /**
131
     * Update the core object
132
     *
133
     * @param object $object
134
     *
135
     * @return $this
136
     */
137
    public function setObject($object)
138
    {
139
        $this->object = $object;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Set the primary ID on the model.
146
     *
147
     * @param string|int $id  The model's ID
148
     *
149
     * @return $this
150
     */
151
    protected function setId($id)
152
    {
153
        $this->object->{static::ID_PROPERTY} = (int) $id;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Expands an alias into its respective object property name.
160
     *
161
     * @param string $key  Alias key
162
     *
163
     * @return mixed|string
164
     */
165
    protected function expandAlias($key)
166
    {
167
        if (isset($this->objectAliases[$key])) {
168
            return $this->objectAliases[$key];
169
        }
170
171
        /**
172
         * Automatically alias shorthand syntax for type_name
173
         * Eg: 'post_content' is aliased to 'content'
174
         */
175
        $expanded = static::OBJECT_TYPE . '_' . $key;
176
177
        if (property_exists($this->object, $expanded)) {
178
            return $expanded;
179
        }
180
181
        return $key;
182
    }
183
184
    /**
185
     * Magic getter.
186
     *
187
     * @param  string $property
188
     *
189
     * @return mixed
190
     */
191
    public function __get($property)
192
    {
193
        if ($property == 'id') {
194
            return $this->object->{static::ID_PROPERTY};
195
        }
196
197
        if (in_array($property, ['object', static::OBJECT_TYPE])) {
198
            return $this->object;
199
        }
200
201
        $property = $this->expandAlias($property);
202
203
        /**
204
         * Finally, hand-off the request to the wrapped object.
205
         * We don't check for existence as we leverage the magic __get
206
         * on the wrapped object as well.
207
         */
208
        return $this->object->$property;
209
    }
210
211
    /**
212
     * Magic Isset Checker.
213
     *
214
     * @return bool
215
     */
216
    public function __isset($property)
217
    {
218
        return ! is_null($this->__get($property));
219
    }
220
221
    /**
222
     * Magic setter.
223
     *
224
     * @param string $property  The property name
225
     * @param mixed  $value     The new property value
226
     */
227
    public function __set($property, $value)
228
    {
229
        $property = $this->expandAlias($property);
230
231
        $this->object->$property = $value;
232
    }
233
234
    /**
235
     * Handle dynamic method calls into the model.
236
     *
237
     * @param  string $method
238
     * @param  array $arguments
239
     *
240
     * @return mixed
241
     */
242
    public function __call($method, $arguments)
243
    {
244
        $query = $this->newQuery();
245
246
        return call_user_func_array([$query, $method], $arguments);
247
    }
248
249
    /**
250
     * Handle dynamic static method calls on the model class.
251
     *
252
     * Proxies calls to direct method calls on a new instance
253
     *
254
     * @param string $method
255
     * @param array $arguments
256
     *
257
     * @return mixed
258
     */
259
    public static function __callStatic($method, array $arguments)
260
    {
261
        return call_user_func_array([new static, $method], $arguments);
262
    }
263
}
264