Completed
Push — master ( 5e51f5...c9193b )
by Marco
13:02
created

ChildishModel   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 388
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 5
dl 0
loc 388
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A clearBootedModels() 0 4 1
A newInstance() 0 12 1
A onWriteConnection() 0 6 1
A all() 0 6 2
A setUpdatedAt() 0 6 1
A setCreatedAt() 0 6 1
A query() 0 4 1
A newQuery() 0 5 1
A newBaseQueryBuilder() 0 4 1
A newCollection() 0 4 1
A getConnection() 0 4 1
A getConnectionName() 0 4 1
A setConnection() 0 6 1
A resolveConnection() 0 4 1
A getConnectionResolver() 0 4 1
A setConnectionResolver() 0 4 1
A unsetConnectionResolver() 0 4 1
A getTable() 0 8 2
A setTable() 0 6 1
A getCreatedAtColumn() 0 4 1
A getUpdatedAtColumn() 0 4 1
A freshTimestampString() 0 7 2
A newFromBuilder() 0 10 2
1
<?php
2
namespace Childish;
3
4
use Childish\database\DatabaseManager;
5
use Childish\database\ModelManager;
6
use Childish\support\Collection;
7
use Childish\support\Tools;
8
9
/**
10
 * ChildishModel
11
 *
12
 * @author    Pu ShaoWei <[email protected]>
13
 * @date      2017/12/7
14
 * @version   1.0
15
 */
