Completed
Branch develop (73bc23)
by Evan
02:37
created

Model::actionClasses()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
namespace Silk\Type;
4
5
use Silk\Meta\ObjectMeta;
6
use Silk\Database\NullAction;
7
use Silk\Contracts\Executable;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * @property-read int    $id
12
 * @property-read object $object
13
 */
14
abstract class Model
15
{
16
    /**
17
     * The core model object
18
     * @var object
19
     */
20
    protected $object;
21
22
    /**
23
     * The object type in WordPress
24
     */
25
    const OBJECT_TYPE = '';
26
27
    /**
28
     * The name of the primary ID property on the object
29
     */
30
    const ID_PROPERTY = '';
31
32
    /**
33
     * Get a new query builder for the model.
34
     *
35
     * @return BuildsQueries
36
     */
37
    abstract public function newQuery();
38
39
    /**
40
     * Get the map of action => class for resolving active actions.
41
     *
42
     * @return array
43
     */
44
    abstract protected function actionClasses();
45
46
    /**
47
     * Create a new query builder instance for this model type.
48
     *
49
     * @return BuildsQueries
50
     */
51
    public static function query()
52
    {
53
        return (new static)->newQuery();
54
    }
55
56
    /**
57
     * Save the changes to the database.
58
     *
59
     * @return $this
60
     */
61
    public function save()
62
    {
63
        $this->activeAction('save');
64
65
        return $this;
66
    }
67
68
    /**
69
     * Delete the record from the database.
70
     *
71
     * @return $this
72
     */
73
    public function delete()
74
    {
75
        $this->activeAction('delete');
76
77
        return $this;
78
    }
79
80
    /**
81
     * Load and set the object from the database.
82
     *
83
     * @return $this
84
     */
85
    public function refresh()
86
    {
87
        $this->activeAction('load');
88
89
        return $this;
90
    }
91
92
    /**
93
     * Meta API for this type
94
     *
95
     * @param  string $key  Meta key to retreive or empty to retreive all.
96
     *
97
     * @return object
98
     */
99
    public function meta($key = '')
100
    {
101
        $meta = new ObjectMeta(static::OBJECT_TYPE, $this->id);
102
103
        if ($key) {
104
            return $meta->get($key);
105
        }
106
107
        return $meta;
108
    }
109
110
    /**
111
     * Update the core object
112
     *
113
     * @param object $object
114
     *
115
     * @return $this
116
     */
117
    public function setObject($object)
118
    {
119
        $this->object = $object;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Set the primary ID on the model.
126
     *
127
     * @param string|int $id  The model's ID
128
     *
129
     * @return $this
130
     */
131
    public function setId($id)
132
    {
133
        $this->object->{static::ID_PROPERTY} = (int) $id;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Perform a database action.
140
     *
141
     * @return void
142
     */
143
    protected function activeAction($action)
144
    {
145
        $actionClass = Collection::make(
146
            $this->actionClasses()
147
        )->get($action, NullAction::class);
148
149
        $this->executeAction(new $actionClass($this));
150
    }
151
152
    /**
153
     * Execute the active action
154
     *
155
     * @return void
156
     */
157
    protected function executeAction(Executable $action)
158
    {
159
        $action->execute();
160
    }
161
162
    /**
163
     * Magic getter.
164
     *
165
     * @param  string $property
166
     *
167
     * @return mixed
168
     */
169
    public function __get($property)
170
    {
171
        if ($property == 'id') {
172
            return $this->object->{static::ID_PROPERTY};
173
        }
174
175
        if ($property == static::OBJECT_TYPE) {
176
            return $this->object;
177
        }
178
179
        return $this->object->$property;
180
    }
181
182
    /**
183
     * Magic Isset Checker.
184
     *
185
     * @return bool
186
     */
187
    public function __isset($property)
188
    {
189
        return ! is_null($this->__get($property));
190
    }
191
192
    /**
193
     * Magic setter.
194
     *
195
     * @param string $property  The property name
196
     * @param mixed  $value     The new property value
197
     */
198
    public function __set($property, $value)
199
    {
200
        if (property_exists($this->object, $property)) {
201
            $this->object->$property = $value;
202
        }
203
    }
204
205
    /**
206
     * Handle dynamic method calls into the model.
207
     *
208
     * @param  string $method
209
     * @param  array $arguments
210
     *
211
     * @return mixed
212
     */
213
    public function __call($method, $arguments)
214
    {
215
        $query = $this->newQuery();
216
217
        return call_user_func_array([$query, $method], $arguments);
218
    }
219
220
    /**
221
     * Handle dynamic static method calls on the model class.
222
     *
223
     * Proxies calls to direct method calls on a new instance
224
     *
225
     * @param string $method
226
     * @param array $arguments
227
     *
228
     * @return mixed
229
     */
230
    public static function __callStatic($method, array $arguments)
231
    {
232
        return call_user_func_array([new static, $method], $arguments);
233
    }
234
}
235