Test Failed
Pull Request — master (#40)
by Giacomo
04:25
created

Model::getAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace OfflineAgency\MongoAutoSync\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model as BaseModel;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Jenssegers\Mongodb\Eloquent\Model as MongoDbModel;
9
10
class Model extends MongoDbModel
11
{
12
    use EmbedsRelationships;
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function setAttribute($key, $value)
18
    {
19
        // Convert _id to ObjectID.
20
        if ($key == '_id' && is_string($value)) {
21
            $builder = $this->newBaseQueryBuilder();
22
23
            $value = $builder->convertKey($value);
24
        } // Support keys in dot notation.
25
        elseif (Str::contains($key, '.')) {
26
            if (in_array($key, $this->getDates()) && $value) {
27
                $value = $this->fromDateTime($value);
28
            }
29
30
            Arr::set($this->attributes, $key, $value);
31
32
            return;
33
        }
34
35
        return BaseModel::setAttribute($key, $value);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getAttribute($key)
42
    {
43
        // This checks for embedded relation support.
44
        if (method_exists($this, $key) && ! method_exists(self::class, $key)) {
45
            return $this->getRelationValue($key);
46
        }
47
48
        return BaseModel::getAttribute($key);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function newEloquentBuilder($query)
55
    {
56
        return new Builder($query);
57
    }
58
}
59