16
abstract class ChildishModel
17
{
18
    /**
19
     * Indicates if the model should be timestamped.
20
     *
21
     * @var bool
22
     */
23
    public $timestamps = true;
24
25
    /**
26
     * The connection name for the model.
27
     *
28
     * @var string
29
     */
30
    protected $connection;
31
32
    /**
33
     * The table associated with the model.
34
     *
35
     * @var string
36
     */
37
    protected $table;
38
39
    /**
40
     * The primary key for the model.
41
     *
42
     * @var string
43
     */
44
    protected $primaryKey = 'id';
45
46
    /**
47
     * The "type" of the auto-incrementing ID.
48
     *
49
     * @var string
50
     */
51
    protected $keyType = 'int';
52
53
    /**
54
     * Indicates if the IDs are auto-incrementing.
55
     *
56
     * @var bool
57
     */
58
    public $incrementing = true;
59
60
    /**
61
     * Indicates if the model exists.
62
     *
63
     * @var bool
64
     */
65
    public $exists = false;
66
67
    /**
68
     * The model attribute's original state.
69
     *
70
     * @var array
71
     */
72
    protected $original = [];
73
74
    /**
75
     * Indicates if the model was inserted during the current request lifecycle.
76
     *
77
     * @var bool
78
     */
79
    public $wasRecentlyCreated = false;
80
81
    /**
82
     * @var  \Childish\database\DatabaseManager
83
     */
84
    protected static $resolver;
85
86
    /**
87
     * The array of booted models.
88
     *
89
     * @var array
90
     */
91
    protected static $booted = [];
92
93
    /**
94
     * The model's attributes.
95
     *
96
     * @var array
97
     */
98
    protected $attributes = [];
99
100
    /**
101
     * @var string 是否被删除
102
     */
103
    const DELETED_AT = null;
104
105
    /**
106
     * Custom update
107
     *
108
     * @var string
109
     */
110
    const DATE_FORMAT = 'Y-m-d H:i:s';
111
112
    /**
113
     * The name of the "created at" column.
114
     *
115
     * @var string
116
     */
117
    const CREATED_AT = 'created_at';
118
119
    /**
120
     * The name of the "updated at" column.
121
     *
122
     * @var string
123
     */
124
    const UPDATED_AT = 'updated_at';
125
126
127
    /**
128
     * Clear the list of booted models so they will be re-booted.
129
     *
130
     * @return void
131
     */
132
    public static function clearBootedModels()
133
    {
134
        static::$booted = [];
135
    }
136
137
    /**
138
     * Create a new instance of the given model.
139
     *
140
     * @param  array $attributes
141
     * @param  bool  $exists
142
     * @return static
143
     */
144
    public function newInstance($attributes = [], $exists = false)
145
    {
146
        $model = new static((array)$attributes);
0 ignored issues
show
Unused Code introduced by
The call to ChildishModel::__construct() has too many arguments starting with (array) $attributes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
147
148
        $model->exists = $exists;
149
150
        $model->setConnection(
151
            $this->getConnectionName()
152
        );
153
154
        return $model;
155
    }
156
157
    /**
158
     * Begin querying the model on the write connection.
159
     *
160
     * @return \Childish\query\Builder
161
     */
162
    public static function onWriteConnection()
163
    {
164
        $instance = new static;
165
166
        return $instance->newQuery()->useWritePdo();
0 ignored issues
show
Documentation Bug introduced by
The method useWritePdo does not exist on object<Childish\database\ModelManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
167
    }
168
169
    /**
170
     * Get all of the models from the database.
171
     *
172
     * @param  array|mixed $columns
173
     * @return \Childish\support\Collection|static[]
174
     */
175
    public static function all($columns = ['*'])
176
    {
177
        return self::query()->get(
178
            is_array($columns) ? $columns : func_get_args()
179
        );
180
    }
181
182
    /**
183
     * Set the value of the "updated at" attribute.
184
     *
185
     * @param  mixed $value
186
     * @return $this
187
     */
188
    public function setUpdatedAt($value)
189
    {
190
        $this->{static::UPDATED_AT} = $value;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Set the value of the "created at" attribute.
197
     *
198
     * @param  mixed $value
199
     * @return $this
200
     */
201
    public function setCreatedAt($value)
202
    {
203
        $this->{static::CREATED_AT} = $value;
204
205
        return $this;
206
    }
207
208
209
    /**
210
     * Begin querying the model.
211
     *
212
     * @return \Childish\query\Builder
213
     */
214
    public static function query()
215
    {
216
        return (new static)->newQuery();
217
    }
218
219
    /**
220
     * Get a new query builder for the model's table.
221
     *
222
     * @return \Childish\database\ModelManager
223
     */
224
    public function newQuery()
225
    {
226
        $builder = new ModelManager($this->newBaseQueryBuilder());
227
        return $builder->setModel($this);
228
    }
229
230
    /**
231
     * Get a new query builder instance for the connection.
232
     *
233
     * @return \Childish\query\Builder
234
     */
235
    protected function newBaseQueryBuilder()
236
    {
237
        return $this->getConnection()->table($this->getTable());
238
    }
239
240
    /**
241
     * Create a new Eloquent Collection instance.
242
     *
243
     * @param  array $models
244
     * @return \Childish\support\Collection
245
     */
246
    public function newCollection(array $models = [])
247
    {
248
        return new Collection($models);
249
    }
250
251
    /**
252
     * Get the database connection for the model.
253
     *
254
     * @return \Childish\connection\Connection
255
     */
256
    public function getConnection()
257
    {
258
        return static::resolveConnection($this->getConnectionName());
259
    }
260
261
    /**
262
     * Get the current connection name for the model.
263
     *
264
     * @return string
265
     */
266
    public function getConnectionName()
267
    {
268
        return $this->connection;
269
    }
270
271
    /**
272
     * Set the connection associated with the model.
273
     *
274
     * @param  string $name
275
     * @return $this
276
     */
277
    public function setConnection($name)
278
    {
279
        $this->connection = $name;
280
281
        return $this;
282
    }
283
284
    /**
285
     * Resolve a connection instance.
286
     *
287
     * @param  string|null $connection
288
     * @return \Childish\connection\Connection
289
     */
290
    public static function resolveConnection($connection = null)
291
    {
292
        return static::$resolver->connection($connection);
293
    }
294
295
    /**
296
     * Get the connection resolver instance.
297
     *
298
     * @return  \Childish\database\DatabaseManager $resolver
299
     */
300
    public static function getConnectionResolver()
301
    {
302
        return static::$resolver;
303
    }
304
305
    /**
306
     * et the connection resolver instance.
307
     *
308
     * @static
309
     * @param \Childish\database\DatabaseManager $resolver
310
     */
311
    public static function setConnectionResolver(DatabaseManager $resolver)
312
    {
313
        static::$resolver = $resolver;
314
    }
315
316
    /**
317
     * Unset the connection resolver for models.
318
     *
319
     * @return void
320
     */
321
    public static function unsetConnectionResolver()
322
    {
323
        static::$resolver = null;
324
    }
325
326
    /**
327
     * Get the table associated with the model.
328
     *
329
     * @return string
330
     */
331
    public function getTable()
332
    {
333
        if (!isset($this->table)) {
334
            return str_replace('\\', '', Tools::snake(Tools::plural(Tools::Basename($this))));
335
        }
336
337
        return $this->table;
338
    }
339
340
    /**
341
     * Set the table associated with the model.
342
     *
343
     * @param  string $table
344
     * @return $this
345
     */
346
    public function setTable($table)
347
    {
348
        $this->table = $table;
349
350
        return $this;
351
    }
352
353
    /**
354
     * Get the name of the "created at" column.
355
     *
356
     * @return string
357
     */
358
    public function getCreatedAtColumn()
359
    {
360
        return static::CREATED_AT;
361
    }
362
363
    /**
364
     * Get the name of the "updated at" column.
365
     *
366
     * @return string
367
     */
368
    public function getUpdatedAtColumn()
369
    {
370
        return static::UPDATED_AT;
371
    }
372
373
    /**
374
     * Get a fresh timestamp for the model.
375
     *
376
     * @return string
377
     */
378
    public function freshTimestampString()
379
    {
380
        if (false === static::DATE_FORMAT) {
381
            return time();
382
        }
383
        return date(static::DATE_FORMAT);
384
    }
385
386
    /**
387
     * Create a new model instance that is existing.
388
     *
389
     * @param  array       $attributes
390
     * @param  string|null $connection
391
     * @return static
392
     */
393
    public function newFromBuilder($attributes = [], $connection = null)
394
    {
395
        $model = $this->newInstance([], true);
396
397
        $model->attributes = (array)$attributes;
398
399
        $model->setConnection($connection ? : $this->getConnectionName());
400
401
        return $model;
402
    }
403
}