Completed
Push — develop ( 98c4f4...7da5a9 )
by Evan
02:19
created

Model::setObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
cc 1
eloc 3
nc 1
nop 1
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
     * Set the primary ID on the model.
132
     *
133
     * @param string|int $id  The model's ID
134
     *
135
     * @return $this
136
     */
137
    protected function setId($id)
138
    {
139
        $this->object->{static::ID_PROPERTY} = (int) $id;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Expands an alias into its respective object property name.
146
     *
147
     * @param string $key  Alias key
148
     *
149
     * @return mixed|string
150
     */
151
    protected function expandAlias($key)
152
    {
153
        if (isset($this->objectAliases[$key])) {
154
            return $this->objectAliases[$key];
155
        }
156
157
        /**
158
         * Automatically alias shorthand syntax for type_name
159
         * Eg: 'post_content' is aliased to 'content'
160
         */
161
        $expanded = static::OBJECT_TYPE . '_' . $key;
162
163
        if (property_exists($this->object, $expanded)) {
164
            return $expanded;
165
        }
166
167
        return $key;
168
    }
169
170
    /**
171
     * Magic getter.
172
     *
173
     * @param  string $property
174
     *
175
     * @return mixed
176
     */
177
    public function __get($property)
178
    {
179
        if ($property == 'id') {
180
            return $this->object->{static::ID_PROPERTY};
181
        }
182
183
        if (in_array($property, ['object', static::OBJECT_TYPE])) {
184
            return $this->object;
185
        }
186
187
        $property = $this->expandAlias($property);
188
189
        /**
190
         * Finally, hand-off the request to the wrapped object.
191
         * We don't check for existence as we leverage the magic __get
192
         * on the wrapped object as well.
193
         */
194
        return $this->object->$property;
195
    }
196
197
    /**
198
     * Magic Isset Checker.
199
     *
200
     * @return bool
201
     */
202
    public function __isset($property)
203
    {
204
        return ! is_null($this->__get($property));
205
    }
206
207
    /**
208
     * Magic setter.
209
     *
210
     * @param string $property  The property name
211
     * @param mixed  $value     The new property value
212
     */
213
    public function __set($property, $value)
214
    {
215
        $property = $this->expandAlias($property);
216
217
        $this->object->$property = $value;
218
    }
219
220
    /**
221
     * Handle dynamic method calls into the model.
222
     *
223
     * @param  string $method
224
     * @param  array $arguments
225
     *
226
     * @return mixed
227
     */
228
    public function __call($method, $arguments)
229
    {
230
        $query = $this->newQuery();
231
232
        return call_user_func_array([$query, $method], $arguments);
233
    }
234
235
    /**
236
     * Handle dynamic static method calls on the model class.
237
     *
238
     * Proxies calls to direct method calls on a new instance
239
     *
240
     * @param string $method
241
     * @param array $arguments
242
     *
243
     * @return mixed
244
     */
245
    public static function __callStatic($method, array $arguments)
246
    {
247
        return call_user_func_array([new static, $method], $arguments);
248
    }
249
}
250